From c107f8f948293a800bbdc4549cac7d16e9569bfc Mon Sep 17 00:00:00 2001 From: Alexey Soshin Date: Mon, 14 Aug 2023 13:55:50 +0100 Subject: [PATCH 01/19] Initial --- examples/pom.xml | 1 + .../serverless/README.md | 30 +++ .../serverless/pom.xml | 202 ++++++++++++++++++ .../serverless/serverless.yml | 114 ++++++++++ .../src/main/java/helloworld/App.java | 107 ++++++++++ .../src/main/java/helloworld/AppStream.java | 38 ++++ .../serverless/src/main/resources/log4j2.xml | 16 ++ .../src/test/java/helloworld/AppTest.java | 59 +++++ 8 files changed, 567 insertions(+) create mode 100644 examples/powertools-examples-core/serverless/README.md create mode 100644 examples/powertools-examples-core/serverless/pom.xml create mode 100644 examples/powertools-examples-core/serverless/serverless.yml create mode 100644 examples/powertools-examples-core/serverless/src/main/java/helloworld/App.java create mode 100644 examples/powertools-examples-core/serverless/src/main/java/helloworld/AppStream.java create mode 100644 examples/powertools-examples-core/serverless/src/main/resources/log4j2.xml create mode 100644 examples/powertools-examples-core/serverless/src/test/java/helloworld/AppTest.java diff --git a/examples/pom.xml b/examples/pom.xml index 9689e8d71..c8bfed762 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -32,6 +32,7 @@ powertools-examples-core/sam powertools-examples-core/cdk/app powertools-examples-core/cdk/infra + powertools-examples-core/serverless powertools-examples-idempotency powertools-examples-parameters powertools-examples-serialization diff --git a/examples/powertools-examples-core/serverless/README.md b/examples/powertools-examples-core/serverless/README.md new file mode 100644 index 000000000..e9ea2a0ea --- /dev/null +++ b/examples/powertools-examples-core/serverless/README.md @@ -0,0 +1,30 @@ +# 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 + +The minimum to deploy the app should be +```bash +sls deploy +``` + + +## 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 \ No newline at end of file diff --git a/examples/powertools-examples-core/serverless/pom.xml b/examples/powertools-examples-core/serverless/pom.xml new file mode 100644 index 000000000..85052554b --- /dev/null +++ b/examples/powertools-examples-core/serverless/pom.xml @@ -0,0 +1,202 @@ + + 4.0.0 + + software.amazon.lambda.examples + 1.16.1 + powertools-examples-core-serverless + jar + + Powertools for AWS Lambda (Java) library Examples - Core + + + 2.20.0 + 1.8 + 1.8 + true + + + + + software.amazon.lambda + powertools-tracing + ${project.version} + + + software.amazon.lambda + powertools-logging + ${project.version} + + + software.amazon.lambda + powertools-metrics + ${project.version} + + + com.amazonaws + aws-lambda-java-core + 1.2.2 + + + com.amazonaws + aws-lambda-java-events + 3.11.2 + + + org.apache.logging.log4j + log4j-core + ${log4j.version} + + + org.apache.logging.log4j + log4j-api + ${log4j.version} + + + + junit + junit + 4.13.2 + test + + + + + helloworld-lambda + + + dev.aspectj + aspectj-maven-plugin + 1.13.1 + + ${maven.compiler.source} + ${maven.compiler.target} + ${maven.compiler.target} + + + software.amazon.lambda + powertools-tracing + + + software.amazon.lambda + powertools-logging + + + software.amazon.lambda + powertools-metrics + + + + + + + compile + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.5.0 + + + package + + shade + + + + + + + + + + + + com.github.edwgiz + maven-shade-plugin.log4j2-cachefile-transformer + 2.15 + + + + + + + + + jdk8 + + (,11) + + + 1.9.7 + + + + + org.aspectj + aspectjtools + ${aspectj.version} + + + + + + + + dev.aspectj + aspectj-maven-plugin + ${aspectj.plugin.version} + + ${maven.compiler.source} + ${maven.compiler.target} + ${maven.compiler.target} + + + software.amazon.lambda + powertools-tracing + + + software.amazon.lambda + powertools-logging + + + software.amazon.lambda + powertools-metrics + + + + + + + compile + test-compile + + + + + + + org.aspectj + aspectjtools + ${aspectj.version} + + + + + + + + + diff --git a/examples/powertools-examples-core/serverless/serverless.yml b/examples/powertools-examples-core/serverless/serverless.yml new file mode 100644 index 000000000..5c89c0436 --- /dev/null +++ b/examples/powertools-examples-core/serverless/serverless.yml @@ -0,0 +1,114 @@ +# Welcome to Serverless! +# +# This file is the main config file for your service. +# It's very minimal at this point and uses default values. +# 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 + +# 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 + 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 + 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" diff --git a/examples/powertools-examples-core/serverless/src/main/java/helloworld/App.java b/examples/powertools-examples-core/serverless/src/main/java/helloworld/App.java new file mode 100644 index 000000000..fccc63b9a --- /dev/null +++ b/examples/powertools-examples-core/serverless/src/main/java/helloworld/App.java @@ -0,0 +1,107 @@ +/* + * Copyright 2023 Amazon.com, Inc. or its affiliates. + * Licensed under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package helloworld; + +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 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 java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Collectors; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import software.amazon.cloudwatchlogs.emf.model.DimensionSet; +import software.amazon.cloudwatchlogs.emf.model.Unit; +import software.amazon.lambda.powertools.logging.Logging; +import software.amazon.lambda.powertools.logging.LoggingUtils; +import software.amazon.lambda.powertools.metrics.Metrics; +import software.amazon.lambda.powertools.tracing.CaptureMode; +import software.amazon.lambda.powertools.tracing.Tracing; +import software.amazon.lambda.powertools.tracing.TracingUtils; + +/** + * Handler for requests to Lambda function. + */ +public class App implements RequestHandler { + private final static Logger log = LogManager.getLogger(App.class); + + @Logging(logEvent = true, samplingRate = 0.7) + @Tracing(captureMode = CaptureMode.RESPONSE_AND_ERROR) + @Metrics(namespace = "ServerlessAirline", service = "payment", captureColdStart = true) + public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) { + Map headers = new HashMap<>(); + + headers.put("Content-Type", "application/json"); + headers.put("X-Custom-Header", "application/json"); + + metricsLogger().putMetric("CustomMetric1", 1, Unit.COUNT); + + withSingleMetric("CustomMetrics2", 1, Unit.COUNT, "Another", (metric) -> + { + metric.setDimensions(DimensionSet.of("AnotherService", "CustomService")); + metric.setDimensions(DimensionSet.of("AnotherService1", "CustomService1")); + }); + + LoggingUtils.appendKey("test", "willBeLogged"); + + APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent() + .withHeaders(headers); + try { + final String pageContents = this.getPageContents("https://checkip.amazonaws.com"); + log.info(pageContents); + TracingUtils.putAnnotation("Test", "New"); + String output = String.format("{ \"message\": \"hello world\", \"location\": \"%s\" }", pageContents); + + TracingUtils.withSubsegment("loggingResponse", subsegment -> + { + String sampled = "log something out"; + log.info(sampled); + log.info(output); + }); + + log.info("After output"); + return response + .withStatusCode(200) + .withBody(output); + } catch (RuntimeException | IOException e) { + return response + .withBody("{}") + .withStatusCode(500); + } + } + + @Tracing + private void log() { + log.info("inside threaded logging for function"); + } + + @Tracing(namespace = "getPageContents", captureMode = CaptureMode.DISABLED) + private String getPageContents(String address) throws IOException { + URL url = new URL(address); + putMetadata("getPageContents", address); + try (BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()))) { + return br.lines().collect(Collectors.joining(System.lineSeparator())); + } + } +} diff --git a/examples/powertools-examples-core/serverless/src/main/java/helloworld/AppStream.java b/examples/powertools-examples-core/serverless/src/main/java/helloworld/AppStream.java new file mode 100644 index 000000000..401ef8c48 --- /dev/null +++ b/examples/powertools-examples-core/serverless/src/main/java/helloworld/AppStream.java @@ -0,0 +1,38 @@ +/* + * Copyright 2023 Amazon.com, Inc. or its affiliates. + * Licensed under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package helloworld; + +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestStreamHandler; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Map; +import software.amazon.lambda.powertools.logging.Logging; +import software.amazon.lambda.powertools.metrics.Metrics; + +public class AppStream implements RequestStreamHandler { + private static final ObjectMapper mapper = new ObjectMapper(); + + @Override + @Logging(logEvent = true) + @Metrics(namespace = "ServerlessAirline", service = "payment", captureColdStart = true) + public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException { + Map map = mapper.readValue(input, Map.class); + + System.out.println(map.size()); + } +} diff --git a/examples/powertools-examples-core/serverless/src/main/resources/log4j2.xml b/examples/powertools-examples-core/serverless/src/main/resources/log4j2.xml new file mode 100644 index 000000000..e1fd14cea --- /dev/null +++ b/examples/powertools-examples-core/serverless/src/main/resources/log4j2.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/examples/powertools-examples-core/serverless/src/test/java/helloworld/AppTest.java b/examples/powertools-examples-core/serverless/src/test/java/helloworld/AppTest.java new file mode 100644 index 000000000..70dad8d71 --- /dev/null +++ b/examples/powertools-examples-core/serverless/src/test/java/helloworld/AppTest.java @@ -0,0 +1,59 @@ +/* + * Copyright 2023 Amazon.com, Inc. or its affiliates. + * Licensed under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package helloworld; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; +import com.amazonaws.xray.AWSXRay; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class AppTest { + + @Before + public void setup() { + if (null == System.getenv("LAMBDA_TASK_ROOT")) { + AWSXRay.beginSegment("test"); + } + } + + @After + public void tearDown() { + if (AWSXRay.getCurrentSubsegmentOptional().isPresent()) { + AWSXRay.endSubsegment(); + } + + if (null == System.getenv("LAMBDA_TASK_ROOT")) { + AWSXRay.endSegment(); + } + } + + @Test + public void successfulResponse() { + App app = new App(); + APIGatewayProxyResponseEvent result = app.handleRequest(null, null); + assertEquals(result.getStatusCode().intValue(), 200); + assertEquals(result.getHeaders().get("Content-Type"), "application/json"); + String content = result.getBody(); + assertNotNull(content); + assertTrue(content.contains("\"message\"")); + assertTrue(content.contains("\"hello world\"")); + assertTrue(content.contains("\"location\"")); + } +} From c405cbb040a421004aefb881e9719303413b7a8e Mon Sep 17 00:00:00 2001 From: Alexey Soshin Date: Mon, 14 Aug 2023 15:05:33 +0100 Subject: [PATCH 02/19] Add memory size and timeouts, same as SAM examples --- examples/powertools-examples-core/serverless/serverless.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/powertools-examples-core/serverless/serverless.yml b/examples/powertools-examples-core/serverless/serverless.yml index 5c89c0436..817bd1508 100644 --- a/examples/powertools-examples-core/serverless/serverless.yml +++ b/examples/powertools-examples-core/serverless/serverless.yml @@ -60,6 +60,9 @@ functions: # 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 From 6896dbdb8168a00fae547bcb82cd672d927f5406 Mon Sep 17 00:00:00 2001 From: Alexey Soshin Date: Mon, 14 Aug 2023 15:05:55 +0100 Subject: [PATCH 03/19] Add useful Serverless commands --- .../serverless/README.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/examples/powertools-examples-core/serverless/README.md b/examples/powertools-examples-core/serverless/README.md index e9ea2a0ea..e822040db 100644 --- a/examples/powertools-examples-core/serverless/README.md +++ b/examples/powertools-examples-core/serverless/README.md @@ -17,14 +17,16 @@ It is a [Maven](https://maven.apache.org/) based project, so you can open this p The minimum to deploy the app should be ```bash -sls deploy +mvn package && sls deploy ``` - ## Useful commands +Deploy your entire stack +```bash +sls deploy +``` -* `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 \ No newline at end of file +Deploy a single function +```bash +sls deploy function -f hello +``` \ No newline at end of file From a4a6445f78037caa7b39da3c87b7c88c85550eec Mon Sep 17 00:00:00 2001 From: Alexey Soshin Date: Mon, 14 Aug 2023 15:41:37 +0100 Subject: [PATCH 04/19] Remove redundant function --- .../serverless/src/main/java/helloworld/App.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/examples/powertools-examples-core/serverless/src/main/java/helloworld/App.java b/examples/powertools-examples-core/serverless/src/main/java/helloworld/App.java index fccc63b9a..ba72e5aac 100644 --- a/examples/powertools-examples-core/serverless/src/main/java/helloworld/App.java +++ b/examples/powertools-examples-core/serverless/src/main/java/helloworld/App.java @@ -91,11 +91,6 @@ public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEv } } - @Tracing - private void log() { - log.info("inside threaded logging for function"); - } - @Tracing(namespace = "getPageContents", captureMode = CaptureMode.DISABLED) private String getPageContents(String address) throws IOException { URL url = new URL(address); From 3a2e6e4ad133e1b22493d2c3bdcc73ed76093741 Mon Sep 17 00:00:00 2001 From: Alexey Soshin Date: Wed, 16 Aug 2023 13:56:06 +0100 Subject: [PATCH 05/19] Update examples/powertools-examples-core/serverless/README.md Co-authored-by: Scott Gerring --- examples/powertools-examples-core/serverless/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/powertools-examples-core/serverless/README.md b/examples/powertools-examples-core/serverless/README.md index e822040db..578e42495 100644 --- a/examples/powertools-examples-core/serverless/README.md +++ b/examples/powertools-examples-core/serverless/README.md @@ -2,7 +2,7 @@ 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) +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) From 8e59d860a6e0f6ed7860d63c77ac5f95df7383cf Mon Sep 17 00:00:00 2001 From: Alexey Soshin Date: Wed, 16 Aug 2023 13:56:18 +0100 Subject: [PATCH 06/19] Update examples/powertools-examples-core/serverless/README.md Co-authored-by: Scott Gerring --- examples/powertools-examples-core/serverless/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/powertools-examples-core/serverless/README.md b/examples/powertools-examples-core/serverless/README.md index 578e42495..43ca716b2 100644 --- a/examples/powertools-examples-core/serverless/README.md +++ b/examples/powertools-examples-core/serverless/README.md @@ -3,7 +3,6 @@ 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 From c2b20933be9f2ee561d0f1bb558f3f18e1024c46 Mon Sep 17 00:00:00 2001 From: Alexey Soshin Date: Wed, 16 Aug 2023 13:56:35 +0100 Subject: [PATCH 07/19] Update examples/powertools-examples-core/serverless/README.md Co-authored-by: Scott Gerring --- examples/powertools-examples-core/serverless/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/powertools-examples-core/serverless/README.md b/examples/powertools-examples-core/serverless/README.md index 43ca716b2..43cb501e2 100644 --- a/examples/powertools-examples-core/serverless/README.md +++ b/examples/powertools-examples-core/serverless/README.md @@ -1,7 +1,6 @@ # 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) From 203298a8440c2efd2265fc91ee64600ca64108b0 Mon Sep 17 00:00:00 2001 From: Alexey Soshin Date: Wed, 16 Aug 2023 13:56:44 +0100 Subject: [PATCH 08/19] Update examples/powertools-examples-core/serverless/README.md Co-authored-by: Scott Gerring --- examples/powertools-examples-core/serverless/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/powertools-examples-core/serverless/README.md b/examples/powertools-examples-core/serverless/README.md index 43cb501e2..782ccfd67 100644 --- a/examples/powertools-examples-core/serverless/README.md +++ b/examples/powertools-examples-core/serverless/README.md @@ -13,7 +13,7 @@ It is a [Maven](https://maven.apache.org/) based project, so you can open this p ## Deploy the sample application -The minimum to deploy the app should be +To deploy the app, simply run the following commands: ```bash mvn package && sls deploy ``` From 134b9deb830d5dd0e526de5098ecfa87377899cb Mon Sep 17 00:00:00 2001 From: Alexey Soshin Date: Fri, 18 Aug 2023 10:30:21 +0100 Subject: [PATCH 09/19] Update examples/powertools-examples-core/serverless/README.md Co-authored-by: Scott Gerring --- examples/powertools-examples-core/serverless/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/powertools-examples-core/serverless/README.md b/examples/powertools-examples-core/serverless/README.md index 782ccfd67..5b440bdbf 100644 --- a/examples/powertools-examples-core/serverless/README.md +++ b/examples/powertools-examples-core/serverless/README.md @@ -2,7 +2,7 @@ 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) +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. From 4f6eaeeb2d02c46ea5af07669fac59484ed48651 Mon Sep 17 00:00:00 2001 From: Alexey Soshin Date: Fri, 18 Aug 2023 10:31:23 +0100 Subject: [PATCH 10/19] Remove reduntant command from README --- examples/powertools-examples-core/serverless/README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/examples/powertools-examples-core/serverless/README.md b/examples/powertools-examples-core/serverless/README.md index 5b440bdbf..aec093182 100644 --- a/examples/powertools-examples-core/serverless/README.md +++ b/examples/powertools-examples-core/serverless/README.md @@ -19,10 +19,6 @@ mvn package && sls deploy ``` ## Useful commands -Deploy your entire stack -```bash -sls deploy -``` Deploy a single function ```bash From 0de614fc35fb6f10df2ff30c1cdfac02d1990af3 Mon Sep 17 00:00:00 2001 From: Alexey Soshin Date: Fri, 18 Aug 2023 10:35:29 +0100 Subject: [PATCH 11/19] Move most of Powertools configuration to the service-wide environment --- .../serverless/serverless.yml | 24 ++++--------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/examples/powertools-examples-core/serverless/serverless.yml b/examples/powertools-examples-core/serverless/serverless.yml index 817bd1508..baa975ee8 100644 --- a/examples/powertools-examples-core/serverless/serverless.yml +++ b/examples/powertools-examples-core/serverless/serverless.yml @@ -1,16 +1,3 @@ -# Welcome to Serverless! -# -# This file is the main config file for your service. -# It's very minimal at this point and uses default values. -# 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 @@ -47,8 +34,11 @@ provider: # - "/*" # you can define service wide environment variables here -# environment: -# variable1: value1 + environment: + POWERTOOLS_LOG_LEVEL: INFO + POWERTOOLS_LOGGER_SAMPLE_RATE: 0.1 + POWERTOOLS_LOGGER_LOG_EVENT: true + POWERTOOLS_METRICS_NAMESPACE: Coreutilities # you can add packaging information here package: @@ -98,10 +88,6 @@ functions: # 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 POWERTOOLS_SERVICE_NAME: hello # you can add CloudFormation resource templates here From 1b9d148a198c505b9b8bab37404e902869add77b Mon Sep 17 00:00:00 2001 From: Alexey Soshin Date: Thu, 31 Aug 2023 15:56:15 +0100 Subject: [PATCH 12/19] Remove more generated comments --- .../serverless/serverless.yml | 66 +------------------ 1 file changed, 2 insertions(+), 64 deletions(-) diff --git a/examples/powertools-examples-core/serverless/serverless.yml b/examples/powertools-examples-core/serverless/serverless.yml index baa975ee8..f37faf65d 100644 --- a/examples/powertools-examples-core/serverless/serverless.yml +++ b/examples/powertools-examples-core/serverless/serverless.yml @@ -15,24 +15,6 @@ provider: # 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: POWERTOOLS_LOG_LEVEL: INFO @@ -47,9 +29,6 @@ package: 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" @@ -57,47 +36,6 @@ functions: - 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 +# Define function environment variables here environment: - 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" + POWERTOOLS_SERVICE_NAME: hello \ No newline at end of file From 86e9744c319eaf5af993309147d32167c8677f5c Mon Sep 17 00:00:00 2001 From: Alexey Soshin Date: Mon, 4 Sep 2023 15:08:29 +0100 Subject: [PATCH 13/19] Add newlines --- examples/powertools-examples-core/serverless/serverless.yml | 2 +- .../serverless/src/main/resources/log4j2.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/powertools-examples-core/serverless/serverless.yml b/examples/powertools-examples-core/serverless/serverless.yml index f37faf65d..56abd9a4d 100644 --- a/examples/powertools-examples-core/serverless/serverless.yml +++ b/examples/powertools-examples-core/serverless/serverless.yml @@ -38,4 +38,4 @@ functions: method: get # Define function environment variables here environment: - POWERTOOLS_SERVICE_NAME: hello \ No newline at end of file + POWERTOOLS_SERVICE_NAME: hello diff --git a/examples/powertools-examples-core/serverless/src/main/resources/log4j2.xml b/examples/powertools-examples-core/serverless/src/main/resources/log4j2.xml index e1fd14cea..0cc0953f0 100644 --- a/examples/powertools-examples-core/serverless/src/main/resources/log4j2.xml +++ b/examples/powertools-examples-core/serverless/src/main/resources/log4j2.xml @@ -13,4 +13,4 @@ - \ No newline at end of file + From b96bb9043608d1fdf227eb830150dcbbdba42ac3 Mon Sep 17 00:00:00 2001 From: Alexey Soshin Date: Mon, 4 Sep 2023 15:21:54 +0100 Subject: [PATCH 14/19] Comment out annotations that are preceded by environment variables --- examples/README.md | 1 + .../serverless/src/main/java/helloworld/App.java | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/README.md b/examples/README.md index 0744c2bb1..52dc0c1e9 100644 --- a/examples/README.md +++ b/examples/README.md @@ -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 diff --git a/examples/powertools-examples-core/serverless/src/main/java/helloworld/App.java b/examples/powertools-examples-core/serverless/src/main/java/helloworld/App.java index ba72e5aac..dacd7f1d4 100644 --- a/examples/powertools-examples-core/serverless/src/main/java/helloworld/App.java +++ b/examples/powertools-examples-core/serverless/src/main/java/helloworld/App.java @@ -46,9 +46,12 @@ public class App implements RequestHandler { private final static Logger log = LogManager.getLogger(App.class); - @Logging(logEvent = true, samplingRate = 0.7) + // This is controlled by POWERTOOLS_LOGGER_SAMPLE_RATE environment variable + // @Logging(logEvent = true, samplingRate = 0.7) + // This is controlled by POWERTOOLS_METRICS_NAMESPACE environment variable + // @Metrics(namespace = "ServerlessAirline", service = "payment", captureColdStart = true) + // This is controlled by POWERTOOLS_TRACER_CAPTURE_ERROR environment variable @Tracing(captureMode = CaptureMode.RESPONSE_AND_ERROR) - @Metrics(namespace = "ServerlessAirline", service = "payment", captureColdStart = true) public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) { Map headers = new HashMap<>(); From 20fd3b9778b1d6d4eb14158e3ca3fca58610fb28 Mon Sep 17 00:00:00 2001 From: Alexey Soshin Date: Mon, 4 Sep 2023 15:28:24 +0100 Subject: [PATCH 15/19] Add link to the Serverless example --- examples/powertools-examples-core/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/powertools-examples-core/README.md b/examples/powertools-examples-core/README.md index d690b01c5..1d1dd031f 100644 --- a/examples/powertools-examples-core/README.md +++ b/examples/powertools-examples-core/README.md @@ -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: From 5471f77ffdd59a5267dca67a9e3993deb0746f1f Mon Sep 17 00:00:00 2001 From: Alexey Soshin Date: Wed, 27 Sep 2023 09:37:50 +0100 Subject: [PATCH 16/19] Update examples/powertools-examples-core/serverless/pom.xml MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jérôme Van Der Linden <117538+jeromevdl@users.noreply.github.com> --- examples/powertools-examples-core/serverless/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/powertools-examples-core/serverless/pom.xml b/examples/powertools-examples-core/serverless/pom.xml index 85052554b..87561dd0b 100644 --- a/examples/powertools-examples-core/serverless/pom.xml +++ b/examples/powertools-examples-core/serverless/pom.xml @@ -3,7 +3,7 @@ 4.0.0 software.amazon.lambda.examples - 1.16.1 + 1.18.0-SNAPSHOT powertools-examples-core-serverless jar From 3e6e889fe7d130a99a3e13e61a6a85da484ffb71 Mon Sep 17 00:00:00 2001 From: Alexey Soshin Date: Wed, 27 Sep 2023 09:38:04 +0100 Subject: [PATCH 17/19] Update examples/powertools-examples-core/serverless/pom.xml MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jérôme Van Der Linden <117538+jeromevdl@users.noreply.github.com> --- examples/powertools-examples-core/serverless/pom.xml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/examples/powertools-examples-core/serverless/pom.xml b/examples/powertools-examples-core/serverless/pom.xml index 87561dd0b..516a90e66 100644 --- a/examples/powertools-examples-core/serverless/pom.xml +++ b/examples/powertools-examples-core/serverless/pom.xml @@ -122,6 +122,14 @@ + + + org.apache.maven.plugins + maven-deploy-plugin + + true + + From 02874523259ac11cf30fca181e39c59698c4fccb Mon Sep 17 00:00:00 2001 From: Alexey Soshin Date: Wed, 27 Sep 2023 09:38:14 +0100 Subject: [PATCH 18/19] Update examples/powertools-examples-core/serverless/pom.xml MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jérôme Van Der Linden <117538+jeromevdl@users.noreply.github.com> --- examples/powertools-examples-core/serverless/pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/powertools-examples-core/serverless/pom.xml b/examples/powertools-examples-core/serverless/pom.xml index 516a90e66..793318da3 100644 --- a/examples/powertools-examples-core/serverless/pom.xml +++ b/examples/powertools-examples-core/serverless/pom.xml @@ -13,7 +13,6 @@ 2.20.0 1.8 1.8 - true From 7e66b9de0c9ab3d79fc3582397a3aba4a8cb5e6d Mon Sep 17 00:00:00 2001 From: Alexey Soshin Date: Wed, 27 Sep 2023 09:40:27 +0100 Subject: [PATCH 19/19] Remove POWERTOOLS_LOGGER_LOG_EVENT, since it's unsupported --- examples/powertools-examples-core/serverless/serverless.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/powertools-examples-core/serverless/serverless.yml b/examples/powertools-examples-core/serverless/serverless.yml index 56abd9a4d..d8dec8080 100644 --- a/examples/powertools-examples-core/serverless/serverless.yml +++ b/examples/powertools-examples-core/serverless/serverless.yml @@ -19,7 +19,6 @@ provider: environment: POWERTOOLS_LOG_LEVEL: INFO POWERTOOLS_LOGGER_SAMPLE_RATE: 0.1 - POWERTOOLS_LOGGER_LOG_EVENT: true POWERTOOLS_METRICS_NAMESPACE: Coreutilities # you can add packaging information here