Skip to content

Commit 4fb1c2a

Browse files
AlexeySoshinscottgerringjeromevdl
committed
docs: Adding CDK example (#1321)
* Move the current example, which is for SAM, under /sam directory * Create a new directory for CDK * Add CDK Example as a Maven module * CDK Stack stub * Restructure CDK application info Infra and App projects * Define the build of the project inside the CDK stack * Use default account and region * Add example of setting environment variables in CDK * Remove threads from the examples, as this should be covered by documentation * Add general README for the project * Add specific README for SAM * Add specific README for CDK * Use Java11 syntax for CDK * Refactor the code for clarity * Fix imports * Add outputs example * Add test for the stack * Update examples/powertools-examples-core/README.md Co-authored-by: Jérôme Van Der Linden <[email protected]> * Update examples/powertools-examples-core/cdk/README.md Co-authored-by: Jérôme Van Der Linden <[email protected]> * Remove unnecessary .gitignore * Mixed log level and sample rate * Combine .gitignore files * Remove `cdk ls`, since there's just a single stack * Remove SAM mentions from the CDK readme * Replace "architecture" with "tool" while talking about SAM/CDK * Update examples/powertools-examples-core/README.md Co-authored-by: Jérôme Van Der Linden <[email protected]> * Update examples/powertools-examples-core/README.md Co-authored-by: Jérôme Van Der Linden <[email protected]> * Reformat imports * Don't include version number in the HelloWorld jar * Update examples/powertools-examples-core/cdk/infra/src/main/java/cdk/CdkStack.java Co-authored-by: Jérôme Van Der Linden <[email protected]> * Add Streaming example as well * Update examples/powertools-examples-core/README.md Co-authored-by: Scott Gerring <[email protected]> * Update examples/.gitignore Co-authored-by: Scott Gerring <[email protected]> * Update examples/.gitignore Co-authored-by: Scott Gerring <[email protected]> * Update examples/powertools-examples-core/README.md Co-authored-by: Scott Gerring <[email protected]> * Update examples/powertools-examples-core/cdk/README.md Co-authored-by: Scott Gerring <[email protected]> * Update examples/powertools-examples-core/sam/README.md Co-authored-by: Scott Gerring <[email protected]> * Exclude examples from duplicate code scan https://github.com/aws-powertools/powertools-lambda-java/pull/1317/files * Fix broken link to the events file * Trim cdk.json * Trim cdk.json * Downgrade code to Java 8 * Correct README to point to powertools-core-idempotency for a quick start * Add the missing license to the new Java files * Reformat code --------- Co-authored-by: Scott Gerring <[email protected]> Co-authored-by: Jérôme Van Der Linden <[email protected]>
1 parent 9e29079 commit 4fb1c2a

File tree

22 files changed

+897
-7
lines changed

22 files changed

+897
-7
lines changed

examples/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ Each example can be copied from its subdirectory and used independently of the r
55

66
## Examples
77

8-
* [powertools-examples-core-utilities](powertools-examples-core-utilities) - Demonstrates the core logging, tracing, and metrics modules with different build tools
9-
* [SAM](powertools-examples-core-utilities/sam)
10-
* [CDK](powertools-examples-core-utilities/cdk)
8+
* [powertools-examples-core](powertools-examples-core) - Demonstrates the core logging, tracing, and metrics modules with different build tools
9+
* [SAM](./powertools-examples-core/sam)
10+
* [CDK](./powertools-examples-core/cdk)
1111
* [powertools-examples-idempotency](powertools-examples-idempotency) - An idempotent HTTP API
1212
* [powertools-examples-parameters](powertools-examples-parameters) - Uses the parameters module to provide runtime parameters to a function
1313
* [powertools-examples-serialization](powertools-examples-serialization) - Uses the serialization module to serialize and deserialize API Gateway & SQS payloads
@@ -54,7 +54,7 @@ The first command will build the source of your application. The second command
5454

5555
You can find your API Gateway Endpoint URL in the output values displayed after deployment.
5656

57-
If you're not using SAM, you can look for examples for other tools under [powertools-examples-core-utilities](./powertools-examples-core-utilities)
57+
If you're not using SAM, you can look for examples for other tools under [powertools-examples-core](./powertools-examples-core)
5858

5959
### External examples
6060

examples/pom.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
</description>
3030

3131
<modules>
32-
<module>powertools-examples-core-utilities/sam</module>
33-
<module>powertools-examples-core-utilities/cdk/app</module>
34-
<module>powertools-examples-core-utilities/cdk/infra</module>
32+
<module>powertools-examples-core/sam</module>
33+
<module>powertools-examples-core/cdk/app</module>
34+
<module>powertools-examples-core/cdk/infra</module>
3535
<module>powertools-examples-idempotency</module>
3636
<module>powertools-examples-parameters</module>
3737
<module>powertools-examples-serialization</module>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Powertools for AWS Lambda (Java) - Core Utilities Example with CDK
2+
3+
This project demonstrates the Lambda for Powertools Java module deployed using [Cloud Development Kit](https://aws.amazon.com/cdk/).
4+
5+
For general information on the deployed example itself, you can refer to the parent [README](../README.md)
6+
7+
## Configuration
8+
CDK uses the following project structure:
9+
- [app](./app) - stores the source code of your application, which is similar between all examples
10+
- [infra](./infra) - stores the definition of your infrastructure
11+
- [cdk.json](./infra/cdk.json) - tells the CDK Toolkit how to execute your app
12+
- [CdkApp](./infra/src/main/java/cdk/CdkApp.java) - bootstraps your stack, taking AWS `account` and `region` as input
13+
- [CdkStack](./infra/src/main/java/cdk/CdkStack.java) - defines the Lambda function to be deployed as well as API Gateway for it.
14+
15+
It is a [Maven](https://maven.apache.org/) based project, so you can open this project with any Maven compatible Java IDE to build and run tests.
16+
17+
18+
## Deploy the sample application
19+
20+
The minimum to deploy the app should be
21+
```bash
22+
cdk deploy
23+
```
24+
25+
If you're running CDK for the first time, you'll need to first run the bootstrap command:
26+
```bash
27+
cdk bootstrap
28+
```
29+
30+
## Useful commands
31+
32+
* `mvn package` compile and run tests
33+
* `cdk synth` emits the synthesized CloudFormation template
34+
* `cdk deploy` deploy this stack to your default AWS account/region
35+
* `cdk diff` compare deployed stack with current state
36+
* `cdk docs` open CDK documentation
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>software.amazon.lambda.examples</groupId>
6+
<version>1.16.1</version>
7+
<artifactId>powertools-examples-core-cdk</artifactId>
8+
<packaging>jar</packaging>
9+
10+
<name>Powertools for AWS Lambda (Java) library Examples - Core</name>
11+
12+
<properties>
13+
<log4j.version>2.20.0</log4j.version>
14+
<maven.compiler.source>1.8</maven.compiler.source>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
<maven.deploy.skip>true</maven.deploy.skip>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>software.amazon.lambda</groupId>
22+
<artifactId>powertools-tracing</artifactId>
23+
<version>${project.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>software.amazon.lambda</groupId>
27+
<artifactId>powertools-logging</artifactId>
28+
<version>${project.version}</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>software.amazon.lambda</groupId>
32+
<artifactId>powertools-metrics</artifactId>
33+
<version>${project.version}</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>com.amazonaws</groupId>
37+
<artifactId>aws-lambda-java-core</artifactId>
38+
<version>1.2.2</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>com.amazonaws</groupId>
42+
<artifactId>aws-lambda-java-events</artifactId>
43+
<version>3.11.2</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.apache.logging.log4j</groupId>
47+
<artifactId>log4j-core</artifactId>
48+
<version>${log4j.version}</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.apache.logging.log4j</groupId>
52+
<artifactId>log4j-api</artifactId>
53+
<version>${log4j.version}</version>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>junit</groupId>
58+
<artifactId>junit</artifactId>
59+
<version>4.13.2</version>
60+
<scope>test</scope>
61+
</dependency>
62+
</dependencies>
63+
64+
<build>
65+
<finalName>helloworld-lambda</finalName>
66+
<plugins>
67+
<plugin>
68+
<groupId>dev.aspectj</groupId>
69+
<artifactId>aspectj-maven-plugin</artifactId>
70+
<version>1.13.1</version>
71+
<configuration>
72+
<source>${maven.compiler.source}</source>
73+
<target>${maven.compiler.target}</target>
74+
<complianceLevel>${maven.compiler.target}</complianceLevel>
75+
<aspectLibraries>
76+
<aspectLibrary>
77+
<groupId>software.amazon.lambda</groupId>
78+
<artifactId>powertools-tracing</artifactId>
79+
</aspectLibrary>
80+
<aspectLibrary>
81+
<groupId>software.amazon.lambda</groupId>
82+
<artifactId>powertools-logging</artifactId>
83+
</aspectLibrary>
84+
<aspectLibrary>
85+
<groupId>software.amazon.lambda</groupId>
86+
<artifactId>powertools-metrics</artifactId>
87+
</aspectLibrary>
88+
</aspectLibraries>
89+
</configuration>
90+
<executions>
91+
<execution>
92+
<goals>
93+
<goal>compile</goal>
94+
</goals>
95+
</execution>
96+
</executions>
97+
</plugin>
98+
<plugin>
99+
<groupId>org.apache.maven.plugins</groupId>
100+
<artifactId>maven-shade-plugin</artifactId>
101+
<version>3.5.0</version>
102+
<executions>
103+
<execution>
104+
<phase>package</phase>
105+
<goals>
106+
<goal>shade</goal>
107+
</goals>
108+
<configuration>
109+
<transformers>
110+
<transformer
111+
implementation="com.github.edwgiz.maven_shade_plugin.log4j2_cache_transformer.PluginsCacheFileTransformer">
112+
</transformer>
113+
</transformers>
114+
</configuration>
115+
</execution>
116+
</executions>
117+
<dependencies>
118+
<dependency>
119+
<groupId>com.github.edwgiz</groupId>
120+
<artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
121+
<version>2.15</version>
122+
</dependency>
123+
</dependencies>
124+
</plugin>
125+
</plugins>
126+
</build>
127+
<profiles>
128+
<!-- Use a profile to enforce AspectJ version 1.9.7 if we are Java 1.8 otherwise we'll get class
129+
version mismatch issues. All subsequent Java releases build with the default AspectJ configuration
130+
on the project.
131+
132+
Note:
133+
- if you are running Java > 1.8, you can remove this profile altogether
134+
- If you are running on Java 1.8, you should apply the aspectJ version here to the project, and remove
135+
the profile.
136+
-->
137+
<profile>
138+
<id>jdk8</id>
139+
<activation>
140+
<jdk>(,11)</jdk> <!-- 8 -->
141+
</activation>
142+
<properties>
143+
<aspectj.version>1.9.7</aspectj.version>
144+
</properties>
145+
<dependencyManagement>
146+
<dependencies>
147+
<dependency>
148+
<groupId>org.aspectj</groupId>
149+
<artifactId>aspectjtools</artifactId>
150+
<version>${aspectj.version}</version>
151+
</dependency>
152+
</dependencies>
153+
</dependencyManagement>
154+
<build>
155+
<pluginManagement>
156+
<plugins>
157+
<plugin>
158+
<groupId>dev.aspectj</groupId>
159+
<artifactId>aspectj-maven-plugin</artifactId>
160+
<version>${aspectj.plugin.version}</version>
161+
<configuration>
162+
<source>${maven.compiler.source}</source>
163+
<target>${maven.compiler.target}</target>
164+
<complianceLevel>${maven.compiler.target}</complianceLevel>
165+
<aspectLibraries>
166+
<aspectLibrary>
167+
<groupId>software.amazon.lambda</groupId>
168+
<artifactId>powertools-tracing</artifactId>
169+
</aspectLibrary>
170+
<aspectLibrary>
171+
<groupId>software.amazon.lambda</groupId>
172+
<artifactId>powertools-logging</artifactId>
173+
</aspectLibrary>
174+
<aspectLibrary>
175+
<groupId>software.amazon.lambda</groupId>
176+
<artifactId>powertools-metrics</artifactId>
177+
</aspectLibrary>
178+
</aspectLibraries>
179+
</configuration>
180+
<executions>
181+
<execution>
182+
<goals>
183+
<goal>compile</goal>
184+
<goal>test-compile</goal>
185+
</goals>
186+
</execution>
187+
</executions>
188+
<!-- Enforce aspectJ 1.9.7 -->
189+
<dependencies>
190+
<dependency>
191+
<groupId>org.aspectj</groupId>
192+
<artifactId>aspectjtools</artifactId>
193+
<version>${aspectj.version}</version>
194+
</dependency>
195+
</dependencies>
196+
</plugin>
197+
</plugins>
198+
</pluginManagement>
199+
</build>
200+
</profile>
201+
</profiles>
202+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"app": "mvn -e -q compile exec:java",
3+
"watch": {
4+
"include": [
5+
"**"
6+
],
7+
"exclude": [
8+
"README.md",
9+
"cdk*.json",
10+
"target",
11+
"pom.xml",
12+
"src/test"
13+
]
14+
},
15+
"context": {
16+
"@aws-cdk/aws-lambda:recognizeLayerVersion": true,
17+
"@aws-cdk/core:checkSecretUsage": true,
18+
"@aws-cdk/core:target-partitions": [
19+
"aws",
20+
"aws-cn"
21+
],
22+
"@aws-cdk/aws-iam:minimizePolicies": true,
23+
"@aws-cdk/core:validateSnapshotRemovalPolicy": true,
24+
"@aws-cdk/aws-apigateway:disableCloudWatchRole": true,
25+
"@aws-cdk/core:enablePartitionLiterals": true,
26+
"@aws-cdk/aws-events:eventsTargetQueueSameAccount": true,
27+
"@aws-cdk/aws-iam:standardizedServicePrincipals": true,
28+
"@aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName": true,
29+
"@aws-cdk/customresources:installLatestAwsSdkDefault": false,
30+
"@aws-cdk/aws-apigateway:authorizerChangeDeploymentLogicalId": true,
31+
"@aws-cdk/aws-secretsmanager:useAttachedSecretResourcePolicyForSecretTargetAttachments": true,
32+
"@aws-cdk/aws-apigateway:requestValidatorUniqueId": true,
33+
"@aws-cdk/aws-kms:aliasNameRef": true,
34+
"@aws-cdk/core:includePrefixInUniqueNameGeneration": true
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
3+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>software.amazon.lambda.examples</groupId>
6+
<artifactId>cdk</artifactId>
7+
<version>1.17.0-SNAPSHOT</version>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<cdk.version>2.88.0</cdk.version>
11+
<constructs.version>[10.0.0,11.0.0)</constructs.version>
12+
<junit.version>5.7.1</junit.version>
13+
</properties>
14+
<build>
15+
<plugins>
16+
<plugin>
17+
<groupId>org.apache.maven.plugins</groupId>
18+
<artifactId>maven-compiler-plugin</artifactId>
19+
<version>3.8.1</version>
20+
<configuration>
21+
<source>1.8</source>
22+
<target>1.8</target>
23+
</configuration>
24+
</plugin>
25+
<plugin>
26+
<groupId>org.codehaus.mojo</groupId>
27+
<artifactId>exec-maven-plugin</artifactId>
28+
<version>3.0.0</version>
29+
<configuration>
30+
<mainClass>cdk.CdkApp</mainClass>
31+
</configuration>
32+
</plugin>
33+
</plugins>
34+
</build>
35+
<dependencies>
36+
<!-- AWS Cloud Development Kit -->
37+
<dependency>
38+
<groupId>software.amazon.awscdk</groupId>
39+
<artifactId>aws-cdk-lib</artifactId>
40+
<version>${cdk.version}</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>software.constructs</groupId>
44+
<artifactId>constructs</artifactId>
45+
<version>${constructs.version}</version>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>org.junit.jupiter</groupId>
50+
<artifactId>junit-jupiter</artifactId>
51+
<version>${junit.version}</version>
52+
<scope>test</scope>
53+
</dependency>
54+
</dependencies>
55+
</project>

0 commit comments

Comments
 (0)