Skip to content

Commit 9f169d4

Browse files
committed
Use java for custom resource handler in custom-resource
1 parent 9ec514e commit 9f169d4

File tree

13 files changed

+259
-139
lines changed

13 files changed

+259
-139
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+
}

java/custom-resource/cdk/pom.xml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
17+
<groupId>org.apache.maven.plugins</groupId>
18+
<artifactId>maven-resources-plugin</artifactId>
19+
<executions>
20+
<execution>
21+
<id>copy-folder</id>
22+
<phase>test</phase>
23+
<goals>
24+
<goal>copy-resources</goal>
25+
</goals>
26+
<configuration>
27+
<outputDirectory>${project.basedir}/cdk.out</outputDirectory>
28+
<resources>
29+
<resource>
30+
<filtering>false</filtering>
31+
<directory>${project.basedir}/cdk.out.dummy</directory>
32+
</resource>
33+
</resources>
34+
</configuration>
35+
</execution>
36+
</executions>
37+
</plugin>
38+
</plugins>
39+
</build>
40+
41+
<properties>
42+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
43+
</properties>
44+
45+
46+
<dependencies>
47+
<dependency>
48+
<groupId>software.amazon.awscdk</groupId>
49+
<artifactId>aws-cdk-lib</artifactId>
50+
<version>[2.0.0,)</version>
51+
</dependency>
52+
53+
<dependency>
54+
<groupId>software.constructs</groupId>
55+
<artifactId>constructs</artifactId>
56+
<version>[10.0.0,)</version>
57+
</dependency>
58+
59+
<!-- https://mvnrepository.com/artifact/junit/junit -->
60+
<dependency>
61+
<groupId>junit</groupId>
62+
<artifactId>junit</artifactId>
63+
<version>4.13.1</version>
64+
<scope>test</scope>
65+
</dependency>
66+
67+
</dependencies>
68+
69+
70+
</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) {

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

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,32 @@
33
import java.nio.file.*;
44

55
import java.util.Map;
6+
import java.util.UUID;
67

78
import software.constructs.Construct;
89
import software.amazon.awscdk.CustomResource;
910
import software.amazon.awscdk.Duration;
10-
import java.util.UUID;
1111
import software.amazon.awscdk.customresources.*;
1212

1313
import software.amazon.awscdk.services.logs.*;
1414
import software.amazon.awscdk.services.lambda.Runtime;
15-
import software.amazon.awscdk.services.lambda.InlineCode;
15+
import software.amazon.awscdk.services.lambda.Code;
1616
import software.amazon.awscdk.services.lambda.SingletonFunction;
1717

1818
public class MyCustomResource extends Construct {
1919
public String response = "";
20+
2021
public MyCustomResource(final Construct scope, final String id, final Map<String, ? extends Object> props) {
2122
super(scope, id);
2223

23-
2424
try {
2525

2626
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)
3027
.uuid(UUID.randomUUID().toString())
31-
.timeout(Duration.minutes(1))
28+
.code(Code.fromAsset("./asset/lambda-1.0.0-jar-with-dependencies.jar"))
29+
.handler("software.amazon.awscdk.examples.CustomResourceHandler")
30+
.runtime(Runtime.JAVA_21).memorySize(1024)
31+
.timeout(Duration.minutes(5))
3232
.build();
3333

3434
final Provider myProvider = Provider.Builder.create(this, "MyProvider")
@@ -41,19 +41,10 @@ public MyCustomResource(final Construct scope, final String id, final Map<String
4141
.properties(props)
4242
.build();
4343

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

4646
} catch (Exception e) {
4747
e.printStackTrace();
4848
}
4949
}
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-
}
5950
}

java/custom-resource/lambda/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+
}

java/custom-resource/lambda/custom-resource-handler.py

Lines changed: 0 additions & 42 deletions
This file was deleted.

java/custom-resource/lambda/pom.xml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
<artifactId>lambda</artifactId>
11+
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
</properties>
15+
16+
<build>
17+
<plugins>
18+
<plugin>
19+
<groupId>org.apache.maven.plugins</groupId>
20+
<artifactId>maven-shade-plugin</artifactId>
21+
<version>3.2.2</version>
22+
<configuration>
23+
<createDependencyReducedPom>false</createDependencyReducedPom>
24+
</configuration>
25+
<executions>
26+
<execution>
27+
<phase>package</phase>
28+
<goals>
29+
<goal>shade</goal>
30+
</goals>
31+
</execution>
32+
</executions>
33+
</plugin>
34+
<plugin>
35+
36+
<groupId>org.apache.maven.plugins</groupId>
37+
<artifactId>maven-resources-plugin</artifactId>
38+
<executions>
39+
<execution>
40+
<id>copy-folder</id>
41+
<phase>test</phase>
42+
<goals>
43+
<goal>copy-resources</goal>
44+
</goals>
45+
<configuration>
46+
<outputDirectory>${project.basedir}/cdk.out</outputDirectory>
47+
<resources>
48+
<resource>
49+
<filtering>false</filtering>
50+
<directory>${project.basedir}/cdk.out.dummy</directory>
51+
</resource>
52+
</resources>
53+
</configuration>
54+
</execution>
55+
</executions>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
60+
<dependencies>
61+
62+
<dependency>
63+
<groupId>com.amazonaws</groupId>
64+
<artifactId>aws-lambda-java-core</artifactId>
65+
<version>1.2.1</version>
66+
</dependency>
67+
<dependency>
68+
<groupId>com.amazonaws</groupId>
69+
<artifactId>aws-lambda-java-events</artifactId>
70+
<version>3.10.0</version>
71+
</dependency>
72+
</dependencies>
73+
<dependencyManagement>
74+
<dependencies>
75+
<dependency>
76+
<groupId>software.amazon.awssdk</groupId>
77+
<artifactId>bom</artifactId>
78+
<version>2.10.24</version>
79+
<type>pom</type>
80+
<scope>import</scope>
81+
</dependency>
82+
</dependencies>
83+
</dependencyManagement>
84+
85+
86+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package software.amazon.awscdk.examples;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import com.amazonaws.services.lambda.runtime.Context;
7+
import com.amazonaws.services.lambda.runtime.RequestHandler;
8+
9+
public class CustomResourceHandler implements RequestHandler<Map<String, Object>, Map<String, Object>> {
10+
@Override
11+
public Map<String, Object> handleRequest(Map<String, Object> event, Context context) {
12+
try {
13+
Map<String, Object> props = (Map<String, Object>) event.get("ResourceProperties");
14+
String message = (String) props.get("Message");
15+
16+
Map<String, String> attributes = new HashMap<String, String>();
17+
attributes.put("Response", String.format("Resource message %s", message));
18+
19+
Map<String, Object> result = new HashMap<String, Object>();
20+
result.put("Data", attributes);
21+
22+
return result;
23+
} catch (Exception e) {
24+
throw new RuntimeException(e);
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)