aws-api-gateway - 使用 AWS CDK 部署多个 API Gateway 阶段

标签 aws-api-gateway aws-cdk

API Gateway 有阶段的概念(例如: 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 网关阶段;例如:STAGE=my_stack_name cdk deploy .

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

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

然而,这不起作用——在上面的例子中 test stage 被 prod 覆盖阶段。

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

但是,有必要通过 api.deploymentStage = stage 显式地为 api 分配一个阶段。看起来只能分配一个。

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

更新

经过进一步的修修补补,我发现似乎可以部署不止一个阶段,尽管我还没有完全摆脱困境......

首先,恢复到 RestApi 的默认行为— 移除 Prop 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 和以下错误消息:

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



在 AWS 控制台中手动重新分配 lambda 会应用权限。我还没有想出如何在 CDK 中解决这个问题。

最佳答案

这应该可以解决问题。请注意,我已将资源重命名为 test_lambdamy_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 和 stage 的 API Gateway 执行日志捕获了此特定错误。

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

我的 super 简单的 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'
  };
}

关于aws-api-gateway - 使用 AWS CDK 部署多个 API Gateway 阶段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62449187/

相关文章:

amazon-web-services - 执行 API 网关的 AWS CLI 命令

aws-api-gateway - CDK API Gateway 默认授权方排除 OPTIONS 方法

continuous-integration - 配置 - AWS_CDK vs Troposphere vs Stacker

amazon-web-services - CDK 销毁不会删除 S3 存储桶

typescript - 调试 TypeScript AWS CDK 应用程序

java - 具有独立环境的 AWS API 网关 - 生产和登台

amazon-web-services - AWS API Gateway : Use Mock Integration to echo response body

amazon-ecs - ECS TaskDefinition 创建失败,错误为 "Invalid containerPort'“

nginx - API网关与反向代理

swagger - CDK 部署后 CloudFormation 模板未更新