|
| 1 | +package com.myorg; |
| 2 | + |
| 3 | +import software.amazon.awscdk.Stack; |
| 4 | +import software.amazon.awscdk.*; |
| 5 | +import software.amazon.awscdk.services.iam.Effect; |
| 6 | +import software.amazon.awscdk.services.iam.PolicyStatement; |
| 7 | +import software.amazon.awscdk.services.iam.PolicyStatementProps; |
| 8 | +import software.amazon.awscdk.services.lambda.Code; |
| 9 | +import software.amazon.awscdk.services.lambda.Function; |
| 10 | +import software.amazon.awscdk.services.lambda.FunctionProps; |
| 11 | +import software.amazon.awscdk.services.lambda.Runtime; |
| 12 | +import software.amazon.awscdk.services.s3.assets.AssetOptions; |
| 13 | +import software.constructs.Construct; |
| 14 | + |
| 15 | +import java.io.Serializable; |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.Collections; |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | + |
| 23 | +import static java.util.Collections.singletonList; |
| 24 | +import static software.amazon.awscdk.BundlingOutput.NOT_ARCHIVED; |
| 25 | + |
| 26 | +public class PowertoolsExamplesCloudformationCdkStack extends Stack { |
| 27 | + |
| 28 | + public static final String SAMPLE_BUCKET_NAME = "sample-bucket-name-20230315-abc123"; |
| 29 | + |
| 30 | + public PowertoolsExamplesCloudformationCdkStack(final Construct scope, final String id) { |
| 31 | + this(scope, id, null); |
| 32 | + } |
| 33 | + |
| 34 | + public PowertoolsExamplesCloudformationCdkStack(final Construct scope, final String id, final StackProps props) { |
| 35 | + super(scope, id, props); |
| 36 | + |
| 37 | + |
| 38 | + List<String> functionPackagingInstructions = Arrays.asList( |
| 39 | + "/bin/sh", |
| 40 | + "-c", |
| 41 | + "mvn clean install" + |
| 42 | + "&& mkdir /asset-output/lib" + |
| 43 | + "&& cp target/powertools-examples-cloudformation-*.jar /asset-output/lib" |
| 44 | + ); |
| 45 | + BundlingOptions bundlingOptions = BundlingOptions.builder() |
| 46 | + .command(functionPackagingInstructions) |
| 47 | + .image(Runtime.JAVA_11.getBundlingImage()) |
| 48 | + .volumes(singletonList( |
| 49 | + // Mount local .m2 repo to avoid download all the dependencies again inside the container |
| 50 | + DockerVolume.builder() |
| 51 | + .hostPath(System.getProperty("user.home") + "/.m2/") |
| 52 | + .containerPath("/root/.m2/") |
| 53 | + .build() |
| 54 | + )) |
| 55 | + .user("root") |
| 56 | + .outputType(NOT_ARCHIVED) |
| 57 | + .build(); |
| 58 | + |
| 59 | + Function helloWorldFunction = new Function(this, "HelloWorldFunction", FunctionProps.builder() |
| 60 | + .runtime(Runtime.JAVA_11) |
| 61 | + .code(Code.fromAsset("../../", AssetOptions.builder().bundling(bundlingOptions) |
| 62 | + .build())) |
| 63 | + .handler("helloworld.App::handleRequest") |
| 64 | + .memorySize(512) |
| 65 | + .timeout(Duration.seconds(20)) |
| 66 | + .environment(Collections |
| 67 | + .singletonMap("JAVA_TOOL_OPTIONS", "-XX:+TieredCompilation -XX:TieredStopAtLevel=1")) |
| 68 | + .build()); |
| 69 | + helloWorldFunction.addToRolePolicy(new PolicyStatement(PolicyStatementProps.builder() |
| 70 | + .effect(Effect.ALLOW) |
| 71 | + .actions(Arrays.asList("s3:GetLifecycleConfiguration", |
| 72 | + "s3:PutLifecycleConfiguration", |
| 73 | + "s3:CreateBucket", |
| 74 | + "s3:ListBucket", |
| 75 | + "s3:DeleteBucket")) |
| 76 | + .resources(singletonList("*")).build())); |
| 77 | + |
| 78 | + String bucketName = (String) this.getNode().tryGetContext("BucketNameParam"); |
| 79 | + |
| 80 | + Map<String, Serializable> crProperties = new HashMap<>(); |
| 81 | + crProperties.put("BucketName", bucketName); |
| 82 | + CustomResource.Builder |
| 83 | + .create(this, "HelloWorldCustomResource") |
| 84 | + .serviceToken(helloWorldFunction.getFunctionArn()) |
| 85 | + .properties(crProperties) |
| 86 | + .build(); |
| 87 | + |
| 88 | + } |
| 89 | +} |
0 commit comments