Skip to content

docs: Adding CDK example #1321

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

Merged
merged 57 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
27ad36a
Move the current example, which is for SAM, under /sam directory
AlexeySoshin Jul 27, 2023
1b4982c
Create a new directory for CDK
AlexeySoshin Jul 27, 2023
537124a
Add CDK Example as a Maven module
AlexeySoshin Jul 27, 2023
b01f105
Merge branch 'main' into cdk-example
AlexeySoshin Jul 27, 2023
c915992
CDK Stack stub
AlexeySoshin Jul 27, 2023
74c9f35
Restructure CDK application info Infra and App projects
AlexeySoshin Jul 28, 2023
9bf23ea
Merge branch 'main' into cdk-example
scottgerring Jul 31, 2023
db09638
Define the build of the project inside the CDK stack
AlexeySoshin Jul 31, 2023
2d12656
Merge remote-tracking branch 'origin/cdk-example' into cdk-example
AlexeySoshin Jul 31, 2023
3e7091b
Use default account and region
AlexeySoshin Jul 31, 2023
c283f78
Merge branch 'main' into cdk-example
AlexeySoshin Jul 31, 2023
7b7aef7
Add example of setting environment variables in CDK
AlexeySoshin Jul 31, 2023
5fae08d
Remove threads from the examples, as this should be covered by docume…
AlexeySoshin Aug 1, 2023
3ef58f7
Add general README for the project
AlexeySoshin Aug 1, 2023
7c0278f
Add specific README for SAM
AlexeySoshin Aug 1, 2023
ff16cb5
Add specific README for CDK
AlexeySoshin Aug 1, 2023
eeeaa6d
Use Java11 syntax for CDK
AlexeySoshin Aug 1, 2023
866a50d
Refactor the code for clarity
AlexeySoshin Aug 1, 2023
20cd148
Fix imports
AlexeySoshin Aug 1, 2023
de3513a
Add outputs example
AlexeySoshin Aug 1, 2023
781ecf5
Merge branch 'main' into cdk-example
AlexeySoshin Aug 2, 2023
5218b86
Add test for the stack
AlexeySoshin Aug 3, 2023
68da97c
Update examples/powertools-examples-core/README.md
AlexeySoshin Aug 3, 2023
2dbb190
Update examples/powertools-examples-core/cdk/README.md
AlexeySoshin Aug 3, 2023
3f76c04
Remove unnecessary .gitignore
AlexeySoshin Aug 3, 2023
103e1ba
Merge branch 'cdk-example' of https://github.com/AlexeySoshin/powerto…
AlexeySoshin Aug 3, 2023
64f3671
Mixed log level and sample rate
AlexeySoshin Aug 3, 2023
a9a3509
Combine .gitignore files
AlexeySoshin Aug 3, 2023
9a40117
Remove `cdk ls`, since there's just a single stack
AlexeySoshin Aug 3, 2023
65fb19c
Remove SAM mentions from the CDK readme
AlexeySoshin Aug 3, 2023
f9215a4
Replace "architecture" with "tool" while talking about SAM/CDK
AlexeySoshin Aug 3, 2023
a4f6043
Update examples/powertools-examples-core/README.md
AlexeySoshin Aug 3, 2023
0405ce5
Update examples/powertools-examples-core/README.md
AlexeySoshin Aug 3, 2023
24a96fc
Merge branch 'main' of https://github.com/aws-powertools/powertools-l…
AlexeySoshin Aug 3, 2023
686448a
Reformat imports
AlexeySoshin Aug 3, 2023
89c21ee
Don't include version number in the HelloWorld jar
AlexeySoshin Aug 3, 2023
06f8c51
Update examples/powertools-examples-core/cdk/infra/src/main/java/cdk/…
AlexeySoshin Aug 3, 2023
97755b8
Add Streaming example as well
AlexeySoshin Aug 4, 2023
6872c9f
Merge remote-tracking branch 'origin/cdk-example' into cdk-example
AlexeySoshin Aug 4, 2023
3fb78d7
Update examples/powertools-examples-core/README.md
AlexeySoshin Aug 4, 2023
8f9e7fe
Update examples/.gitignore
AlexeySoshin Aug 4, 2023
1fd7c4e
Update examples/.gitignore
AlexeySoshin Aug 4, 2023
f44db14
Update examples/powertools-examples-core/README.md
AlexeySoshin Aug 4, 2023
da4558c
Update examples/powertools-examples-core/cdk/README.md
AlexeySoshin Aug 4, 2023
8c93da8
Update examples/powertools-examples-core/sam/README.md
AlexeySoshin Aug 4, 2023
e33536e
Exclude examples from duplicate code scan
AlexeySoshin Aug 4, 2023
b580931
Fix broken link to the events file
AlexeySoshin Aug 4, 2023
10ad54d
Trim cdk.json
AlexeySoshin Aug 4, 2023
8c50227
Trim cdk.json
AlexeySoshin Aug 4, 2023
d6ecd32
Merge
AlexeySoshin Aug 4, 2023
fd3e345
Merge
AlexeySoshin Aug 8, 2023
7a5cbc7
Merge branch 'main' of https://github.com/aws-powertools/powertools-l…
AlexeySoshin Aug 8, 2023
fdfb168
Downgrade code to Java 8
AlexeySoshin Aug 8, 2023
578c800
Merge branch 'main' into cdk-example
scottgerring Aug 14, 2023
6e0b9fe
Correct README to point to powertools-core-idempotency for a quick start
AlexeySoshin Aug 14, 2023
71a47dc
Add the missing license to the new Java files
AlexeySoshin Aug 14, 2023
f078eea
Reformat code
AlexeySoshin Aug 14, 2023
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
2 changes: 2 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
dependency-reduced-pom.xml
.aws-sam
cdk.out
.m2
10 changes: 7 additions & 3 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ Each example can be copied from its subdirectory and used independently of the r

## Examples

* [powertools-examples-core](powertools-examples-core) - Demonstrates the core logging, tracing, and metrics modules
* [powertools-examples-core](powertools-examples-core) - Demonstrates the core logging, tracing, and metrics modules with different build tools
* [SAM](./powertools-examples-core/sam)
* [CDK](./powertools-examples-core/cdk)
* [powertools-examples-idempotency](powertools-examples-idempotency) - An idempotent HTTP API
* [powertools-examples-parameters](powertools-examples-parameters) - Uses the parameters module to provide runtime parameters to a function
* [powertools-examples-serialization](powertools-examples-serialization) - Uses the serialization module to serialize and deserialize API Gateway & SQS payloads
Expand Down Expand Up @@ -34,8 +36,8 @@ amongst other things.
To build and deploy an example application for the first time, run the following in your shell:

```bash
# Switch to the directory containing an example for the powertools-core module
$ cd powertools-examples-core
# Switch to the directory containing an example for the powertools-idempotency module
$ cd powertools-examples-idempotency

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

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

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

### External examples

You can find more examples in the https://github.com/aws/aws-sam-cli-app-templates project:
Expand Down
4 changes: 3 additions & 1 deletion examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
</description>

<modules>
<module>powertools-examples-core</module>
<module>powertools-examples-core/sam</module>
<module>powertools-examples-core/cdk/app</module>
<module>powertools-examples-core/cdk/infra</module>
<module>powertools-examples-idempotency</module>
<module>powertools-examples-parameters</module>
<module>powertools-examples-serialization</module>
Expand Down
Empty file.
27 changes: 9 additions & 18 deletions examples/powertools-examples-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ This project demonstrates the Lambda for Powertools Java module - including
[tracing](https://docs.powertools.aws.dev/lambda/java/core/tracing/), and
[metrics](https://docs.powertools.aws.dev/lambda/java/core/metrics/).

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

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

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

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

## Test the application

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

Likewise, from the CloudWatch dashboard, under **Metrics**, **all metrics**, you will find the namespaces `Another`
and `ServerlessAirline`. The values in each of these are published by the code in
[App.java](src/main/java/helloworld/App.java).

You can also watch the trace information or log information using the SAM CLI:
```bash
# Tail the logs
sam logs --tail $MY_STACK

# Tail the traces
sam traces --tail
```
[App.java](sam/src/main/java/helloworld/App.java).
36 changes: 36 additions & 0 deletions examples/powertools-examples-core/cdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Powertools for AWS Lambda (Java) - Core Utilities Example with CDK

This project demonstrates the Lambda for Powertools Java module deployed using [Cloud Development Kit](https://aws.amazon.com/cdk/).

For general information on the deployed example itself, you can refer to the parent [README](../README.md)

## Configuration
CDK uses the following project structure:
- [app](./app) - stores the source code of your application, which is similar between all examples
- [infra](./infra) - stores the definition of your infrastructure
- [cdk.json](./infra/cdk.json) - tells the CDK Toolkit how to execute your app
- [CdkApp](./infra/src/main/java/cdk/CdkApp.java) - bootstraps your stack, taking AWS `account` and `region` as input
- [CdkStack](./infra/src/main/java/cdk/CdkStack.java) - defines the Lambda function to be deployed as well as API Gateway for it.

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.


## Deploy the sample application

The minimum to deploy the app should be
```bash
cdk deploy
```

If you're running CDK for the first time, you'll need to first run the bootstrap command:
```bash
cdk bootstrap
```

## Useful commands

* `mvn package` compile and run tests
* `cdk synth` emits the synthesized CloudFormation template
* `cdk deploy` deploy this stack to your default AWS account/region
* `cdk diff` compare deployed stack with current state
* `cdk docs` open CDK documentation
202 changes: 202 additions & 0 deletions examples/powertools-examples-core/cdk/app/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>software.amazon.lambda.examples</groupId>
<version>1.16.1</version>
<artifactId>powertools-examples-core-cdk</artifactId>
<packaging>jar</packaging>

<name>Powertools for AWS Lambda (Java) library Examples - Core</name>

<properties>
<log4j.version>2.20.0</log4j.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>

<dependencies>
<dependency>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-tracing</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-logging</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-metrics</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>3.11.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>helloworld-lambda</finalName>
<plugins>
<plugin>
<groupId>dev.aspectj</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.13.1</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<complianceLevel>${maven.compiler.target}</complianceLevel>
<aspectLibraries>
<aspectLibrary>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-tracing</artifactId>
</aspectLibrary>
<aspectLibrary>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-logging</artifactId>
</aspectLibrary>
<aspectLibrary>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-metrics</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="com.github.edwgiz.maven_shade_plugin.log4j2_cache_transformer.PluginsCacheFileTransformer">
</transformer>
</transformers>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.github.edwgiz</groupId>
<artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
<version>2.15</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<profiles>
<!-- Use a profile to enforce AspectJ version 1.9.7 if we are Java 1.8 otherwise we'll get class
version mismatch issues. All subsequent Java releases build with the default AspectJ configuration
on the project.

Note:
- if you are running Java > 1.8, you can remove this profile altogether
- If you are running on Java 1.8, you should apply the aspectJ version here to the project, and remove
the profile.
-->
<profile>
<id>jdk8</id>
<activation>
<jdk>(,11)</jdk> <!-- 8 -->
</activation>
<properties>
<aspectj.version>1.9.7</aspectj.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>dev.aspectj</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>${aspectj.plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<complianceLevel>${maven.compiler.target}</complianceLevel>
<aspectLibraries>
<aspectLibrary>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-tracing</artifactId>
</aspectLibrary>
<aspectLibrary>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-logging</artifactId>
</aspectLibrary>
<aspectLibrary>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-metrics</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<!-- Enforce aspectJ 1.9.7 -->
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@
import static software.amazon.lambda.powertools.metrics.MetricsUtils.metricsLogger;
import static software.amazon.lambda.powertools.metrics.MetricsUtils.withSingleMetric;
import static software.amazon.lambda.powertools.tracing.TracingUtils.putMetadata;
import static software.amazon.lambda.powertools.tracing.TracingUtils.withEntitySubsegment;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import com.amazonaws.xray.AWSXRay;
import com.amazonaws.xray.entities.Entity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -83,38 +80,17 @@ public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEv
log.info(output);
});

threadOption1();

threadOption2();

log.info("After output");
return response
.withStatusCode(200)
.withBody(output);
} catch (IOException | InterruptedException e) {
} catch (IOException e) {
return response
.withBody("{}")
.withStatusCode(500);
}
}

private void threadOption1() throws InterruptedException {
final Entity traceEntity = AWSXRay.getTraceEntity();
assert traceEntity != null;
traceEntity.run(new Thread(this::log));
}

private void threadOption2() throws InterruptedException {
Entity traceEntity = AWSXRay.getTraceEntity();
Thread anotherThread = new Thread(() -> withEntitySubsegment("inlineLog", traceEntity, subsegment ->
{
String var = "somethingToProcess";
log.info("inside threaded logging inline {}", var);
}));
anotherThread.start();
anotherThread.join();
}

@Tracing
private void log() {
log.info("inside threaded logging for function");
Expand Down
Loading