3

Using CDK to connect ApiGateway to a lambda, we first create a RestApi(), and then create a LambdaIntegration to connect the Apigateway to a lambda. How do you do this when working with a lambda Alias?

How to point ApiGateway to a specific Lambda alias explains how to connect ApiG to a Lambda alias without CDK. How can this be translated to CDK?

Our goal is to add provisioned concurrency and autoscaling to a lambda used with API Gateway.

Amit Baranes
  • 7,398
  • 2
  • 31
  • 53
fileyfood500
  • 1,199
  • 1
  • 13
  • 29

2 Answers2

7

LambdaIntegration Function gets handler: IFunction as a parameter which is the lambda object.

With that been said, you can import the lambda version you want using the following code snippet:

const lambdaAlias=lambda.Function.fromFunctionArn(scope,'LambdaImportUsingARN',"lambdaAliasARN")

Replace lambdaAliasARN with the ARN of your lambda.

e.g -

arn:aws:lambda:${AWS_REGION}:${AWS_ACCOUNT}:function:${LAMBDA_NAME}:${ALIAS_NAME}

And later, pass it to LambdaIntegration :

const lambdaIntegrationUsingAlias  = new apigateway.LambdaIntegration(lambdaAlias) 

In addition, the lambda alias needs permissions in order to allow the ApiGateway to invoke it.

const lambda = ...
const alias = Alias(this, "alias-id", AliasProps.builder()
            .provisionedConcurrentExecutions(1)
            .version(lambda.currentVersion)
            .aliasName(ALIAS_NAME)
            .build()
        )
// First add permission for your stage to invoke
alias.addPermission("apigateway-permission", Permission.builder()
            .action("lambda:InvokeFunction")
            .principal(ServicePrincipal("apigateway.amazonaws.com"))          
            .sourceArn("arn:aws:execute-api:$region:$account:${api.restApiId}/$stage/POST/$path")
            .build())
// Next add permission for testing
alias.addPermission("apigateway-test-permission", Permission.builder()
            .action("lambda:InvokeFunction")
            .principal(ServicePrincipal("apigateway.amazonaws.com"))
            .sourceArn("arn:aws:execute-api:$region:$account:${api.restApiId}/test-invoke-stage/POST/$path")
            .build())
Amit Baranes
  • 7,398
  • 2
  • 31
  • 53
3

Here is a TypeScript CDK example of an "API Gateway HTTP API" backed by a Lambda using an alias and autoscaled provisioned concurrency.

See autoscaling docs for more info.

import * as apigateway from '@aws-cdk/aws-apigatewayv2'
import * as apigatewayIntegrations from '@aws-cdk/aws-apigatewayv2-integrations'
import * as lambda from '@aws-cdk/aws-lambda'

const fn = new lambda.Function(this, 'MyFunction', {
  //...
})

const liveAlias = new lambda.Alias(this, 'LiveAlias', {
  aliasName: 'live',
  version: fn.currentVersion,
})

const target = liveAlias.addAutoScaling({
  minCapacity: 1,
  maxCapacity: 100,
})

target.scaleOnUtilization({
  utilizationTarget: 0.75,
})

const httpApi = new apigateway.HttpApi(this, 'HttpApi', {
  defaultIntegration: new apigatewayIntegrations.LambdaProxyIntegration({
    handler: liveAlias,
  }),
})
Dharman
  • 30,962
  • 25
  • 85
  • 135
Mike Vosseller
  • 4,097
  • 5
  • 26
  • 28
  • 1
    This should be the accepted answer. This also works for setting the gateway to point to a Java lambda version with alias with the new SnapStart feature. – The_Real_Epk Jan 19 '23 at 12:46