Question:
AWS API Gateway: Ways to do REST API Versioning

Solution

How to do REST API versioning? In this solution, we are using Terraform instead of AWS Console in the whole process. To perform this process using AWS Console, use the configuration named “Method Execution” in the AWS API Gateway and add the stage variable to the Lambda function. 


The below link will help you test the process using the Terraform:

>https://github.com/FliesWithWind/aws-api-versioning-tf 


You can better understand with the help of this example. In this case, we have a single GET endpoint API with two versions. Moreover, every version refers to a different lambda:


https://{restapi-id}.execute-api.{region}.amazonaws.com/v1/ping


Now we are using points towards the Lambda name “ping-v1”, and


https://{restapi-id}.execute-api.{region}.amazonaws.com/v2/ping


Second, Points towards the lambda named “ping-v2”.


Use the Lambda version variable in the Lambda name presented inside the API deployment stage  This will help you achieve the above version of the Lambda. The below is the Terraform example for v1 version:


resource "aws_api_gateway_stage" "v1" {

  deployment_id = aws_api_gateway_deployment.api-serverless-gateway-deployment.id

  rest_api_id   = aws_api_gateway_rest_api.api-serverless.id

  stage_name    = "v1"

  variables     = {

    lambda_version = "-v1"

  }

}


After this you will have the part of the open API doc, where you can find APIGateway integration with our Lambda. 


"x-amazon-apigateway-integration": {

    "httpMethod": "POST",

    "uri": "arn:aws:apigateway:${aws_region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${aws_region}:${aws_account_id}:function:${ping_lambda_name}$${stageVariables.lambda_version}/invocations",

    "responses": {

    "default": {

    "statusCode": "200"

    }

    },

    "passthroughBehavior": "when_no_match",

    "timeoutInMillis": 200,

    "type": "aws_proxy"

}


The "uri" format is the most crucial component here. You'll probably have to scroll to view the highlighted portion of the URI provided below, but it relates to the lambda_version variable. The $whatever_is_here variable refers to Terraform variables, whereas the $stageVariables.lambda_version variable refers to AWS Stage variables.


arn:aws:apigateway:${aws_region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${aws_region}:${aws_account_id}:function:${ping_lambda_name}$${stageVariables.lambda_version}/invocations


Once, you deploy this, the following is how it will look in the AWS console.

                                                         Source:> Wojtek


Now, it's time to test the solution by ping/calling the endpoint of our variables v1 and v2 API. Below are the results:


                                             Source:> Wojtek

                                                        Source:> Wojtek


Ritu Singh

Ritu Singh

Submit
0 Answers