使用AWS CDK部署多个API网关阶段

时间:2020-06-18 11:40:34

标签: aws-api-gateway aws-cdk

API网关具有阶段(例如:devtestprod)的概念,并且通过AWS控制台部署多个阶段非常简单。

是否可以使用AWS CDK定义和部署多个阶段?

我已经尝试过,但到目前为止似乎还没有实现。以下是一个非常基本的堆栈的简化示例,该堆栈构造了API网关RestApi来提供lambda函数:

export class TestStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // Define stage at deploy time; e.g: STAGE=prod cdk deploy
    const STAGE = process.env.STAGE || 'dev'

    // First, create a test lambda
    const testLambda = new apilambda.Function(this, 'test_lambda', {
      runtime: apilambda.Runtime.NODEJS_10_X,    
      code: apilambda.Code.fromAsset('lambda'),  
      handler: 'test.handler',
      environment: { STAGE }
    })

    // Then, create the API construct, integrate with lambda and define a catch-all method
    const api = new apigw.RestApi(this, 'test_api', { deploy: false });
    const integration = new apigw.LambdaIntegration(testLambda);

    api.root.addMethod('ANY', integration)

    // Then create an explicit Deployment construct
    const deployment  = new apigw.Deployment(this, 'test_deployment', { api });

    // And, a Stage construct
    const stage = new apigw.Stage(this, 'test_stage', { 
      deployment,
      stageName: STAGE
    });

    // There doesn't seem to be a way to add more than one stage...
    api.deploymentStage = stage
  }
}

我不使用LambdaRestApi,因为存在一个不允许显式Deployment的错误,这显然是显式定义Stage所必需的。这种方法需要额外的LambdaIntegration步骤。

此堆栈运行良好-我可以部署一个新堆栈,并使用环境变量定义API Gateway阶段;例如:STAGE=my_stack_name cdk deploy

我希望这可以使我通过执行以下操作来添加阶段:

STAGE=test cdk deploy
STAGE=prod cdk deploy
# etc.

但是,这不起作用-在上面的示例中,test阶段被prod阶段覆盖。

在尝试上述方法之前,我认为可以简单地创建一个或多个Stage构造对象并将它们分配给同一部署(已经以RestApi作为参数)。

但是,有必要通过api.deploymentStage = stage向api明确分配一个阶段,并且看起来只能分配一个阶段。

这意味着不可能,相反,您将不得不为testprod等创建不同的堆栈。这意味着同一个API网关和Lambda函数的多个实例。

更新

进一步修改之后,我发现似乎可以部署多个阶段,尽管我还没有走出困境...

首先,恢复为RestApi的默认行为-删除自动创建deploy: false的道具Deployment

const api = new apigw.RestApi(this, 'test_api');

然后,像以前一样,创建一个显式的Deployment构造:

const deployment  = new apigw.Deployment(this, 'test_deployment', { api });

在这一点上,重要的是要注意prod阶段已已定义,并且如果您为以下内容显式创建cdk deploy构造,则Stage将失败prod

相反,为要创建的其他每个阶段创建一个Stage构造;例如:

new apigw.Stage(this, 'stage_test', { deployment, stageName: 'test' });
new apigw.Stage(this, 'stage_dev', { deployment, stageName: 'dev' });
// etc.

这将部署,prod将按预期工作。但是,testdev都将失败,并显示500 Internal Server Error和以下错误消息:

由于配置错误,执行失败:Lambda函数的权限无效

在AWS Console中手动重新分配lambda将应用权限。我尚未弄清楚如何在CDK中解决此问题。

1 个答案:

答案 0 :(得分:2)

这应该可以解决问题。请注意,我已将资源从test_lambda重命名为my_lambda,以避免与阶段名称混淆。另外请注意,为简洁起见,我将environment变量删除了lambda。

import * as cdk from '@aws-cdk/core';
import * as apigw from '@aws-cdk/aws-apigateway';
import * as lambda from '@aws-cdk/aws-lambda';
import { ServicePrincipal } from '@aws-cdk/aws-iam';

export class ApigwDemoStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // First, create a test lambda
    const myLambda = new lambda.Function(this, 'my_lambda', {
      runtime: lambda.Runtime.NODEJS_10_X,    
      code: lambda.Code.fromAsset('lambda'),  
      handler: 'test.handler'
    });

    // IMPORTANT: Lambda grant invoke to APIGateway
    myLambda.grantInvoke(new ServicePrincipal('apigateway.amazonaws.com'));

    // Then, create the API construct, integrate with lambda
    const api = new apigw.RestApi(this, 'my_api', { deploy: false });
    const integration = new apigw.LambdaIntegration(myLambda);
    api.root.addMethod('ANY', integration)

    // Then create an explicit Deployment construct
    const deployment  = new apigw.Deployment(this, 'my_deployment', { api });

    // And different stages
    const [devStage, testStage, prodStage] = ['dev', 'test', 'prod'].map(item => 
      new apigw.Stage(this, `${item}_stage`, { deployment, stageName: item }));

    api.deploymentStage = prodStage
  }
}

这里要注意的重要部分是:

myLambda.grantInvoke(new ServicePrincipal('apigateway.amazonaws.com'));

明确授予对API Gateway的调用访问权限,所有其他阶段(不直接与API关联的其他阶段)都不会抛出以下错误:

Execution failed due to configuration error: Invalid permissions on Lambda function

我必须通过从控制台显式创建另一个阶段并启用日志跟踪来进行测试。 api的API网关执行日志,并捕获该特定错误。

我自己对此进行了测试。这应该可以解决您的问题。我建议完全创建一个新的堆栈来进行测试。

我超级简单的Lambda代码:

// lambda/test.ts
export const handler = async (event: any = {}) : Promise <any> => {
  console.log("Inside Lambda");
  return { 
    statusCode: 200, 
    body: 'Successfully Invoked Lambda through API Gateway'
  };
}