Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use java for custom resource handler in custom-resource #1030

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions java/custom-resource/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,13 @@ to run the CDK toolkit commands as usual (Maven will recompile as needed):

$ cdk diff
<diff against deployed stack>

## 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
<generates jar with lambda handler code>

$ cp lambda/target/lambda-1.0.0-jar-with-dependencies.jar asset/
<copies lambda handler jar to asset dir>
Binary file not shown.
3 changes: 1 addition & 2 deletions java/custom-resource/cdk.json
Original file line number Diff line number Diff line change
@@ -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"
}
3 changes: 3 additions & 0 deletions java/custom-resource/cdk/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"app": "mvn test"
}
2 changes: 2 additions & 0 deletions java/custom-resource/cdk/cdk.out.dummy/dummy.template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
18 changes: 18 additions & 0 deletions java/custom-resource/cdk/cdk.out.dummy/manifest.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
70 changes: 70 additions & 0 deletions java/custom-resource/cdk/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>

<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">
<parent>
<artifactId>custom-resource</artifactId>
<groupId>com.amazonaws.cdk</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>cdk</artifactId>

<build>
<plugins>
<plugin>

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-folder</id>
<phase>test</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/cdk.out</outputDirectory>
<resources>
<resource>
<filtering>false</filtering>
<directory>${project.basedir}/cdk.out.dummy</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>


<dependencies>
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>aws-cdk-lib</artifactId>
<version>[2.0.0,)</version>
</dependency>

<dependency>
<groupId>software.constructs</groupId>
<artifactId>constructs</artifactId>
<version>[10.0.0,)</version>
</dependency>

<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>

</dependencies>


</project>
Original file line number Diff line number Diff line change
@@ -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<String, ? extends Object> props) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, ? extends Object> 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")
Expand All @@ -41,19 +41,10 @@ public MyCustomResource(final Construct scope, final String id, final Map<String
.properties(props)
.build();

response = resource.getAtt("Response").toString();
response = resource.getAttString("Response");

} catch (Exception e) {
e.printStackTrace();
}
}
// function to read the file content
public static String readFileAsString(String fileName) throws Exception {
try {
return new String(Files.readAllBytes(Paths.get(fileName)), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}
3 changes: 3 additions & 0 deletions java/custom-resource/lambda/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"app": "mvn test"
}
2 changes: 2 additions & 0 deletions java/custom-resource/lambda/cdk.out.dummy/dummy.template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
18 changes: 18 additions & 0 deletions java/custom-resource/lambda/cdk.out.dummy/manifest.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
42 changes: 0 additions & 42 deletions java/custom-resource/lambda/custom-resource-handler.py

This file was deleted.

86 changes: 86 additions & 0 deletions java/custom-resource/lambda/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>

<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">
<parent>
<artifactId>custom-resource</artifactId>
<groupId>com.amazonaws.cdk</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lambda</artifactId>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-folder</id>
<phase>test</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/cdk.out</outputDirectory>
<resources>
<resource>
<filtering>false</filtering>
<directory>${project.basedir}/cdk.out.dummy</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>

<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>3.10.0</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.10.24</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>


</project>
Original file line number Diff line number Diff line change
@@ -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<String, Object>, Map<String, Object>> {
@Override
public Map<String, Object> handleRequest(Map<String, Object> event, Context context) {
try {
Map<String, Object> props = (Map<String, Object>) event.get("ResourceProperties");
String message = (String) props.get("Message");

Map<String, String> attributes = new HashMap<String, String>();
attributes.put("Response", String.format("Resource message %s", message));

Map<String, Object> result = new HashMap<String, Object>();
result.put("Data", attributes);

return result;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Loading
Loading