简体   繁体   English

有没有办法使用 AWS-CDK 将新的 lambda function 连接到现有的 AWS ApiGateway? (Python)

[英]Is there a way to connect a new lambda function an existing AWS ApiGateway using AWS-CDK? (Python)

I am trying to wrap my head around AWS-CDK to deploy Lambda functions to aws through it.我正试图围绕 AWS-CDK 将 Lambda 功能部署到 AWS。 I already have a pre-existing Api gateway, that I have manually deployed through the console and would like to know if there is any means to connect it as a trigger to new lambda functions deployed using the CDK?我已经有一个预先存在的 Api 网关,我通过控制台手动部署了它,想知道是否有任何方法可以将它作为触发器连接到使用 CDK 部署的新 lambda 功能? I have read through the example code:我已阅读示例代码:

apigw.LambdaRestApi(
            self, 'Endpoint',
            handler=my_lambda,
        ) 

This creates a new gateway in aws, which is not the functionality that I am looking for.这在 aws 中创建了一个新网关,这不是我正在寻找的功能。

Any help would be much appreciated!任何帮助将非常感激!

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM