amazon-web-services - AWS CDK 事件桥和 api 网关 AWS 示例不起作用
问题描述
我按照此处的说明设置事件桥:https ://eventbus-cdk.workshop.aws/en/04-api-gateway-service-integrations/01-rest-api/rest-apis.html
根据错误消息,错误来自这行代码:languageResource.addMethod("POST", new apigw.Integration({
我不确定是什么导致了这个问题,因为这是 AWS 给出的一个例子,应该可以工作,但事实并非如此。
我可以构建它,但它在 cdk deploy 上失败并出现以下错误:
CREATE_FAILED | AWS::ApiGateway::Method | MyRestAPI/Default/{language}/POST (MyRestAPIlanguagePOSTB787D51A) Invalid Resource identifier specified (Service: AmazonApiGateway; Status Code: 404; Error Code: NotFoundException;
代码如下:
const myLambda = new lambda.Function(this, "MyEventProcessor", {
code: new lambda.InlineCode("def main(event, context):\n\tprint(event)\n\treturn {'statusCode': 200, 'body': 'Hello, World'}"),
handler: "index.main",
runtime: lambda.Runtime.PYTHON_3_7
})
const bus = new events.EventBus(this, `pwm-${this.stage}-MdpEventBus`)
new cdk.CfnOutput(this, "PwmMdpEventBus", {value: bus.eventBusName})
new events.Rule(this, `PwmMdpEventBusRule`, {
eventBus: bus,
eventPattern: {source: [`com.amazon.alexa.english`]},
targets: [new targets.LambdaFunction(myLambda)]
})
const apigwRole = new iam.Role(this, "MYAPIGWRole", {
assumedBy: new iam.ServicePrincipal("apigateway"),
inlinePolicies: {
"putEvents": new iam.PolicyDocument({
statements: [new iam.PolicyStatement({
actions: ["events:PutEvents"],
resources: [bus.eventBusArn]
})]
})
}
});
const options = {
credentialsRole: apigwRole,
requestParameters: {
"integration.request.header.X-Amz-Target": "'AWSEvents.PutEvents'",
"integration.request.header.Content-Type": "'application/x-amz-json-1.1'"
},
requestTemplates: {
"application/json": `#set($language=$input.params('language'))\n{"Entries": [{"Source": "com.amazon.alexa.$language", "Detail": "$util.escapeJavaScript($input.body)", "Resources": ["resource1", "resource2"], "DetailType": "myDetailType", "EventBusName": "${bus.eventBusName}"}]}`
},
integrationResponses: [{
statusCode: "200",
responseTemplates: {
"application/json": ""
}
}]
}
const myRestAPI = new apigw.RestApi(this, "MyRestAPI");
const languageResource = myRestAPI.root.addResource("{language}");
languageResource.addMethod("POST", new apigw.Integration({
type: apigw.IntegrationType.AWS,
uri: `arn:aws:apigateway:${cdk.Aws.REGION}:events:path//`,
integrationHttpMethod: "POST",
options: options,
}),
{
methodResponses: [{
statusCode: "200"
}],
requestModels: {"application/json": model.getModel(this, myRestAPI) },
requestValidator: new apigw.RequestValidator(this, "myValidator", {
restApi: myRestAPI,
validateRequestBody: true
})
})
解决方案
在 AWS 示例中,它们将您的代码封装在里面
export class MyCdkAppStack extends cdk.Stack {
...
}
你错过了那个封装吗?我注意到您的示例代码没有包含它。因为当你执行时const myRestAPI = new apigw.RestApi(this, "MyRestAPI");
应该this
引用MyCdkAppStack
实例。
推荐阅读
- c++ - 在 C++ 中打印链接列表时出错?
- r - 从特定列创建 varSelectInput
- winapi - 无法在win32的属性页中更改状态栏控件的高度
- php - 在 ajax 上使用 jasny/sso 时获取正确的服务器会话 id 的问题
- share-button - 在 Facebook Workplace 上共享带有自定义消息的网站
- java - 合并共享单词链接的行
- android-studio - 与我添加到购物车的项目一样多,它多次显示总价布局
- ios - 可以通过 WKWebView 中的用户脚本修改 XHR 请求吗?
- python-3.x - 为什么 One2many 字段的默认行没有在 Odoo 13 中自动填写相关值和默认值?
- reactjs - 反应导航依赖问题