如何使用 CDK-2.0 创建API GW-V2与Lambda?

0

【以下的问题经过翻译处理】 我想创建一个代码仓库、构建、CI/CD流水线来部署Kotlin代码到Lambda。Lambda将从API GW调用。我找不到一个好的代码参考来写API GW-V2与Lambda整合的Java-CDK-2.19.0版本。请问是否有人可以分享Java的apigw-v2样例CDK代码参考?

1 回答
0

【以下的回答经过翻译处理】 截至2022年4月4日,CDK-2.19.0版本中似乎没有API GW v2的L2 Builder可用。目前处于alpha状态,可以在https://docs.aws.amazon.com/cdk/api/v2/docs/aws-apigatewayv2-alpha-readme.html中找到参考文档。

下面代码可参考。

<!-- https://mvnrepository.com/artifact/software.amazon.awscdk/apigatewayv2-alpha -->
        <dependency>
            <groupId>software.amazon.awscdk</groupId>
            <artifactId>apigatewayv2-alpha</artifactId>
            <version>2.19.0-alpha.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/software.amazon.awscdk/apigatewayv2-integrations-alpha -->
        <dependency>
            <groupId>software.amazon.awscdk</groupId>
            <artifactId>apigatewayv2-integrations-alpha</artifactId>
            <version>2.19.0-alpha.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/software.amazon.awscdk/apigatewayv2-authorizers-alpha -->
        <dependency>
            <groupId>software.amazon.awscdk</groupId>
            <artifactId>apigatewayv2-authorizers-alpha</artifactId>
            <version>2.19.0-alpha.0</version>
        </dependency>

//imports
import software.amazon.awscdk.services.apigatewayv2.alpha.AddRoutesOptions;
import software.amazon.awscdk.services.apigatewayv2.alpha.HttpApi;
import software.amazon.awscdk.services.apigatewayv2.alpha.HttpMethod;
import software.amazon.awscdk.services.apigatewayv2.alpha.HttpStage;
import software.amazon.awscdk.services.apigatewayv2.integrations.alpha.HttpLambdaIntegration;

//code
Function lambdaFunction = Function.Builder.create(this, "LambdaFunction")
                .runtime(Runtime.JAVA_11)
                .code(Code.fromAsset("software/Test/build/libs/jvmlangs-kotlin-1.0-SNAPSHOT-all.jar"))
                .handler("com.aws.blog.jvmlangs.kotlin.MainTest")
                .memorySize(1024)
                .timeout(Duration.seconds(10))
                .logRetention(RetentionDays.ONE_WEEK)
                .build();

        HttpLambdaIntegration lambdaIntegration = HttpLambdaIntegration.Builder
                .create("TestAPI_GW_v2_Integrations", lambdaFunction)
                .build();

        HttpApi httpApi = HttpApi.Builder
                .create(this, "HttpApi")
                .build();

        httpApi.addRoutes(AddRoutesOptions.builder()
                        .path("/testBasePath")
                        .methods(Arrays.asList(HttpMethod.GET))
                        .integration(lambdaIntegration)
                .build());

        HttpStage.Builder.create(this, "Stage")
                .httpApi(httpApi)
                .stageName("dev")
                .build();
profile picture
专家
已回答 1 年前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则