Skip to content

Commit 8d09870

Browse files
AlexeySoshinscottgerringjeromevdl
committed
docs: Add Serveless Framework example (#1363)
* Initial * Add memory size and timeouts, same as SAM examples * Add useful Serverless commands * Remove redundant function * Update examples/powertools-examples-core/serverless/README.md Co-authored-by: Scott Gerring <[email protected]> * Update examples/powertools-examples-core/serverless/README.md Co-authored-by: Scott Gerring <[email protected]> * Update examples/powertools-examples-core/serverless/README.md Co-authored-by: Scott Gerring <[email protected]> * Update examples/powertools-examples-core/serverless/README.md Co-authored-by: Scott Gerring <[email protected]> * Update examples/powertools-examples-core/serverless/README.md Co-authored-by: Scott Gerring <[email protected]> * Remove reduntant command from README * Move most of Powertools configuration to the service-wide environment * Remove more generated comments * Add newlines * Comment out annotations that are preceded by environment variables * Add link to the Serverless example * Update examples/powertools-examples-core/serverless/pom.xml Co-authored-by: Jérôme Van Der Linden <[email protected]> * Update examples/powertools-examples-core/serverless/pom.xml Co-authored-by: Jérôme Van Der Linden <[email protected]> * Update examples/powertools-examples-core/serverless/pom.xml Co-authored-by: Jérôme Van Der Linden <[email protected]> * Remove POWERTOOLS_LOGGER_LOG_EVENT, since it's unsupported --------- Co-authored-by: Scott Gerring <[email protected]> Co-authored-by: Jérôme Van Der Linden <[email protected]>
1 parent 50d3df8 commit 8d09870

File tree

10 files changed

+496
-0
lines changed

10 files changed

+496
-0
lines changed

examples/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Each example can be copied from its subdirectory and used independently of the r
88
* [powertools-examples-core](powertools-examples-core) - Demonstrates the core logging, tracing, and metrics modules with different build tools
99
* [SAM](./powertools-examples-core/sam)
1010
* [CDK](./powertools-examples-core/cdk)
11+
* [Serverless](./powertools-examples-core/serverless)
1112
* [powertools-examples-idempotency](powertools-examples-idempotency) - An idempotent HTTP API
1213
* [powertools-examples-parameters](powertools-examples-parameters) - Uses the parameters module to provide runtime parameters to a function
1314
* [powertools-examples-serialization](powertools-examples-serialization) - Uses the serialization module to serialize and deserialize API Gateway & SQS payloads

examples/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<module>powertools-examples-core/sam</module>
3333
<module>powertools-examples-core/cdk/app</module>
3434
<module>powertools-examples-core/cdk/infra</module>
35+
<module>powertools-examples-core/serverless</module>
3536
<module>powertools-examples-idempotency</module>
3637
<module>powertools-examples-parameters</module>
3738
<module>powertools-examples-serialization</module>

examples/powertools-examples-core-utilities/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ We provide examples for the following infrastructure-as-code tools:
99

1010
* [AWS SAM](sam/)
1111
* [AWS CDK](cdk/)
12+
* [Serverless framework](serverless/)
1213

1314
We also provide an example showing the integration of SAM, Powertools, and Gradle:
1415

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Powertools for AWS Lambda (Java) - Core Utilities Example with Serverless Framework
2+
3+
This project demonstrates the Lambda for Powertools Java module deployed using [Serverless Framework](https://www.serverless.com/framework).
4+
For general information on the deployed example itself, you can refer to the parent [README](../README.md).
5+
To install Serverless Framework if you don't have it yet, you can follow the [Getting Started Guide](https://www.serverless.com/framework/docs/getting-started).
6+
7+
## Configuration
8+
Serverless Framework uses [serverless.yml](./serverless.yml) to define the application's AWS resources.
9+
This file defines the Lambda function to be deployed as well as API Gateway for it.
10+
11+
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.
12+
13+
14+
## Deploy the sample application
15+
16+
To deploy the app, simply run the following commands:
17+
```bash
18+
mvn package && sls deploy
19+
```
20+
21+
## Useful commands
22+
23+
Deploy a single function
24+
```bash
25+
sls deploy function -f hello
26+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
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.18.0-SNAPSHOT</version>
7+
<artifactId>powertools-examples-core-serverless</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+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>software.amazon.lambda</groupId>
21+
<artifactId>powertools-tracing</artifactId>
22+
<version>${project.version}</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>software.amazon.lambda</groupId>
26+
<artifactId>powertools-logging</artifactId>
27+
<version>${project.version}</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>software.amazon.lambda</groupId>
31+
<artifactId>powertools-metrics</artifactId>
32+
<version>${project.version}</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>com.amazonaws</groupId>
36+
<artifactId>aws-lambda-java-core</artifactId>
37+
<version>1.2.2</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>com.amazonaws</groupId>
41+
<artifactId>aws-lambda-java-events</artifactId>
42+
<version>3.11.2</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.apache.logging.log4j</groupId>
46+
<artifactId>log4j-core</artifactId>
47+
<version>${log4j.version}</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.apache.logging.log4j</groupId>
51+
<artifactId>log4j-api</artifactId>
52+
<version>${log4j.version}</version>
53+
</dependency>
54+
55+
<dependency>
56+
<groupId>junit</groupId>
57+
<artifactId>junit</artifactId>
58+
<version>4.13.2</version>
59+
<scope>test</scope>
60+
</dependency>
61+
</dependencies>
62+
63+
<build>
64+
<finalName>helloworld-lambda</finalName>
65+
<plugins>
66+
<plugin>
67+
<groupId>dev.aspectj</groupId>
68+
<artifactId>aspectj-maven-plugin</artifactId>
69+
<version>1.13.1</version>
70+
<configuration>
71+
<source>${maven.compiler.source}</source>
72+
<target>${maven.compiler.target}</target>
73+
<complianceLevel>${maven.compiler.target}</complianceLevel>
74+
<aspectLibraries>
75+
<aspectLibrary>
76+
<groupId>software.amazon.lambda</groupId>
77+
<artifactId>powertools-tracing</artifactId>
78+
</aspectLibrary>
79+
<aspectLibrary>
80+
<groupId>software.amazon.lambda</groupId>
81+
<artifactId>powertools-logging</artifactId>
82+
</aspectLibrary>
83+
<aspectLibrary>
84+
<groupId>software.amazon.lambda</groupId>
85+
<artifactId>powertools-metrics</artifactId>
86+
</aspectLibrary>
87+
</aspectLibraries>
88+
</configuration>
89+
<executions>
90+
<execution>
91+
<goals>
92+
<goal>compile</goal>
93+
</goals>
94+
</execution>
95+
</executions>
96+
</plugin>
97+
<plugin>
98+
<groupId>org.apache.maven.plugins</groupId>
99+
<artifactId>maven-shade-plugin</artifactId>
100+
<version>3.5.0</version>
101+
<executions>
102+
<execution>
103+
<phase>package</phase>
104+
<goals>
105+
<goal>shade</goal>
106+
</goals>
107+
<configuration>
108+
<transformers>
109+
<transformer
110+
implementation="com.github.edwgiz.maven_shade_plugin.log4j2_cache_transformer.PluginsCacheFileTransformer">
111+
</transformer>
112+
</transformers>
113+
</configuration>
114+
</execution>
115+
</executions>
116+
<dependencies>
117+
<dependency>
118+
<groupId>com.github.edwgiz</groupId>
119+
<artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
120+
<version>2.15</version>
121+
</dependency>
122+
</dependencies>
123+
</plugin>
124+
<!-- Don't deploy the example -->
125+
<plugin>
126+
<groupId>org.apache.maven.plugins</groupId>
127+
<artifactId>maven-deploy-plugin</artifactId>
128+
<configuration>
129+
<skip>true</skip>
130+
</configuration>
131+
</plugin>
132+
</plugins>
133+
</build>
134+
<profiles>
135+
<!-- Use a profile to enforce AspectJ version 1.9.7 if we are Java 1.8 otherwise we'll get class
136+
version mismatch issues. All subsequent Java releases build with the default AspectJ configuration
137+
on the project.
138+
139+
Note:
140+
- if you are running Java > 1.8, you can remove this profile altogether
141+
- If you are running on Java 1.8, you should apply the aspectJ version here to the project, and remove
142+
the profile.
143+
-->
144+
<profile>
145+
<id>jdk8</id>
146+
<activation>
147+
<jdk>(,11)</jdk> <!-- 8 -->
148+
</activation>
149+
<properties>
150+
<aspectj.version>1.9.7</aspectj.version>
151+
</properties>
152+
<dependencyManagement>
153+
<dependencies>
154+
<dependency>
155+
<groupId>org.aspectj</groupId>
156+
<artifactId>aspectjtools</artifactId>
157+
<version>${aspectj.version}</version>
158+
</dependency>
159+
</dependencies>
160+
</dependencyManagement>
161+
<build>
162+
<pluginManagement>
163+
<plugins>
164+
<plugin>
165+
<groupId>dev.aspectj</groupId>
166+
<artifactId>aspectj-maven-plugin</artifactId>
167+
<version>${aspectj.plugin.version}</version>
168+
<configuration>
169+
<source>${maven.compiler.source}</source>
170+
<target>${maven.compiler.target}</target>
171+
<complianceLevel>${maven.compiler.target}</complianceLevel>
172+
<aspectLibraries>
173+
<aspectLibrary>
174+
<groupId>software.amazon.lambda</groupId>
175+
<artifactId>powertools-tracing</artifactId>
176+
</aspectLibrary>
177+
<aspectLibrary>
178+
<groupId>software.amazon.lambda</groupId>
179+
<artifactId>powertools-logging</artifactId>
180+
</aspectLibrary>
181+
<aspectLibrary>
182+
<groupId>software.amazon.lambda</groupId>
183+
<artifactId>powertools-metrics</artifactId>
184+
</aspectLibrary>
185+
</aspectLibraries>
186+
</configuration>
187+
<executions>
188+
<execution>
189+
<goals>
190+
<goal>compile</goal>
191+
<goal>test-compile</goal>
192+
</goals>
193+
</execution>
194+
</executions>
195+
<!-- Enforce aspectJ 1.9.7 -->
196+
<dependencies>
197+
<dependency>
198+
<groupId>org.aspectj</groupId>
199+
<artifactId>aspectjtools</artifactId>
200+
<version>${aspectj.version}</version>
201+
</dependency>
202+
</dependencies>
203+
</plugin>
204+
</plugins>
205+
</pluginManagement>
206+
</build>
207+
</profile>
208+
</profiles>
209+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
service: hello
2+
# app and org for use with dashboard.serverless.com
3+
#app: your-app-name
4+
#org: your-org-name
5+
6+
# You can pin your service to only deploy with a specific Serverless version
7+
# Check out our docs for more details
8+
frameworkVersion: '3'
9+
10+
provider:
11+
name: aws
12+
runtime: java11
13+
14+
# you can overwrite defaults here
15+
# stage: dev
16+
# region: us-east-1
17+
18+
# you can define service wide environment variables here
19+
environment:
20+
POWERTOOLS_LOG_LEVEL: INFO
21+
POWERTOOLS_LOGGER_SAMPLE_RATE: 0.1
22+
POWERTOOLS_METRICS_NAMESPACE: Coreutilities
23+
24+
# you can add packaging information here
25+
package:
26+
artifact: target/helloworld-lambda.jar
27+
28+
functions:
29+
hello:
30+
handler: helloworld.App
31+
memorySize: 512
32+
timeout: 20
33+
tracing: "Active"
34+
events:
35+
- httpApi:
36+
path: /hello
37+
method: get
38+
# Define function environment variables here
39+
environment:
40+
POWERTOOLS_SERVICE_NAME: hello

0 commit comments

Comments
 (0)