Create API GW-v2 with Lambda using CDK-2.0

0

I want to create code repo, build, ci/cd pipeline to deploy the kotlin code to lambda. The lambda will be invoked from API GW. I could not find good code reference to write the API GW-V2 with Lambda integration with Java - CDK-2.19.0 . It will be super helpful, if some one share sample cdk code reference in java for apigw-v2 ?

1 Answer
1
Accepted Answer

It looks like the L2 constructs for API GW v2 is not available in CDK - 2.19.0 version as of 4th-Apr-2022. The constructs are available in alpha. The reference is available in https://docs.aws.amazon.com/cdk/api/v2/docs/aws-apigatewayv2-alpha-readme.html

Pasting the code which used to create below for others to refer.

<!-- 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();
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions