Skip to content

Commit ef11a64

Browse files
AlexeySoshinscottgerringjeromevdl
authored
docs: Adding CDK example (aws-powertools#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 ae58dd6 commit ef11a64

File tree

26 files changed

+906
-49
lines changed

26 files changed

+906
-49
lines changed

examples/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
dependency-reduced-pom.xml
22
.aws-sam
3+
cdk.out
4+
.m2

examples/README.md

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

66
## Examples
77

8-
* [powertools-examples-core](powertools-examples-core) - Demonstrates the core logging, tracing, and metrics modules
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)
911
* [powertools-examples-idempotency](powertools-examples-idempotency) - An idempotent HTTP API
1012
* [powertools-examples-parameters](powertools-examples-parameters) - Uses the parameters module to provide runtime parameters to a function
1113
* [powertools-examples-serialization](powertools-examples-serialization) - Uses the serialization module to serialize and deserialize API Gateway & SQS payloads
@@ -34,8 +36,8 @@ amongst other things.
3436
To build and deploy an example application for the first time, run the following in your shell:
3537

3638
```bash
37-
# Switch to the directory containing an example for the powertools-core module
38-
$ cd powertools-examples-core
39+
# Switch to the directory containing an example for the powertools-idempotency module
40+
$ cd powertools-examples-idempotency
3941

4042
# Build and deploy the example
4143
$ sam build
@@ -52,6 +54,8 @@ The first command will build the source of your application. The second command
5254

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

57+
If you're not using SAM, you can look for examples for other tools under [powertools-examples-core](./powertools-examples-core)
58+
5559
### External examples
5660

5761
You can find more examples in the https://github.com/aws/aws-sam-cli-app-templates project:

examples/pom.xml

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

3131
<modules>
32-
<module>powertools-examples-core</module>
32+
<module>powertools-examples-core/sam</module>
33+
<module>powertools-examples-core/cdk/app</module>
34+
<module>powertools-examples-core/cdk/infra</module>
3335
<module>powertools-examples-idempotency</module>
3436
<module>powertools-examples-parameters</module>
3537
<module>powertools-examples-serialization</module>

examples/powertools-examples-core/.gitignore

Whitespace-only changes.

examples/powertools-examples-core/README.md

+9-18
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ This project demonstrates the Lambda for Powertools Java module - including
55
[tracing](https://docs.powertools.aws.dev/lambda/java/core/tracing/), and
66
[metrics](https://docs.powertools.aws.dev/lambda/java/core/metrics/).
77

8-
It is made up of the following:
8+
We provide examples for the following infrastructure-as-code tools:
9+
* [AWS SAM](sam/)
10+
* [AWS CDK](cdk/)
911

10-
- [App.java](src/main/java/helloworld/App.java) - Code for the application's Lambda function.
11-
- [events](events) - Invocation events that you can use to invoke the function.
12-
- [AppTests.java](src/test/java/helloworld/AppTest.java) - Unit tests for the application code.
13-
- [template.yaml](template.yaml) - A template that defines the application's AWS resources.
12+
For each of the tools, the example application is the same, and consists of the following files:
1413

15-
## Deploy the sample application
14+
- [App.java](sam/src/main/java/helloworld/App.java) - Code for the application's Lambda function.
15+
- [AppTests.java](sam/src/test/java/helloworld/AppTest.java) - Unit tests for the application code.
16+
- [events](sam/events/event.json) - Invocation events that you can use to invoke the function.
1617

17-
This sample is based on Serverless Application Model (SAM). To deploy it, check out the instructions for getting
18-
started with SAM in [the examples directory](../README.md)
18+
Configuration files and deployment process for each tool are described in corresponding README files.
1919

2020
## Test the application
2121

@@ -35,13 +35,4 @@ different function calls within the example
3535

3636
Likewise, from the CloudWatch dashboard, under **Metrics**, **all metrics**, you will find the namespaces `Another`
3737
and `ServerlessAirline`. The values in each of these are published by the code in
38-
[App.java](src/main/java/helloworld/App.java).
39-
40-
You can also watch the trace information or log information using the SAM CLI:
41-
```bash
42-
# Tail the logs
43-
sam logs --tail $MY_STACK
44-
45-
# Tail the traces
46-
sam traces --tail
47-
```
38+
[App.java](sam/src/main/java/helloworld/App.java).
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>

examples/powertools-examples-core/src/main/java/helloworld/App.java renamed to examples/powertools-examples-core/cdk/app/src/main/java/helloworld/App.java

+1-25
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@
1717
import static software.amazon.lambda.powertools.metrics.MetricsUtils.metricsLogger;
1818
import static software.amazon.lambda.powertools.metrics.MetricsUtils.withSingleMetric;
1919
import static software.amazon.lambda.powertools.tracing.TracingUtils.putMetadata;
20-
import static software.amazon.lambda.powertools.tracing.TracingUtils.withEntitySubsegment;
2120

2221
import com.amazonaws.services.lambda.runtime.Context;
2322
import com.amazonaws.services.lambda.runtime.RequestHandler;
2423
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
2524
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
26-
import com.amazonaws.xray.AWSXRay;
27-
import com.amazonaws.xray.entities.Entity;
2825
import java.io.BufferedReader;
2926
import java.io.IOException;
3027
import java.io.InputStreamReader;
@@ -83,38 +80,17 @@ public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEv
8380
log.info(output);
8481
});
8582

86-
threadOption1();
87-
88-
threadOption2();
89-
9083
log.info("After output");
9184
return response
9285
.withStatusCode(200)
9386
.withBody(output);
94-
} catch (IOException | InterruptedException e) {
87+
} catch (IOException e) {
9588
return response
9689
.withBody("{}")
9790
.withStatusCode(500);
9891
}
9992
}
10093

101-
private void threadOption1() throws InterruptedException {
102-
final Entity traceEntity = AWSXRay.getTraceEntity();
103-
assert traceEntity != null;
104-
traceEntity.run(new Thread(this::log));
105-
}
106-
107-
private void threadOption2() throws InterruptedException {
108-
Entity traceEntity = AWSXRay.getTraceEntity();
109-
Thread anotherThread = new Thread(() -> withEntitySubsegment("inlineLog", traceEntity, subsegment ->
110-
{
111-
String var = "somethingToProcess";
112-
log.info("inside threaded logging inline {}", var);
113-
}));
114-
anotherThread.start();
115-
anotherThread.join();
116-
}
117-
11894
@Tracing
11995
private void log() {
12096
log.info("inside threaded logging for function");

0 commit comments

Comments
 (0)