-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInfrastructureStack.java
114 lines (100 loc) · 5.19 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
package example;
import software.amazon.awscdk.core.CfnOutput;
import software.amazon.awscdk.core.CfnOutputProps;
import software.amazon.awscdk.core.Construct;
import software.amazon.awscdk.core.Duration;
import software.amazon.awscdk.core.Stack;
import software.amazon.awscdk.core.StackProps;
import software.amazon.awscdk.services.apigatewayv2.AddRoutesOptions;
import software.amazon.awscdk.services.apigatewayv2.HttpApi;
import software.amazon.awscdk.services.apigatewayv2.HttpApiProps;
import software.amazon.awscdk.services.apigatewayv2.HttpMethod;
import software.amazon.awscdk.services.apigatewayv2.PayloadFormatVersion;
import software.amazon.awscdk.services.apigatewayv2.integrations.LambdaProxyIntegration;
import software.amazon.awscdk.services.apigatewayv2.integrations.LambdaProxyIntegrationProps;
import software.amazon.awscdk.services.dynamodb.Attribute;
import software.amazon.awscdk.services.dynamodb.AttributeType;
import software.amazon.awscdk.services.dynamodb.BillingMode;
import software.amazon.awscdk.services.dynamodb.Table;
import software.amazon.awscdk.services.dynamodb.TableProps;
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.LayerVersion;
import software.amazon.awscdk.services.lambda.LayerVersionProps;
import software.amazon.awscdk.services.lambda.Runtime;
import software.amazon.awscdk.services.logs.RetentionDays;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import static java.util.Collections.singletonList;
public class InfrastructureStack extends Stack {
public InfrastructureStack(final Construct scope, final String id) {
this(scope, id, null);
}
public InfrastructureStack(final Construct scope, final String id, final StackProps props) {
super(scope, id, props);
Table exampleTable = new Table(this, "ExampleTable", TableProps.builder()
.partitionKey(Attribute.builder()
.type(AttributeType.STRING)
.name("id").build())
.billingMode(BillingMode.PAY_PER_REQUEST)
.build());
Function exampleWithoutTieredComp = new Function(this, "ExampleWithoutTieredComp", FunctionProps.builder()
.functionName("example-without-tiered-comp")
.description("example-without-tiered-comp")
.handler("example.ExampleDynamoDbHandler::handleRequest")
.runtime(Runtime.JAVA_11)
.code(Code.fromAsset("../software/ExampleFunction/target/example.jar"))
.memorySize(512)
.environment(mapOf("TABLE_NAME", exampleTable.getTableName()))
.timeout(Duration.seconds(20))
.logRetention(RetentionDays.ONE_WEEK)
.build());
Function exampleWithTieredComp = new Function(this, "ExampleWithTieredComp", FunctionProps.builder()
.functionName("example-with-tiered-comp")
.description("example-with-tiered-comp")
.handler("example.ExampleDynamoDbHandler::handleRequest")
.runtime(Runtime.JAVA_11)
.code(Code.fromAsset("../software/ExampleFunction/target/example.jar"))
.memorySize(512)
.environment(mapOf("TABLE_NAME", exampleTable.getTableName(),
"JAVA_TOOL_OPTIONS", "-XX:+TieredCompilation -XX:TieredStopAtLevel=1"))
.timeout(Duration.seconds(20))
.logRetention(RetentionDays.ONE_WEEK)
.build());
exampleTable.grantWriteData(exampleWithoutTieredComp);
exampleTable.grantWriteData(exampleWithTieredComp);
HttpApi httpApi = new HttpApi(this, "ExampleApi", HttpApiProps.builder()
.apiName("ExampleApi")
.build());
httpApi.addRoutes(AddRoutesOptions.builder()
.path("/without")
.methods(singletonList(HttpMethod.GET))
.integration(new LambdaProxyIntegration(LambdaProxyIntegrationProps.builder()
.handler(exampleWithoutTieredComp)
.payloadFormatVersion(PayloadFormatVersion.VERSION_2_0)
.build()))
.build());
httpApi.addRoutes(AddRoutesOptions.builder()
.path("/with")
.methods(singletonList(HttpMethod.GET))
.integration(new LambdaProxyIntegration(LambdaProxyIntegrationProps.builder()
.handler(exampleWithTieredComp)
.payloadFormatVersion(PayloadFormatVersion.VERSION_2_0)
.build()))
.build());
new CfnOutput(this, "api-endpoint", CfnOutputProps.builder()
.value(httpApi.getApiEndpoint())
.build());
}
private Map<String, String> mapOf(String... keyValues) {
Map<String, String> map = new HashMap<>(keyValues.length/2);
for (int i = 0; i < keyValues.length; i++) {
if(i % 2 == 0) {
map.put(keyValues[i], keyValues[i + 1]);
}
}
return map;
}
}