问题描述
我试图围绕AWS-CDK部署Lambda函数以通过AWS部署到AWS。我已经有一个预先存在的Api网关,我已经通过控制台手动进行了部署,并且想知道是否有任何方法可以将其作为触发来连接到使用CDK部署的新lambda函数?我已经阅读了示例代码:
apigw.LambdaRestApi(
self,'Endpoint',handler=my_lambda,)
任何帮助将不胜感激!
解决方法
import * as cdk from '@aws-cdk/core';
import * as lambda from '@aws-cdk/aws-lambda';
import * as apigateway from '@aws-cdk/aws-apigateway';
class BindApiFunctionStack extends cdk.Stack {
constructor(scope: cdk.Construct,id: string,props?: cdk.StackProps) {
super(scope,id,props);
const restapi = apigateway.RestApi.fromRestApiAttributes(this,"myapi",{
restApiId : "<yourrestapiid>",rootResourceId : "<yourrootresourceid>"
});
const helloWorld = new lambda.Function(this,"hello",{
runtime: lambda.Runtime.NODEJS_10_X,handler: 'index.handler',code: lambda.Code.fromInline('exports.handler = function(event,ctx,cb) { return cb(null,"hi"); }')
})
restapi.root.addResource("test").addMethod("GET",new apigateway.LambdaIntegration(helloWorld))
}
}
const app = new cdk.App();
new BindApiFunctionStack(app,'MyStack');
app.synth();