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

docs: Add Serveless Framework example #1363

Merged
merged 29 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c107f8f
Initial
AlexeySoshin Aug 14, 2023
c405cbb
Add memory size and timeouts, same as SAM examples
AlexeySoshin Aug 14, 2023
6896dbd
Add useful Serverless commands
AlexeySoshin Aug 14, 2023
a4a6445
Remove redundant function
AlexeySoshin Aug 14, 2023
3a2e6e4
Update examples/powertools-examples-core/serverless/README.md
AlexeySoshin Aug 16, 2023
8e59d86
Update examples/powertools-examples-core/serverless/README.md
AlexeySoshin Aug 16, 2023
c2b2093
Update examples/powertools-examples-core/serverless/README.md
AlexeySoshin Aug 16, 2023
203298a
Update examples/powertools-examples-core/serverless/README.md
AlexeySoshin Aug 16, 2023
c3e5f43
Merge branch 'main' of https://github.com/aws-powertools/powertools-l…
AlexeySoshin Aug 16, 2023
134b9de
Update examples/powertools-examples-core/serverless/README.md
AlexeySoshin Aug 18, 2023
ca596dc
Merge branch 'main' of https://github.com/aws-powertools/powertools-l…
AlexeySoshin Aug 18, 2023
4f6eaee
Remove reduntant command from README
AlexeySoshin Aug 18, 2023
0de614f
Move most of Powertools configuration to the service-wide environment
AlexeySoshin Aug 18, 2023
917f3f7
Merge branch 'main' into serverless-example
scottgerring Aug 21, 2023
c28e144
Merge branch 'main' of https://github.com/aws-powertools/powertools-l…
AlexeySoshin Aug 22, 2023
d76ff02
Merge branch 'serverless-example' of https://github.com/AlexeySoshin/…
AlexeySoshin Aug 22, 2023
d7b7d75
Merge branch 'main' of https://github.com/aws-powertools/powertools-l…
AlexeySoshin Aug 31, 2023
1b9d148
Remove more generated comments
AlexeySoshin Aug 31, 2023
0d7703d
Merge branch 'main' of https://github.com/aws-powertools/powertools-l…
AlexeySoshin Sep 4, 2023
86e9744
Add newlines
AlexeySoshin Sep 4, 2023
b96bb90
Comment out annotations that are preceded by environment variables
AlexeySoshin Sep 4, 2023
20fd3b9
Add link to the Serverless example
AlexeySoshin Sep 4, 2023
802b149
Merge branch 'main' of https://github.com/aws-powertools/powertools-l…
AlexeySoshin Sep 25, 2023
099bb0a
Merge branch 'main' of https://github.com/aws-powertools/powertools-l…
AlexeySoshin Sep 26, 2023
5471f77
Update examples/powertools-examples-core/serverless/pom.xml
AlexeySoshin Sep 27, 2023
3e6e889
Update examples/powertools-examples-core/serverless/pom.xml
AlexeySoshin Sep 27, 2023
0287452
Update examples/powertools-examples-core/serverless/pom.xml
AlexeySoshin Sep 27, 2023
7e66b9d
Remove POWERTOOLS_LOGGER_LOG_EVENT, since it's unsupported
AlexeySoshin Sep 27, 2023
8c04c27
Merge branch 'main' into serverless-example
scottgerring Sep 28, 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
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Each example can be copied from its subdirectory and used independently of the r
* [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)
* [Serverless](./powertools-examples-core/serverless)
* [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
1 change: 1 addition & 0 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<module>powertools-examples-core/sam</module>
<module>powertools-examples-core/cdk/app</module>
<module>powertools-examples-core/cdk/infra</module>
<module>powertools-examples-core/serverless</module>
<module>powertools-examples-idempotency</module>
<module>powertools-examples-parameters</module>
<module>powertools-examples-serialization</module>
Expand Down
1 change: 1 addition & 0 deletions examples/powertools-examples-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ We provide examples for the following infrastructure-as-code tools:

* [AWS SAM](sam/)
* [AWS CDK](cdk/)
* [Serverless framework](serverless/)

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

Expand Down
26 changes: 26 additions & 0 deletions examples/powertools-examples-core/serverless/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Powertools for AWS Lambda (Java) - Core Utilities Example with Serverless Framework

This project demonstrates the Lambda for Powertools Java module deployed using [Serverless Framework](https://www.serverless.com/framework).
For general information on the deployed example itself, you can refer to the parent [README](../README.md).
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).

## Configuration
Serverless Framework uses [serverless.yml](./serverless.yml) to define the application's AWS resources.
This file 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

To deploy the app, simply run the following commands:
```bash
mvn package && sls deploy
```

## Useful commands

Deploy a single function
```bash
sls deploy function -f hello
```
202 changes: 202 additions & 0 deletions examples/powertools-examples-core/serverless/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>
AlexeySoshin marked this conversation as resolved.
Show resolved Hide resolved
<artifactId>powertools-examples-core-serverless</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>
AlexeySoshin marked this conversation as resolved.
Show resolved Hide resolved
</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>
jeromevdl marked this conversation as resolved.
Show resolved Hide resolved
<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>
AlexeySoshin marked this conversation as resolved.
Show resolved Hide resolved
</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>
41 changes: 41 additions & 0 deletions examples/powertools-examples-core/serverless/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
service: hello
# app and org for use with dashboard.serverless.com
#app: your-app-name
#org: your-org-name

# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
frameworkVersion: '3'

provider:
name: aws
runtime: java11
jeromevdl marked this conversation as resolved.
Show resolved Hide resolved

# you can overwrite defaults here
# stage: dev
# region: us-east-1

# you can define service wide environment variables here
environment:
POWERTOOLS_LOG_LEVEL: INFO
POWERTOOLS_LOGGER_SAMPLE_RATE: 0.1
POWERTOOLS_LOGGER_LOG_EVENT: true
jeromevdl marked this conversation as resolved.
Show resolved Hide resolved
POWERTOOLS_METRICS_NAMESPACE: Coreutilities

# you can add packaging information here
package:
artifact: target/helloworld-lambda.jar

functions:
hello:
handler: helloworld.App
memorySize: 512
timeout: 20
tracing: "Active"
events:
- httpApi:
path: /hello
method: get
# Define function environment variables here
environment:
POWERTOOLS_SERVICE_NAME: hello
Loading