diff --git a/java/custom-resource/README.md b/java/custom-resource/README.md index eede35a0f..91e943457 100644 --- a/java/custom-resource/README.md +++ b/java/custom-resource/README.md @@ -42,3 +42,13 @@ to run the CDK toolkit commands as usual (Maven will recompile as needed): $ cdk diff + +## Lambda +[`lambda`](./lambda) contains the source code for lambda handler. +After any code changes in the handler, follow these steps to deploy code changes. + + $ mvn package -f lambda/pom.xml + + + $ cp lambda/target/lambda-1.0.0-jar-with-dependencies.jar asset/ + diff --git a/java/custom-resource/asset/lambda-1.0.0-jar-with-dependencies.jar b/java/custom-resource/asset/lambda-1.0.0-jar-with-dependencies.jar new file mode 100644 index 000000000..9ebae0330 Binary files /dev/null and b/java/custom-resource/asset/lambda-1.0.0-jar-with-dependencies.jar differ diff --git a/java/custom-resource/cdk.json b/java/custom-resource/cdk.json index 1fcdaefc6..98074fd58 100644 --- a/java/custom-resource/cdk.json +++ b/java/custom-resource/cdk.json @@ -1,4 +1,3 @@ { - "app": "mvn exec:java -Dexec.mainClass=software.amazon.awscdk.examples.CustomResourceApp" - + "app": "mvn exec:java -pl cdk -Dexec.mainClass=software.amazon.awscdk.examples.CustomResourceApp" } diff --git a/java/custom-resource/cdk/cdk.json b/java/custom-resource/cdk/cdk.json new file mode 100644 index 000000000..0a247595d --- /dev/null +++ b/java/custom-resource/cdk/cdk.json @@ -0,0 +1,3 @@ +{ + "app": "mvn test" +} \ No newline at end of file diff --git a/java/custom-resource/cdk/cdk.out.dummy/dummy.template.json b/java/custom-resource/cdk/cdk.out.dummy/dummy.template.json new file mode 100644 index 000000000..2c63c0851 --- /dev/null +++ b/java/custom-resource/cdk/cdk.out.dummy/dummy.template.json @@ -0,0 +1,2 @@ +{ +} diff --git a/java/custom-resource/cdk/cdk.out.dummy/manifest.json b/java/custom-resource/cdk/cdk.out.dummy/manifest.json new file mode 100644 index 000000000..6ae56789f --- /dev/null +++ b/java/custom-resource/cdk/cdk.out.dummy/manifest.json @@ -0,0 +1,18 @@ +{ + "version": "1.16.0", + "artifacts": { + "Tree": { + "type": "cdk:tree", + "properties": { + "file": "tree.json" + } + }, + "dummy":{ + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "dummy.template.json" + } + } + } +} diff --git a/java/custom-resource/cdk/pom.xml b/java/custom-resource/cdk/pom.xml new file mode 100644 index 000000000..5d6e2d18b --- /dev/null +++ b/java/custom-resource/cdk/pom.xml @@ -0,0 +1,70 @@ + + + + + custom-resource + com.amazonaws.cdk + 1.0.0 + + 4.0.0 + + cdk + + + + + + org.apache.maven.plugins + maven-resources-plugin + + + copy-folder + test + + copy-resources + + + ${project.basedir}/cdk.out + + + false + ${project.basedir}/cdk.out.dummy + + + + + + + + + + + UTF-8 + + + + + + software.amazon.awscdk + aws-cdk-lib + [2.0.0,) + + + + software.constructs + constructs + [10.0.0,) + + + + + junit + junit + 4.13.1 + test + + + + + + \ No newline at end of file diff --git a/java/custom-resource/src/main/java/software/amazon/awscdk/examples/CustomResourceApp.java b/java/custom-resource/cdk/src/main/java/software/amazon/awscdk/examples/CustomResourceApp.java similarity index 100% rename from java/custom-resource/src/main/java/software/amazon/awscdk/examples/CustomResourceApp.java rename to java/custom-resource/cdk/src/main/java/software/amazon/awscdk/examples/CustomResourceApp.java diff --git a/java/custom-resource/src/main/java/software/amazon/awscdk/examples/CustomResourceStack.java b/java/custom-resource/cdk/src/main/java/software/amazon/awscdk/examples/CustomResourceStack.java similarity index 64% rename from java/custom-resource/src/main/java/software/amazon/awscdk/examples/CustomResourceStack.java rename to java/custom-resource/cdk/src/main/java/software/amazon/awscdk/examples/CustomResourceStack.java index 2ca2b6738..6655eeea0 100644 --- a/java/custom-resource/src/main/java/software/amazon/awscdk/examples/CustomResourceStack.java +++ b/java/custom-resource/cdk/src/main/java/software/amazon/awscdk/examples/CustomResourceStack.java @@ -1,22 +1,11 @@ package software.amazon.awscdk.examples; -import java.nio.file.*; - import java.util.Map; -import java.util.HashMap; import software.constructs.Construct; -import software.amazon.awscdk.CustomResource; -import software.amazon.awscdk.Duration; import software.amazon.awscdk.Stack; -import software.amazon.awscdk.customresources.*; import software.amazon.awscdk.CfnOutput; -import software.amazon.awscdk.services.logs.*; -import software.amazon.awscdk.services.lambda.Runtime; -import software.amazon.awscdk.services.lambda.InlineCode; -import software.amazon.awscdk.services.lambda.SingletonFunction; - public class CustomResourceStack extends Stack { public String response = ""; public CustomResourceStack(final Construct scope, final String id, final Map props) { diff --git a/java/custom-resource/src/main/java/software/amazon/awscdk/examples/MyCustomResource.java b/java/custom-resource/cdk/src/main/java/software/amazon/awscdk/examples/MyCustomResource.java similarity index 68% rename from java/custom-resource/src/main/java/software/amazon/awscdk/examples/MyCustomResource.java rename to java/custom-resource/cdk/src/main/java/software/amazon/awscdk/examples/MyCustomResource.java index d04c61ad8..fee4df671 100644 --- a/java/custom-resource/src/main/java/software/amazon/awscdk/examples/MyCustomResource.java +++ b/java/custom-resource/cdk/src/main/java/software/amazon/awscdk/examples/MyCustomResource.java @@ -3,32 +3,32 @@ import java.nio.file.*; import java.util.Map; +import java.util.UUID; import software.constructs.Construct; import software.amazon.awscdk.CustomResource; import software.amazon.awscdk.Duration; -import java.util.UUID; import software.amazon.awscdk.customresources.*; import software.amazon.awscdk.services.logs.*; import software.amazon.awscdk.services.lambda.Runtime; -import software.amazon.awscdk.services.lambda.InlineCode; +import software.amazon.awscdk.services.lambda.Code; import software.amazon.awscdk.services.lambda.SingletonFunction; public class MyCustomResource extends Construct { public String response = ""; + public MyCustomResource(final Construct scope, final String id, final Map props) { super(scope, id); - try { final SingletonFunction onEvent = SingletonFunction.Builder.create(this, "Singleton") - .code(InlineCode.fromAsset("lambda")) - .handler("custom-resource-handler.on_event") - .runtime(Runtime.PYTHON_3_8) .uuid(UUID.randomUUID().toString()) - .timeout(Duration.minutes(1)) + .code(Code.fromAsset("./asset/lambda-1.0.0-jar-with-dependencies.jar")) + .handler("software.amazon.awscdk.examples.CustomResourceHandler") + .runtime(Runtime.JAVA_21).memorySize(1024) + .timeout(Duration.minutes(5)) .build(); final Provider myProvider = Provider.Builder.create(this, "MyProvider") @@ -41,19 +41,10 @@ public MyCustomResource(final Construct scope, final String id, final Map + + + + custom-resource + com.amazonaws.cdk + 1.0.0 + + 4.0.0 + lambda + + + UTF-8 + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.2 + + false + + + + package + + shade + + + + + + + org.apache.maven.plugins + maven-resources-plugin + + + copy-folder + test + + copy-resources + + + ${project.basedir}/cdk.out + + + false + ${project.basedir}/cdk.out.dummy + + + + + + + + + + + + + com.amazonaws + aws-lambda-java-core + 1.2.1 + + + com.amazonaws + aws-lambda-java-events + 3.10.0 + + + + + + software.amazon.awssdk + bom + 2.10.24 + pom + import + + + + + + \ No newline at end of file diff --git a/java/custom-resource/lambda/src/main/java/software/amazon/awscdk/examples/CustomResourceHandler.java b/java/custom-resource/lambda/src/main/java/software/amazon/awscdk/examples/CustomResourceHandler.java new file mode 100644 index 000000000..a3ce8b368 --- /dev/null +++ b/java/custom-resource/lambda/src/main/java/software/amazon/awscdk/examples/CustomResourceHandler.java @@ -0,0 +1,27 @@ +package software.amazon.awscdk.examples; + +import java.util.HashMap; +import java.util.Map; + +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; + +public class CustomResourceHandler implements RequestHandler, Map> { + @Override + public Map handleRequest(Map event, Context context) { + try { + Map props = (Map) event.get("ResourceProperties"); + String message = (String) props.get("Message"); + + Map attributes = new HashMap(); + attributes.put("Response", String.format("Resource message %s", message)); + + Map result = new HashMap(); + result.put("Data", attributes); + + return result; + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} diff --git a/java/custom-resource/pom.xml b/java/custom-resource/pom.xml index 7425d4e01..f071e6579 100644 --- a/java/custom-resource/pom.xml +++ b/java/custom-resource/pom.xml @@ -1,75 +1,59 @@ - - 4.0.0 - com.amazonaws.cdk - custom-resource - 1.0.0 + + 4.0.0 - - UTF-8 - + com.amazonaws.cdk + custom-resource + pom + 1.0.0 - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.8.1 - - 1.8 - 1.8 - - + + lambda + cdk + - - org.apache.maven.plugins - maven-assembly-plugin - 3.1.0 - - - jar-with-dependencies - - - - com.amazonaws.cdk.examples.CustomResourceApp - - - - - - make-assembly - package - - single - - - - - - + + UTF-8 + - - - - software.amazon.awscdk - aws-cdk-lib - [2.0.0,) - + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + + - - software.constructs - constructs - [10.0.0,) - + + org.apache.maven.plugins + maven-assembly-plugin + 3.1.0 + + + jar-with-dependencies + + + + com.amazonaws.cdk.software.amazon.awscdk.examples.S3LambdaApp + + + + + + make-assembly + package + + single + + + + - - - junit - junit - 4.13.1 - test - - - + + + \ No newline at end of file