Dar*_*ght 15 aws-api-gateway aws-cdk
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
}
}
Run Code Online (Sandbox Code Playgroud)
我没有使用,LambdaRestApi因为有一个不允许显式的错误Deployment,这显然是显式定义Stage. 这种方法需要额外的 LambdaIntegration步骤。
这个堆栈运行良好——我可以部署一个新堆栈并使用环境变量定义 API 网关阶段;例如:STAGE=my_stack_name cdk deploy。
我希望这能让我通过执行以下操作来添加阶段:
STAGE=test cdk deploy
STAGE=prod cdk deploy
# etc.
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用 - 在上面的示例中,test舞台被舞台覆盖prod。
在尝试上述方法之前,我认为可以简单地创建一个或多个Stage构造对象并将它们分配给相同的部署(已经将RestApi用作参数)。
但是,需要显式地为 api via 分配一个阶段,api.deploymentStage = stage并且看起来只能分配一个阶段。
这意味着,这是不可能的,相反,你必须创建一个不同的堆栈test,prod等等。这意味着相同的API网关和Lambda函数的多个实例。
经过进一步的修改,我发现似乎可以部署不止一个阶段,尽管我还没有完全摆脱困境......
首先,恢复到默认行为RestApi— remove propdeploy: false自动创建一个Deployment:
const api = new apigw.RestApi(this, 'test_api');
Run Code Online (Sandbox Code Playgroud)
然后,像以前一样,创建一个显式Deployment构造:
const deployment = new apigw.Deployment(this, 'test_deployment', { api });
Run Code Online (Sandbox Code Playgroud)
在这一点上,重要的是要注意一个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.
Run Code Online (Sandbox Code Playgroud)
这prod将按预期部署和工作。但是,test和dev都将失败并显示 500 Internal Server Error 和以下错误消息:
由于配置错误,执行失败:Lambda 函数的权限无效
在 AWS 控制台中手动重新分配 lambda 会应用权限。我还没有想出如何在 CDK 中解决这个问题。
这应该可以解决问题。请注意,我已将资源从 重命名为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
}
}
Run Code Online (Sandbox Code Playgroud)
这里要注意的重要部分是:
myLambda.grantInvoke(new ServicePrincipal('apigateway.amazonaws.com'));
Run Code Online (Sandbox Code Playgroud)
显式授予对 API Gateway 的调用访问权限允许所有其他阶段(不直接与 API 关联)不会抛出以下错误:
Execution failed due to configuration error: Invalid permissions on Lambda function
Run Code Online (Sandbox Code Playgroud)
我必须通过从控制台显式创建另一个阶段并启用日志跟踪来测试它。api 和 stage 的 API Gateway 执行日志捕获了此特定错误。
我自己测试过了。这应该可以解决您的问题。我建议完全创建一个新堆栈来测试这一点。
我的超级简单的 Lambda 代码:
Execution failed due to configuration error: Invalid permissions on Lambda function
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4001 次 |
| 最近记录: |