简体   繁体   中英

How do you point API Gateway to a lambda Alias in CDK?

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.

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.

eg -

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())

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,
  }),
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM