|
| 1 | +package example; |
| 2 | + |
| 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.Code; |
| 22 | +import software.amazon.awscdk.services.lambda.Function; |
| 23 | +import software.amazon.awscdk.services.lambda.FunctionProps; |
| 24 | +import software.amazon.awscdk.services.lambda.LayerVersion; |
| 25 | +import software.amazon.awscdk.services.lambda.LayerVersionProps; |
| 26 | +import software.amazon.awscdk.services.lambda.Runtime; |
| 27 | +import software.amazon.awscdk.services.logs.RetentionDays; |
| 28 | + |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.HashMap; |
| 31 | +import java.util.Map; |
| 32 | + |
| 33 | +import static java.util.Collections.singletonList; |
| 34 | + |
| 35 | +public class InfrastructureStack extends Stack { |
| 36 | + public InfrastructureStack(final Construct scope, final String id) { |
| 37 | + this(scope, id, null); |
| 38 | + } |
| 39 | + |
| 40 | + public InfrastructureStack(final Construct scope, final String id, final StackProps props) { |
| 41 | + super(scope, id, props); |
| 42 | + |
| 43 | + Table exampleTable = new Table(this, "ExampleTable", TableProps.builder() |
| 44 | + .partitionKey(Attribute.builder() |
| 45 | + .type(AttributeType.STRING) |
| 46 | + .name("id").build()) |
| 47 | + .billingMode(BillingMode.PAY_PER_REQUEST) |
| 48 | + .build()); |
| 49 | + |
| 50 | + LayerVersion java17layer = new LayerVersion(this, "Java17Layer", LayerVersionProps.builder() |
| 51 | + .layerVersionName("Java17Layer") |
| 52 | + .description("Java 17") |
| 53 | + .compatibleRuntimes(Arrays.asList(Runtime.PROVIDED_AL2)) |
| 54 | + .code(Code.fromAsset("../../java17layer.zip")) |
| 55 | + .build()); |
| 56 | + |
| 57 | + Function exampleWithLayer = new Function(this, "ExampleWithLayer", FunctionProps.builder() |
| 58 | + .functionName("example-with-layer") |
| 59 | + .description("example-with-layer") |
| 60 | + .handler("example.ExampleDynamoDbHandler::handleRequest") |
| 61 | + .runtime(Runtime.PROVIDED_AL2) |
| 62 | + .code(Code.fromAsset("../software/ExampleFunction/target/example.jar")) |
| 63 | + .memorySize(512) |
| 64 | + .environment(mapOf("TABLE_NAME", exampleTable.getTableName())) |
| 65 | + .timeout(Duration.seconds(20)) |
| 66 | + .logRetention(RetentionDays.ONE_WEEK) |
| 67 | + .layers(singletonList(java17layer)) |
| 68 | + .build()); |
| 69 | + |
| 70 | + exampleTable.grantWriteData(exampleWithLayer); |
| 71 | + |
| 72 | + HttpApi httpApi = new HttpApi(this, "ExampleApi", HttpApiProps.builder() |
| 73 | + .apiName("ExampleApi") |
| 74 | + .build()); |
| 75 | + |
| 76 | + httpApi.addRoutes(AddRoutesOptions.builder() |
| 77 | + .path("/with") |
| 78 | + .methods(singletonList(HttpMethod.GET)) |
| 79 | + .integration(new LambdaProxyIntegration(LambdaProxyIntegrationProps.builder() |
| 80 | + .handler(exampleWithLayer) |
| 81 | + .payloadFormatVersion(PayloadFormatVersion.VERSION_2_0) |
| 82 | + .build())) |
| 83 | + .build()); |
| 84 | + |
| 85 | + new CfnOutput(this, "api-endpoint", CfnOutputProps.builder() |
| 86 | + .value(httpApi.getApiEndpoint()) |
| 87 | + .build()); |
| 88 | + } |
| 89 | + |
| 90 | + private Map<String, String> mapOf(String... keyValues) { |
| 91 | + Map<String, String> map = new HashMap<>(keyValues.length/2); |
| 92 | + for (int i = 0; i < keyValues.length; i++) { |
| 93 | + if(i % 2 == 0) { |
| 94 | + map.put(keyValues[i], keyValues[i + 1]); |
| 95 | + } |
| 96 | + } |
| 97 | + return map; |
| 98 | + } |
| 99 | +} |
0 commit comments