Skip to content

Commit 4af4296

Browse files
committed
Refactored to CDK v2 and simplified setup
1 parent 59bdf34 commit 4af4296

File tree

9 files changed

+48
-96
lines changed

9 files changed

+48
-96
lines changed

Dockerfile

+5-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM --platform=linux/amd64 amazonlinux:2 AS packer
1+
FROM amazonlinux:2
22

33
# Add the Amazon Corretto repository
44
RUN rpm --import https://yum.corretto.aws/corretto.key
@@ -17,20 +17,14 @@ COPY software software
1717
WORKDIR /software/example-function
1818
RUN mvn clean package
1919

20-
# Find JDK module dependencies dynamically from our uber jar
21-
RUN jdeps \
22-
# dont worry about missing modules
20+
# Find JDK module dependencies dynamically from the uber jar
21+
RUN jdeps -q \
2322
--ignore-missing-deps \
24-
# suppress any warnings printed to console
25-
-q \
26-
# java release version targeting
2723
--multi-release 18 \
28-
# output the dependencies at end of run
2924
--print-module-deps \
30-
# pipe the result of running jdeps on the function jar to file
3125
target/function.jar > jre-deps.info
3226

33-
# Create a slim Java 18 JRE which only contains the required modules to run this function
27+
# Create a slim Java 18 JRE which only contains the required modules to run the function
3428
RUN jlink --verbose \
3529
--compress 2 \
3630
--strip-java-debug-attributes \
@@ -39,14 +33,12 @@ RUN jlink --verbose \
3933
--output /jre18-slim \
4034
--add-modules $(cat jre-deps.info)
4135

42-
4336
# Use Javas Application Class Data Sharing feature
4437
# It creates the file /jre18-slim/lib/server/classes.jsa
45-
RUN /jre18-slim/bin/java -Xshare:dump -Xbootclasspath/a:/software/example-function/target/function.jar
38+
RUN /jre18-slim/bin/java -Xshare:dump
4639

4740
# Package everything together into a custom runtime archive
4841
WORKDIR /
49-
5042
COPY bootstrap bootstrap
5143
RUN chmod 755 bootstrap
5244
RUN cp /software/example-function/target/function.jar function.jar

bootstrap

-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ $LAMBDA_TASK_ROOT/jre18-slim/bin/java \
44
--add-opens java.base/java.util=ALL-UNNAMED \
55
-XX:+TieredCompilation \
66
-XX:TieredStopAtLevel=1 \
7-
-Xshare:on \
87
-XX:+UseSerialGC \
98
-jar function.jar "$_HANDLER"

build.sh

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
#!/bin/sh
2-
# remove a maybe earlier build custom runtime archives
3-
rm runtime.zip
42

5-
# Build the docker image which will:
6-
# 1. Use the latest Amazon Linux 2 image and install Amazon Corretto 18
7-
# 2. Copy the software directory into the Docker container and run the build using Maven, which creates an uber jar
8-
# 3. run jdeps to calculate the module dependencies for this uber jar
9-
# 4. feeding the jdeps result into jlink, creating a minimal Java 18 JRE which only contains the necessary modules to run this jar
10-
# 5. Use Javas Application Class Data Sharing for further optimizations
11-
# 6. Create the runtime.zip archive, based on the AWS Lambda custom runtime specification
3+
# Remove a previously created custom runtime
4+
file="runtime.zip"
5+
if [ -f "$file" ] ; then
6+
rm "$file"
7+
fi
8+
9+
# Build the custom Java runtime from the Dockerfile
1210
docker build -f Dockerfile --progress=plain -t lambda-custom-runtime-minimal-jre-18-x86 .
13-
# Extract the runtime.zip from the Docker container and store it locally
11+
12+
# Extract the runtime.zip from the Docker environment and store it locally
1413
docker run --rm --entrypoint cat lambda-custom-runtime-minimal-jre-18-x86 runtime.zip > runtime.zip

infrastructure/cdk.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"app": "mvn -e -q compile exec:java",
33
"context": {
4-
"@aws-cdk/core:enableStackNameDuplicates": "true",
54
"aws-cdk:enableDiffNoFail": "true",
65
"@aws-cdk/core:stackRelativeExports": "true",
76
"@aws-cdk/aws-ecr-assets:dockerIgnoreSupport": true,

infrastructure/pom.xml

+5-27
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<properties>
1111
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12-
<cdk.version>1.132.0</cdk.version>
12+
<cdk.version>2.19.0</cdk.version>
1313
<junit.version>5.7.0</junit.version>
1414
</properties>
1515

@@ -20,8 +20,8 @@
2020
<artifactId>maven-compiler-plugin</artifactId>
2121
<version>3.8.1</version>
2222
<configuration>
23-
<source>9</source>
24-
<target>9</target>
23+
<source>11</source>
24+
<target>11</target>
2525
</configuration>
2626
</plugin>
2727

@@ -40,31 +40,9 @@
4040
<!-- AWS Cloud Development Kit -->
4141
<dependency>
4242
<groupId>software.amazon.awscdk</groupId>
43-
<artifactId>core</artifactId>
44-
<version>${cdk.version}</version>
43+
<artifactId>aws-cdk-lib</artifactId>
44+
<version>2.19.0</version>
4545
</dependency>
4646

47-
<dependency>
48-
<groupId>software.amazon.awscdk</groupId>
49-
<artifactId>lambda</artifactId>
50-
<version>${cdk.version}</version>
51-
</dependency>
52-
<dependency>
53-
<groupId>software.amazon.awscdk</groupId>
54-
<artifactId>apigatewayv2</artifactId>
55-
<version>${cdk.version}</version>
56-
</dependency>
57-
<dependency>
58-
<groupId>software.amazon.awscdk</groupId>
59-
<artifactId>apigatewayv2-integrations</artifactId>
60-
<version>${cdk.version}</version>
61-
</dependency>
62-
<dependency>
63-
<groupId>software.amazon.awscdk</groupId>
64-
<artifactId>dynamodb</artifactId>
65-
<version>${cdk.version}</version>
66-
</dependency>
67-
68-
6947
</dependencies>
7048
</project>

infrastructure/src/main/java/com/amazon/aws/example/InfrastructureApp.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.amazon.aws.example;
22

3-
import software.amazon.awscdk.core.App;
3+
import software.amazon.awscdk.App;
44

55
public class InfrastructureApp {
66
public static void main(final String[] args) {
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,19 @@
11
package com.amazon.aws.example;
22

3-
import software.amazon.awscdk.core.CfnOutput;
4-
import software.amazon.awscdk.core.CfnOutputProps;
5-
import software.amazon.awscdk.core.Construct;
6-
import software.amazon.awscdk.core.Duration;
7-
import software.amazon.awscdk.core.Stack;
8-
import software.amazon.awscdk.core.StackProps;
9-
import software.amazon.awscdk.services.apigatewayv2.AddRoutesOptions;
10-
import software.amazon.awscdk.services.apigatewayv2.HttpApi;
11-
import software.amazon.awscdk.services.apigatewayv2.HttpApiProps;
12-
import software.amazon.awscdk.services.apigatewayv2.HttpMethod;
13-
import software.amazon.awscdk.services.apigatewayv2.PayloadFormatVersion;
14-
import software.amazon.awscdk.services.apigatewayv2.integrations.LambdaProxyIntegration;
15-
import software.amazon.awscdk.services.apigatewayv2.integrations.LambdaProxyIntegrationProps;
16-
import software.amazon.awscdk.services.dynamodb.Attribute;
17-
import software.amazon.awscdk.services.dynamodb.AttributeType;
18-
import software.amazon.awscdk.services.dynamodb.BillingMode;
19-
import software.amazon.awscdk.services.dynamodb.Table;
20-
import software.amazon.awscdk.services.dynamodb.TableProps;
21-
import software.amazon.awscdk.services.lambda.*;
3+
import software.amazon.awscdk.*;
4+
import software.amazon.awscdk.services.apigateway.LambdaIntegration;
5+
import software.amazon.awscdk.services.apigateway.RestApi;
6+
import software.amazon.awscdk.services.apigateway.RestApiProps;
7+
import software.amazon.awscdk.services.dynamodb.*;
8+
import software.amazon.awscdk.services.lambda.Code;
9+
import software.amazon.awscdk.services.lambda.Function;
10+
import software.amazon.awscdk.services.lambda.FunctionProps;
2211
import software.amazon.awscdk.services.lambda.Runtime;
2312
import software.amazon.awscdk.services.logs.RetentionDays;
13+
import software.constructs.Construct;
2414

25-
import java.util.HashMap;
2615
import java.util.Map;
2716

28-
import static java.util.Collections.singletonList;
29-
3017
public class InfrastructureStack extends Stack {
3118
public InfrastructureStack(final Construct scope, final String id) {
3219
this(scope, id, null);
@@ -35,18 +22,17 @@ public InfrastructureStack(final Construct scope, final String id) {
3522
public InfrastructureStack(final Construct scope, final String id, final StackProps props) {
3623
super(scope, id, props);
3724

38-
Table exampleTable = new Table(this, "ExampleTable", TableProps.builder()
25+
var exampleTable = new Table(this, "ExampleTable", TableProps.builder()
3926
.partitionKey(Attribute.builder()
4027
.type(AttributeType.STRING)
4128
.name("id").build())
4229
.billingMode(BillingMode.PAY_PER_REQUEST)
4330
.build());
4431

45-
Function customJava18Function = new Function(this, "LambdaCustomRuntimeJava18", FunctionProps.builder()
32+
var customJava18Function = new Function(this, "LambdaCustomRuntimeJava18", FunctionProps.builder()
4633
.functionName("custom-runtime-java-18")
4734
.handler("com.amazon.aws.example.ExampleDynamoDbHandler::handleRequest")
4835
.runtime(Runtime.PROVIDED_AL2)
49-
.architecture(Architecture.X86_64)
5036
.code(Code.fromAsset("../runtime.zip"))
5137
.memorySize(512)
5238
.environment(Map.of("TABLE_NAME", exampleTable.getTableName()))
@@ -56,21 +42,16 @@ public InfrastructureStack(final Construct scope, final String id, final StackPr
5642

5743
exampleTable.grantWriteData(customJava18Function);
5844

59-
HttpApi httpApi = new HttpApi(this, "ExampleApi", HttpApiProps.builder()
60-
.apiName("ExampleApi")
45+
var restApi = new RestApi(this, "ExampleApi", RestApiProps.builder()
46+
.restApiName("ExampleApi")
6147
.build());
6248

63-
httpApi.addRoutes(AddRoutesOptions.builder()
64-
.path("/custom-runtime")
65-
.methods(singletonList(HttpMethod.POST))
66-
.integration(new LambdaProxyIntegration(LambdaProxyIntegrationProps.builder()
67-
.handler(customJava18Function)
68-
.payloadFormatVersion(PayloadFormatVersion.VERSION_2_0)
69-
.build()))
70-
.build());
49+
restApi.getRoot()
50+
.addResource("custom-runtime")
51+
.addMethod("POST", new LambdaIntegration(customJava18Function));
7152

7253
new CfnOutput(this, "api-endpoint", CfnOutputProps.builder()
73-
.value(httpApi.getApiEndpoint())
54+
.value(restApi.getUrl())
7455
.build());
7556
}
7657
}

provision-infrastructure.sh

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
#!/bin/sh
22
set -e
33

4-
# let's remember the projects root directory location
4+
# Remember the projects root directory location
55
PROJECT_ROOT_DIRECTORY=$(pwd)
66

7-
# navigate into the infrastructure sub-directory
7+
# Navigate into the infrastructure sub-directory
88
cd infrastructure
99

10-
# deploy the AWS infrastructure
10+
# Deploy the AWS infrastructure via AWS CDK and store the outputs in a file
1111
cdk deploy --outputs-file target/outputs.json
1212

13-
# test the Amazon API Gateway endpoint
14-
# we should see an HTTP 200 status code
15-
curl -i -XPOST $(cat target/outputs.json | jq -r '.LambdaCustomRuntimeMinimalJRE18InfrastructureStack.apiendpoint')/custom-runtime
13+
# Test the Amazon API Gateway endpoint - We should see a "successful" message
14+
curl -XPOST $(cat target/outputs.json | jq -r '.LambdaCustomRuntimeMinimalJRE18InfrastructureStack.apiendpoint')/custom-runtime
1615

17-
# navigate back into the projects root directory
16+
# Navigate back into the projects root directory
1817
cd $(pwd)

software/example-function/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
<artifactId>aws-lambda-java-core</artifactId>
3030
<version>1.2.1</version>
3131
</dependency>
32+
<dependency>
33+
<groupId>org.slf4j</groupId>
34+
<artifactId>slf4j-simple</artifactId>
35+
<version>1.7.36</version>
36+
</dependency>
3237
<dependency>
3338
<groupId>com.amazonaws</groupId>
3439
<artifactId>aws-lambda-java-events</artifactId>

0 commit comments

Comments
 (0)