Skip to content

Commit bff5197

Browse files
committed
Use java for custom resource handler in custom-resource
1 parent 61b06b3 commit bff5197

File tree

18 files changed

+346
-142
lines changed

18 files changed

+346
-142
lines changed

java/custom-resource/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,13 @@ to run the CDK toolkit commands as usual (Maven will recompile as needed):
4242

4343
$ cdk diff
4444
<diff against deployed stack>
45+
46+
## Lambda
47+
[`lambda`](./lambda) contains the source code for lambda handler.
48+
After any code changes in the handler, follow these steps to deploy code changes.
49+
50+
$ mvn package -f lambda/pom.xml
51+
<generates jar with lambda handler code>
52+
53+
$ cp lambda/target/lambda-1.0.0-jar-with-dependencies.jar asset/
54+
<copies lambda handler jar to asset dir>
Binary file not shown.

java/custom-resource/cdk.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
{
2-
"app": "mvn exec:java -Dexec.mainClass=software.amazon.awscdk.examples.CustomResourceApp"
3-
2+
"app": "mvn exec:java -pl cdk -Dexec.mainClass=software.amazon.awscdk.examples.CustomResourceApp"
43
}

java/custom-resource/cdk/cdk.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"app": "mvn test"
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"version": "1.16.0",
3+
"artifacts": {
4+
"Tree": {
5+
"type": "cdk:tree",
6+
"properties": {
7+
"file": "tree.json"
8+
}
9+
},
10+
"dummy": {
11+
"type": "aws:cloudformation:stack",
12+
"environment": "aws://unknown-account/unknown-region",
13+
"properties": {
14+
"templateFile": "dummy.template.json"
15+
}
16+
}
17+
}
18+
}

java/custom-resource/cdk/pom.xml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<parent>
5+
<artifactId>custom-resource</artifactId>
6+
<groupId>com.amazonaws.cdk</groupId>
7+
<version>1.0.0</version>
8+
</parent>
9+
<modelVersion>4.0.0</modelVersion>
10+
11+
<artifactId>cdk</artifactId>
12+
13+
<build>
14+
<plugins>
15+
<plugin>
16+
<groupId>org.apache.maven.plugins</groupId>
17+
<artifactId>maven-resources-plugin</artifactId>
18+
<executions>
19+
<execution>
20+
<id>copy-folder</id>
21+
<phase>test</phase>
22+
<goals>
23+
<goal>copy-resources</goal>
24+
</goals>
25+
<configuration>
26+
<outputDirectory>${project.basedir}/cdk.out</outputDirectory>
27+
<resources>
28+
<resource>
29+
<filtering>false</filtering>
30+
<directory>${project.basedir}/cdk.out.dummy</directory>
31+
</resource>
32+
</resources>
33+
</configuration>
34+
</execution>
35+
</executions>
36+
</plugin>
37+
</plugins>
38+
</build>
39+
40+
<properties>
41+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
42+
</properties>
43+
44+
<dependencies>
45+
<dependency>
46+
<groupId>software.amazon.awscdk</groupId>
47+
<artifactId>aws-cdk-lib</artifactId>
48+
<version>[2.0.0,)</version>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>software.constructs</groupId>
53+
<artifactId>constructs</artifactId>
54+
<version>[10.0.0,)</version>
55+
</dependency>
56+
57+
<!-- https://mvnrepository.com/artifact/junit/junit -->
58+
<dependency>
59+
<groupId>junit</groupId>
60+
<artifactId>junit</artifactId>
61+
<version>4.13.1</version>
62+
<scope>test</scope>
63+
</dependency>
64+
65+
</dependencies>
66+
</project>

java/custom-resource/src/main/java/software/amazon/awscdk/examples/CustomResourceStack.java renamed to java/custom-resource/cdk/src/main/java/software/amazon/awscdk/examples/CustomResourceStack.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
11
package software.amazon.awscdk.examples;
22

3-
import java.nio.file.*;
4-
53
import java.util.Map;
6-
import java.util.HashMap;
74

85
import software.constructs.Construct;
9-
import software.amazon.awscdk.CustomResource;
10-
import software.amazon.awscdk.Duration;
116
import software.amazon.awscdk.Stack;
12-
import software.amazon.awscdk.customresources.*;
137
import software.amazon.awscdk.CfnOutput;
148

15-
import software.amazon.awscdk.services.logs.*;
16-
import software.amazon.awscdk.services.lambda.Runtime;
17-
import software.amazon.awscdk.services.lambda.InlineCode;
18-
import software.amazon.awscdk.services.lambda.SingletonFunction;
19-
209
public class CustomResourceStack extends Stack {
2110
public String response = "";
2211
public CustomResourceStack(final Construct scope, final String id, final Map<String, ? extends Object> props) {
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
11
package software.amazon.awscdk.examples;
22

3-
import java.nio.file.*;
4-
53
import java.util.Map;
4+
import java.util.UUID;
65

76
import software.constructs.Construct;
87
import software.amazon.awscdk.CustomResource;
98
import software.amazon.awscdk.Duration;
10-
import java.util.UUID;
119
import software.amazon.awscdk.customresources.*;
1210

1311
import software.amazon.awscdk.services.logs.*;
1412
import software.amazon.awscdk.services.lambda.Runtime;
15-
import software.amazon.awscdk.services.lambda.InlineCode;
13+
import software.amazon.awscdk.services.lambda.Code;
1614
import software.amazon.awscdk.services.lambda.SingletonFunction;
1715

1816
public class MyCustomResource extends Construct {
1917
public String response = "";
18+
2019
public MyCustomResource(final Construct scope, final String id, final Map<String, ? extends Object> props) {
2120
super(scope, id);
2221

23-
2422
try {
23+
final SingletonFunction onEvent = SingletonFunction.Builder.create(this, "Singleton1")
24+
.uuid(UUID.randomUUID().toString())
25+
.code(Code.fromAsset("./asset/lambda-1.0.0-jar-with-dependencies.jar"))
26+
.handler("software.amazon.awscdk.examples.CustomResourceHandler")
27+
.runtime(Runtime.JAVA_21).memorySize(1024)
28+
.timeout(Duration.minutes(5))
29+
.build();
2530

26-
final SingletonFunction onEvent = SingletonFunction.Builder.create(this, "Singleton")
27-
.code(InlineCode.fromAsset("lambda"))
28-
.handler("custom-resource-handler.on_event")
29-
.runtime(Runtime.PYTHON_3_8)
31+
final SingletonFunction isComplete = SingletonFunction.Builder.create(this, "Singleton2")
3032
.uuid(UUID.randomUUID().toString())
31-
.timeout(Duration.minutes(1))
33+
.code(Code.fromAsset("./asset/lambda-1.0.0-jar-with-dependencies.jar"))
34+
.handler("software.amazon.awscdk.examples.CustomResourceIsCompleteHandler")
35+
.runtime(Runtime.JAVA_21).memorySize(1024)
36+
.timeout(Duration.minutes(5))
3237
.build();
3338

3439
final Provider myProvider = Provider.Builder.create(this, "MyProvider")
3540
.onEventHandler(onEvent)
41+
.isCompleteHandler(isComplete)
3642
.logRetention(RetentionDays.ONE_DAY)
3743
.build();
3844

@@ -41,19 +47,10 @@ public MyCustomResource(final Construct scope, final String id, final Map<String
4147
.properties(props)
4248
.build();
4349

44-
response = resource.getAtt("Response").toString();
50+
response = resource.getAttString("Response");
4551

4652
} catch (Exception e) {
4753
e.printStackTrace();
4854
}
4955
}
50-
// function to read the file content
51-
public static String readFileAsString(String fileName) throws Exception {
52-
try {
53-
return new String(Files.readAllBytes(Paths.get(fileName)), "UTF-8");
54-
} catch (Exception e) {
55-
e.printStackTrace();
56-
throw e;
57-
}
58-
}
5956
}

0 commit comments

Comments
 (0)