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 4 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/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
32 changes: 32 additions & 0 deletions examples/powertools-examples-core/serverless/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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).

AlexeySoshin marked this conversation as resolved.
Show resolved Hide resolved
For general information on the deployed example itself, you can refer to the parent [README](../README.md)
AlexeySoshin marked this conversation as resolved.
Show resolved Hide resolved

AlexeySoshin marked this conversation as resolved.
Show resolved Hide resolved
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)
AlexeySoshin marked this conversation as resolved.
Show resolved Hide resolved

## 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

The minimum to deploy the app should be
AlexeySoshin marked this conversation as resolved.
Show resolved Hide resolved
```bash
mvn package && sls deploy
```

## Useful commands
Deploy your entire stack
```bash
sls deploy
scottgerring marked this conversation as resolved.
Show resolved Hide resolved
```

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>
117 changes: 117 additions & 0 deletions examples/powertools-examples-core/serverless/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Welcome to Serverless!
#
# This file is the main config file for your service.
# It's very minimal at this point and uses default values.
scottgerring marked this conversation as resolved.
Show resolved Hide resolved
# You can always add more config options for more control.
# We've included some commented out config examples here.
# Just uncomment any of them to get that config option.
#
# For full config options, check the docs:
# docs.serverless.com
#
# Happy Coding!

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 add statements to the Lambda function's IAM Role here
# iam:
# role:
# statements:
# - Effect: "Allow"
# Action:
# - "s3:ListBucket"
# Resource: { "Fn::Join" : ["", ["arn:aws:s3:::", { "Ref" : "ServerlessDeploymentBucket" } ] ] }
# - Effect: "Allow"
# Action:
# - "s3:PutObject"
# Resource:
# Fn::Join:
# - ""
# - - "arn:aws:s3:::"
# - "Ref" : "ServerlessDeploymentBucket"
# - "/*"

# you can define service wide environment variables here
# environment:
# variable1: value1

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

functions:
hello:
handler: helloworld.App
# The following are a few example events you can configure
# NOTE: Please make sure to change your handler code to work with those events
# Check the event documentation for details
memorySize: 512
timeout: 20
tracing: "Active"
events:
- httpApi:
path: /hello
method: get
# - websocket: $connect
# - s3: ${env:BUCKET}
# - schedule: rate(10 minutes)
# - sns: greeter-topic
# - stream: arn:aws:dynamodb:region:XXXXXX:table/foo/stream/1970-01-01T00:00:00.000
# - alexaSkill: amzn1.ask.skill.xx-xx-xx-xx
# - alexaSmartHome: amzn1.ask.skill.xx-xx-xx-xx
# - iot:
# sql: "SELECT * FROM 'some_topic'"
# - cloudwatchEvent:
# event:
# source:
# - "aws.ec2"
# detail-type:
# - "EC2 Instance State-change Notification"
# detail:
# state:
# - pending
# - cloudwatchLog: '/aws/lambda/hello'
# - cognitoUserPool:
# pool: MyUserPool
# trigger: PreSignUp
# - alb:
# listenerArn: arn:aws:elasticloadbalancing:us-east-1:XXXXXX:listener/app/my-load-balancer/50dc6c495c0c9188/
# priority: 1
# conditions:
# host: example.com
# path: /hello

# Define function environment variables here
environment:
POWERTOOLS_LOG_LEVEL: INFO
POWERTOOLS_LOGGER_SAMPLE_RATE: 0.1
POWERTOOLS_LOGGER_LOG_EVENT: true
POWERTOOLS_METRICS_NAMESPACE: Coreutilities
scottgerring marked this conversation as resolved.
Show resolved Hide resolved
POWERTOOLS_SERVICE_NAME: hello

# you can add CloudFormation resource templates here
#resources:
# Resources:
# NewResource:
# Type: AWS::S3::Bucket
# Properties:
# BucketName: my-new-bucket
# Outputs:
# NewOutput:
# Description: "Description for the output"
# Value: "Some output value"
Loading