-
Notifications
You must be signed in to change notification settings - Fork 1
/
InfrastructureStack.java
165 lines (137 loc) · 7.65 KB
/
InfrastructureStack.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package virtua.demo.graalvm.lambda;
import org.jetbrains.annotations.NotNull;
import software.amazon.awscdk.BundlingOptions;
import software.amazon.awscdk.CfnOutput;
import software.amazon.awscdk.CfnOutputProps;
import software.amazon.awscdk.DockerImage;
import software.amazon.awscdk.DockerVolume;
import software.amazon.awscdk.Stack;
import software.amazon.awscdk.StackProps;
import software.amazon.awscdk.services.apigatewayv2.alpha.AddRoutesOptions;
import software.amazon.awscdk.services.apigatewayv2.alpha.HttpApi;
import software.amazon.awscdk.services.apigatewayv2.alpha.HttpApiProps;
import software.amazon.awscdk.services.apigatewayv2.alpha.HttpMethod;
import software.amazon.awscdk.services.apigatewayv2.alpha.PayloadFormatVersion;
import software.amazon.awscdk.services.apigatewayv2.integrations.alpha.HttpLambdaIntegration;
import software.amazon.awscdk.services.apigatewayv2.integrations.alpha.HttpLambdaIntegrationProps;
import software.amazon.awscdk.services.lambda.Architecture;
import software.amazon.awscdk.services.lambda.Code;
import software.amazon.awscdk.services.lambda.Function;
import software.amazon.awscdk.services.lambda.FunctionProps;
import software.amazon.awscdk.services.lambda.Runtime;
import software.amazon.awscdk.services.lambda.SnapStartConf;
import software.amazon.awscdk.services.s3.assets.AssetOptions;
import software.constructs.Construct;
import java.util.Arrays;
import java.util.List;
import static java.util.Collections.singletonList;
import static software.amazon.awscdk.BundlingOutput.ARCHIVED;
@SuppressWarnings("unused")
public class InfrastructureStack extends Stack {
public static final String HELLO_WORLD_JVM_PATH = "/hello-world-jvm";
public static final String HELLO_WORLD_JVM_SNAPSTART_PATH = "/hello-world-jvm-snapstert";
public static final String HELLO_WORLD_GRAAL_PATH = "/hello-world-graal";
public static final String HELLO_WORLD_JVM_SNAP_START_API_URL = "HelloWorldJvmSnapStartApiUrl";
public static final String HELLO_WORLD_JVM_API_URL = "HelloWorldJvmApiUrl";
public static final String HELLO_WORLD_GRAAL_API_URL = "HelloWorldGraalApiUrl";
public InfrastructureStack(final Construct parent, final String id) {
this(parent, id, null);
}
public InfrastructureStack(final Construct parent, final String id, final StackProps props) {
super(parent, id, props);
HttpApi httpApi = new HttpApi(this, "GraalVmLambdaDemoApi", HttpApiProps.builder()
.apiName("GraalVmLambdaDemoApi")
.build());
configureJvmFunction(httpApi);
configureJvmSnapStartFunction(httpApi);
configureGraalVmFunction(httpApi);
}
private void configureJvmFunction(HttpApi httpApi) {
Function jvmHelloWorldFunction = new Function(this, "HelloWorldFunctionJvm", FunctionProps.builder()
.runtime(Runtime.JAVA_11)
.code(Code.fromAsset("../function/target/hello-world-lambda.jar"))
.handler("virtua.demo.graalvm.lambda.HelloWorldRequestHandler")
.memorySize(2048)
.build());
httpApi.addRoutes(AddRoutesOptions.builder()
.path(HELLO_WORLD_JVM_PATH)
.methods(singletonList(HttpMethod.GET))
.integration(new HttpLambdaIntegration("HelloWorldFunctionJvm", jvmHelloWorldFunction,
HttpLambdaIntegrationProps.builder()
.payloadFormatVersion(PayloadFormatVersion.VERSION_2_0)
.build()))
.build());
// Output the URL for the API to the console after creating the CloudFormation stack
new CfnOutput(this, HELLO_WORLD_JVM_API_URL, CfnOutputProps.builder()
.exportName(HELLO_WORLD_JVM_API_URL)
.value(httpApi.getApiEndpoint() + HELLO_WORLD_JVM_PATH)
.build());
}
private void configureJvmSnapStartFunction(HttpApi httpApi) {
Function jvmHelloWorldFunction = new Function(this, "HelloWorldFunctionJvmSnapStart", FunctionProps.builder()
.runtime(Runtime.JAVA_11)
.code(Code.fromAsset("../function/target/hello-world-lambda.jar"))
.handler("virtua.demo.graalvm.lambda.HelloWorldRequestHandler")
.snapStart(SnapStartConf.ON_PUBLISHED_VERSIONS)
.memorySize(2048)
.build());
httpApi.addRoutes(AddRoutesOptions.builder()
.path(HELLO_WORLD_JVM_SNAPSTART_PATH)
.methods(singletonList(HttpMethod.GET))
.integration(new HttpLambdaIntegration(HELLO_WORLD_JVM_SNAP_START_API_URL, jvmHelloWorldFunction,
HttpLambdaIntegrationProps.builder()
.payloadFormatVersion(PayloadFormatVersion.VERSION_2_0)
.build()))
.build());
// Output the URL for the API to the console after creating the CloudFormation stack
new CfnOutput(this, HELLO_WORLD_JVM_SNAP_START_API_URL, CfnOutputProps.builder()
.exportName(HELLO_WORLD_JVM_SNAP_START_API_URL)
.value(httpApi.getApiEndpoint() + HELLO_WORLD_JVM_SNAPSTART_PATH)
.build());
}
private void configureGraalVmFunction(@NotNull HttpApi httpApi) {
List<String> functionOnePackagingInstructions = Arrays.asList(
"-c",
"mvn clean install -P native-image && cp /asset-input/target/function.zip /asset-output/ && ls -la /asset-input/target"
);
BundlingOptions.Builder builderOptions = BundlingOptions.builder()
.command(functionOnePackagingInstructions)
// x86 image
// .image(DockerImage.fromRegistry("marksailes/al2-graalvm:al2-21.2.0"))
// ARM image
.image(DockerImage.fromRegistry("marksailes/arm64-al2-graalvm:11-22.2.0"))
.volumes(singletonList(
DockerVolume.builder()
.hostPath(System.getProperty("user.home") + "/.m2/")
.containerPath("/root/.m2/")
.build()
))
.user("root")
.outputType(ARCHIVED);
Function graalHelloWorldFunction = new Function(this, "HelloWorldFunctionGraalVm", FunctionProps.builder()
.runtime(Runtime.PROVIDED_AL2)
.code(Code.fromAsset("../function/", AssetOptions.builder()
.bundling(builderOptions.build())
.build()))
.handler("virtua.demo.graalvm.lambda.HelloWorldRequestHandler")
.memorySize(256)
// x86 deployment
//.architectures(singletonList(Architecture.X86_64))
// ARM deployment
.architecture(Architecture.ARM_64)
.build());
httpApi.addRoutes(AddRoutesOptions.builder()
.path(HELLO_WORLD_GRAAL_PATH)
.methods(singletonList(HttpMethod.GET))
.integration(new HttpLambdaIntegration(HELLO_WORLD_GRAAL_API_URL, graalHelloWorldFunction,
HttpLambdaIntegrationProps.builder()
.payloadFormatVersion(PayloadFormatVersion.VERSION_2_0)
.build()))
.build());
// Output the URL for the API to the console after creating the CloudFormation stack
new CfnOutput(this, HELLO_WORLD_GRAAL_API_URL, CfnOutputProps.builder()
.exportName(HELLO_WORLD_GRAAL_API_URL)
.value(httpApi.getApiEndpoint() + HELLO_WORLD_GRAAL_PATH)
.build());
}
}