1
votes

I create CloudFormation template for my AWS API Gateway and Lambda function and I need to connect specific API Gateway stage to specific Lambda alias. I have two aliases - QA and Prod, and two API stages (QA & Prod too), in CloudFormation template it looks like:

AWSTemplateFormatVersion: "2010-09-09"

Transform: "AWS::Serverless-2016-10-31"

Description: Lambda function configuration

Resources:
  EndpointLambda:
    Type: "AWS::Lambda::Function"
    Properties:
      FunctionName: "endpoint-lambda"
      Handler: "com.test.aws.RequestHandler::handleRequest"
      Runtime: java8
      Code:
        S3Bucket: "lambda-functions"
        S3Key: "test-endpoint-lambda-0.0.1.jar"
      Description: Test Lambda function
      MemorySize: 256
      Timeout: 60
      Environment:
        Variables:
          ES_HOST: test-es-host-url
          ES_ON: true
          ES_PORT: 443
          ES_PROTOCOL: https
          REDIS_URL: test-redis-host-url

  QaLambdaAlias:
    Type: "AWS::Lambda::Alias"
    Properties:
      FunctionName: !Ref EndpointLambda
      FunctionVersion: 1
      Name: "QA"
      Description: "QA alias"

  ProdLambdaAlias:
    Type: "AWS::Lambda::Alias"
    Properties:
      FunctionName: !Ref EndpointLambda
      FunctionVersion: 1
      Name: "Prod"
      Description: "Production alias"

  RestApi:
    Type: "AWS::ApiGateway::RestApi"
    Properties:
      Name: "test-rest-api"
      Description: "Test REST API"

  RestApiResource:
    Type: "AWS::ApiGateway::Resource"
    Properties:
      RestApiId: !Ref "RestApi"
      ParentId: !GetAtt "RestApi.RootResourceId"
      PathPart: "/test"

  RestApiDeployment:
    Type: "AWS::ApiGateway::Deployment"
    Properties:
      RestApiId: !Ref "RestApi"

  QaRestApiStage:
    Type: "AWS::ApiGateway::Stage"
    Properties:
      DeploymentId: !Ref "RestApiDeployment"
      RestApiId: !Ref "RestApi"
      StageName: "qa"

  ProdRestApiStage:
    Type: "AWS::ApiGateway::Stage"
    Properties:
      DeploymentId: !Ref "RestApiDeployment"
      RestApiId: !Ref "RestApi"
      StageName: "prod"

How can I describe in template that QA API stage should call QA alias of Lambda function, and Prod stage - Prod alias?

1

1 Answers

3
votes

To begin with find out how to do it using the GUI. There some documentation about what you want to do here. Theres some extra permissions you'll need to add aswell if this is the first time you've set this up which are included here -

https://docs.aws.amazon.com/apigateway/latest/developerguide/stage-variables.html

But for a quick answer what your looking for is $:{stageVariables.stage} what this does is links the alias of the lambda you want to trigger. In the GUI it'd look something like this: enter image description here

What this will do is allow your lambda to trigger a certain alias. Once this is entered you'll be able to see a new option when using the Testing feature in the API gateway. So here you'd specify QA. Enter the lambda Alias here

So, to reflect this in Cloudformation we need to do something similar -

  RestApi:
      Type: "AWS::ApiGateway::RestApi"
      Properties:
          Name: "test-rest-api"
          Description: "Test REST API"
          paths:
              /ExamplePath:
                put:
                    #Here will go all the configuration setting you want
                    #Such as security, httpMethod, amd passthroughBehavior
                    #But what you need is
                    uri: 'arn:aws:apigateway:${AWS:Region}:lambda:path/2-15/03/31/functions/${LambdaARN}:${!stageVariables.stage}/invocations'

More info on this can be found here: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apitgateway-method-integration.html what you'll want to see is at the very bottom of the page. Hope this helps!