API有阶段的概念(例如: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
,因为有一个bug不允许显式Deployment
,这显然是显式定义Stage
所必需的。这种方法需要额外的LambdaIntegration
步骤。
这个堆栈工作得很好--我可以部署一个新的堆栈,并使用一个环境变量(例如:STAGE=my_stack_name cdk deploy
)定义API阶段。
我希望这将使我能够通过做以下工作来增加阶段:
STAGE=test cdk deploy
STAGE=prod cdk deploy
# etc.
但是,这不起作用--在上面的示例中,test
阶段被prod
阶段覆盖。
在尝试上述方法之前,我认为只需创建一个或多个Stage
构造对象并将它们分配给相同的部署(该部署已经将RestApi
作为参数)。
但是,有必要通过api.deploymentStage = stage
显式地为api分配一个阶段,而且它似乎只能分配一个阶段。
这意味着这是不可能的,相反,您必须为test
、prod
等创建一个不同的堆栈。这意味着同一个API和Lambda函数的多个实例。
更新
经过进一步的修补,我发现部署不止一个阶段似乎是可能的,尽管我还没有完全脱离险境……
首先,恢复到RestApi
- remove deploy: 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个内部服务器错误和以下错误消息中失败:
由于配置错误,
执行失败:对Lambda函数的权限无效
在AWS控制台中手动重新分配lambda应用权限。我还没有想出如何在基民盟解决这个问题。
发布于 2020-06-19 15:55:57
这应该能起作用。请注意,我已将资源从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网关的调用访问允许所有其他阶段(与API没有直接关联)不抛出错误:
Execution failed due to configuration error: Invalid permissions on Lambda function
我不得不通过显式地从控制台创建另一个阶段并启用日志跟踪来测试它。API和stage的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'
};
}
https://stackoverflow.com/questions/62449187
复制相似问题