使用AWS CDK部署多个API Gateway阶段 [英] Deploy multiple API Gateway stages with AWS CDK
问题描述
dev
、test
、prod
),通过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
阶段被prod
阶段覆盖。
在尝试上述方法之前,我认为只需创建一个或多个Stage
构造对象并将它们分配给相同的部署(该部署已经将RestApi
作为参数)。
但是需要通过api.deploymentStage = stage
给API显式分配一个Stage,看起来只能分配一个。
这意味着这是不可能的,您必须为test
、prod
等创建不同的堆栈。这意味着同一API网关和Lambda函数有多个实例。
更新
经过进一步修改,我发现似乎可以部署多个阶段,尽管我还没有完全脱离险境.
首先,恢复为RestApi
-remove propdeploy: false
的默认行为,它会自动创建Deployment
:
const api = new apigw.RestApi(this, 'test_api');
然后,与前面一样,创建显式Deployment
构造:
const deployment = new apigw.Deployment(this, 'test_deployment', { api });
此时,重要的是要注意,prod
阶段已经定义,如果您为prod
显式创建Stage
构造,cdk deploy
将失败。
相反,请为要创建的每个其他舞台创建Stage
结构;例如:
new apigw.Stage(this, 'stage_test', { deployment, stageName: 'test' });
new apigw.Stage(this, 'stage_dev', { deployment, stageName: 'dev' });
// etc.
此部署和prod
按预期工作。但是,test
和dev
都将失败,并显示500内部服务器错误和以下错误消息:
在AWS控制台中手动重新分配lambda将应用权限。我还没有想好如何在CDK中解决此问题。由于配置错误,执行失败:Lambda函数的权限无效
推荐答案
这应该可以解决问题。请注意,我已经将资源从test_lambda
重命名为my_lambda
,以避免念力使用Stage名称。还要注意,为简洁起见,我删除了lambda的environment
变量。
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网关的调用访问权限允许所有其他阶段(与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'
};
}
这篇关于使用AWS CDK部署多个API Gateway阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!