Vivien Sonntag
Vivien Sonntag

Reputation: 4639

AWS SAM/Cloudformation configure API Gateway to point to lambda function version

I am currently trying to enable provisioned concurrency for my AWS lambda function. I already figured I can only do this on either a Lambda Function Version or a Lambda Function Alias. But I am having a hard time to point my API Gateway to this version, it seems to always point to the base function, not the version.

In the UI I can easily attach my Lambda Function Version to an API Gateway Endpoint, but I cannot figure out how to do it in my SAM Template.

This is what I currently have:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Transform": "AWS::Serverless-2016-10-31",
    "Description": "Desc.",
    "Parameters": { },
    "Resources": {
      "MyLambdaFunction": {
        "Type": "AWS::Serverless::Function",
        "Properties": {
          "Runtime": "dotnetcore3.1",
          "CodeUri": "MyCodeUri",
          "MemorySize": 1024,
          "Timeout": 30,
          "Events": {
            "HttpEvent1": {
              "Type": "Api",
              "Properties": {
                "Path": "/v1/test",
                "Method": "GET",
                "RestApiId": {
                  "Ref": "ApiGateway"
                }
              }
            }
          },
          "Handler": "MyNamespace::MyNamespace.MyFunc::RunAsync"
        }
      },
      "MyLambdaFunctionConcurrentV1": {
        "Type": "AWS::Lambda::Version",
        "Properties": {
          "FunctionName": {
            "Ref": "MyLambdaFunction"
          },
          "ProvisionedConcurrencyConfig": {
            "ProvisionedConcurrentExecutions": 1
          }
        }
      },
      "ApiGateway": {
        "Type": "AWS::Serverless::Api",
        "Properties": {
          "StageName": {
            "Ref": "ApiStageName"
          },
          "Cors": {
            "AllowCredentials": true,
            "AllowHeaders": "'*'",
            "AllowMethods": "'*'",
            "AllowOrigin": "'*'",
            "MaxAge": "'600'"
          }
        }
      }
    },
    "Outputs": {
      "ApiUrl": {
        "Description": "API Gateway Endpoint URL",
        "Value": {
          "Fn::Sub": "https://${ApiGateway}.execute-api.${AWS::Region}.${AWS::URLSuffix}/${ApiStageName}"
        },
        "Export": {
          "Name": {
            "Fn::Sub": "${AWS::StackName}-ApiUrl"
          }
        }
      }
    }
  }

So I am fine to deploy my Lambda function, my API Gateway and my version. But I cannot figure out how to link the API Gateway to my version.

Upvotes: 3

Views: 887

Answers (1)

Vivien Sonntag
Vivien Sonntag

Reputation: 4639

I just figured out I was looking at the wrong documentation. As I have a SAM template and not a Cloudformation template I can use the AutoPublishAlias together with the ProvisionedConcurrencyConfig directly attached to my lambda function.

Knowing this, the solution was way easier - the version is not necessary as the SAM template version of the AWS::Serverless::Function supports ProvisionedConcurrencyConfig directly - as long as AutoPublishAlias is set as well.

This is my working template:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Transform": "AWS::Serverless-2016-10-31",
    "Description": "Desc.",
    "Parameters": { },
    "Resources": {
      "MyLambdaFunction": {
        "Type": "AWS::Serverless::Function",
        "Properties": {
          "AutoPublishAlias": "V1",
          "ProvisionedConcurrencyConfig": {
            "ProvisionedConcurrentExecutions": 1
          },
          "Runtime": "dotnetcore3.1",
          "CodeUri": "MyCodeUri",
          "MemorySize": 1024,
          "Timeout": 30,
          "Events": {
            "HttpEvent1": {
              "Type": "Api",
              "Properties": {
                "Path": "/v1/test",
                "Method": "GET",
                "RestApiId": {
                  "Ref": "ApiGateway"
                }
              }
            }
          },
          "Handler": "MyNamespace::MyNamespace.MyFunc::RunAsync"
        }
      },
      "ApiGateway": {
        "Type": "AWS::Serverless::Api",
        "Properties": {
          "StageName": {
            "Ref": "ApiStageName"
          },
          "Cors": {
            "AllowCredentials": true,
            "AllowHeaders": "'*'",
            "AllowMethods": "'*'",
            "AllowOrigin": "'*'",
            "MaxAge": "'600'"
          }
        }
      }
    },
    "Outputs": {
      "ApiUrl": {
        "Description": "API Gateway Endpoint URL",
        "Value": {
          "Fn::Sub": "https://${ApiGateway}.execute-api.${AWS::Region}.${AWS::URLSuffix}/${ApiStageName}"
        },
        "Export": {
          "Name": {
            "Fn::Sub": "${AWS::StackName}-ApiUrl"
          }
        }
      }
    }
  }

Side note: I also tried to apply the same AutoPublishAlias to multiple functions in the same stack - it works, so it does not need to be unique.

Upvotes: 1

Related Questions