【问题标题】:Is there a way to connect a new lambda function an existing AWS ApiGateway using AWS-CDK? (Python)有没有办法使用 AWS-CDK 将新的 lambda 函数连接到现有的 AWS ApiGateway? (Python)
【发布时间】:2020-12-11 16:16:08
【问题描述】:

我正试图围绕 AWS-CDK 将 Lambda 函数部署到 AWS。我已经有一个预先存在的 Api 网关,我已经通过控制台手动部署了它,并且想知道是否有任何方法可以将它作为触发器连接到使用 CDK 部署的新 lambda 函数?我已经阅读了示例代码:

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

这会在 aws 中创建一个新网关,这不是我正在寻找的功能。

任何帮助将不胜感激!

【问题讨论】:

  • @balderman 谢谢你的快速回复,在发帖之前我也经历过这个问题,你能指出任何代码 sn-ps 或实现它的示例吗?
  • 对不起 - 我不知道任何例子。

标签: python amazon-web-services aws-lambda aws-api-gateway aws-cdk


【解决方案1】:
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();

【讨论】:

  • 谁能告诉我,在哪里可以找到restApiId和rootResourceId?)
猜你喜欢
  • 2020-12-13
  • 2020-10-06
  • 2021-02-10
  • 1970-01-01
  • 2023-01-24
  • 2019-12-06
  • 2020-08-05
  • 2020-09-14
  • 1970-01-01
相关资源
最近更新 更多