diff --git a/docs/index.md b/docs/index.md index f00e0314d..d3e487174 100644 --- a/docs/index.md +++ b/docs/index.md @@ -253,7 +253,7 @@ Depending on your version of Java (either Java 1.8 or 11+), the configuration sl ``` ???+ tip "Why a different configuration?" - Lambda Powertools for Java is using [AspectJ](https://eclipse.dev/aspectj/doc/released/progguide/starting.html) internally + Powertools for AWS Lambda (Java) is using [AspectJ](https://eclipse.dev/aspectj/doc/released/progguide/starting.html) internally to handle annotations. Recently, in order to support Java 17 we had to move to `dev.aspectj:aspectj-maven-plugin` because `org.codehaus.mojo:aspectj-maven-plugin` does not support Java 17. Under the hood, `org.codehaus.mojo:aspectj-maven-plugin` is based on AspectJ 1.9.7, diff --git a/examples/README.md b/examples/README.md index f7e6fc620..1869b4e8f 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,4 +1,62 @@ -## aws-lambda-powertools-examples +# Powertools for AWS Lambda (Java) Examples This directory holds example projects demoing different components of the Powertools for AWS Lambda (Java). -Each example can be copied from its subdirectory and used independently of the rest of this repository. \ No newline at end of file +Each example can be copied from its subdirectory and used independently of the rest of this repository. + +## The Examples + +* [powertools-examples-core](powertools-examples-core) - Demonstrates the core logging, tracing, and metrics modules +* [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 +* [powertools-examples-sqs](powertools-examples-sqs) - Processes SQS batch requests +* [powertools-examples-validation](powertools-examples-validation) - Uses the validation module to validate user requests received via API Gateway +* [powertools-examples-cloudformation](powertools-examples-cloudformation) - Deploys a Cloudformation custom resource + +## Working with AWS Serverless Application Model (SAM) Examples +Many of the examples use [AWS Serverless Application Model](https://aws.amazon.com/serverless/sam/) (SAM). To get started +with them, you can use the SAM Command Line Interface (SAM CLI) to build it and deploy an example to AWS. + +To use the SAM CLI, you need the following tools. + +* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) +* Java11 - [Install the Java 11](https://docs.aws.amazon.com/corretto/latest/corretto-11-ug/downloads-list.html) +* Maven - [Install Maven](https://maven.apache.org/install.html) +* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community) + +To learn more about SAM, +[check out the developer guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/using-sam-cli.html). +You can use the CLI to [test events locally](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/using-sam-cli-local-invoke.html), +and [run the application locally](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/using-sam-cli-local-start-api.html), +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 + +# Build and deploy the example +$ sam build +$ sam deploy --guided +``` + +The first command will build the source of your application. The second command will package and deploy your application to AWS, with a series of prompts: + +* **Stack Name**: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name. +* **AWS Region**: The AWS region you want to deploy your app to. +* **Confirm changes before deploy**: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes. +* **Allow SAM CLI IAM role creation**: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modified IAM roles, the `CAPABILITY_IAM` value for `capabilities` must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass `--capabilities CAPABILITY_IAM` to the `sam deploy` command. +* **Save arguments to samconfig.toml**: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run `sam deploy` without parameters to deploy changes to your application. + +You can find your API Gateway Endpoint URL in the output values displayed after deployment. + +### SAM - Other Tools + +If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit. +The AWS Toolkit is an open source plug-in for popular IDEs that uses the SAM CLI to build and deploy serverless applications on AWS. The AWS Toolkit also adds a simplified step-through debugging experience for Lambda function code. See the following links to get started. + +* [PyCharm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [IntelliJ](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [VS Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html) +* [Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/welcome.html) \ No newline at end of file diff --git a/examples/powertools-examples-cloudformation/README.md b/examples/powertools-examples-cloudformation/README.md index 6dbffcf37..4b53ff0aa 100644 --- a/examples/powertools-examples-cloudformation/README.md +++ b/examples/powertools-examples-cloudformation/README.md @@ -1,4 +1,4 @@ -# Cloudformation Custom Resource Example +# Powertools for AWS Lambda (Java) - Cloudformation Custom Resource Example This project contains an example of Lambda function using the CloudFormation module of Powertools for AWS Lambda in Java. For more information on this module, please refer to the [documentation](https://awslabs.github.io/aws-lambda-powertools-java/utilities/custom_resources/). @@ -7,20 +7,7 @@ This project contains an example of Lambda function using the CloudFormation mod This sample can be used either with the Serverless Application Model (SAM) or with CDK. ### Deploy with SAM CLI -To use the SAM CLI, you need the following tools. - -* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) -* Java 8 - [Install Java 8](https://docs.aws.amazon.com/corretto/latest/corretto-8-ug/downloads-list.html) -* Maven - [Install Maven](https://maven.apache.org/install.html) -* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community) - -To build and deploy this application for the first time, run the following in your shell: - -```bash -cd infra/sam -sam build -sam deploy --guided --parameter-overrides BucketNameParam=my-unique-bucket-20230717 -``` +To deploy it using the SAM CLI, check out the instructions for getting started in [the examples directory](../README.md) ### Deploy with CDK To use CDK you need the following tools. diff --git a/examples/powertools-examples-core/README.md b/examples/powertools-examples-core/README.md index ba8859027..a47d0d26c 100644 --- a/examples/powertools-examples-core/README.md +++ b/examples/powertools-examples-core/README.md @@ -1,138 +1,47 @@ -# CoreUtilities +# Powertools for AWS Lambda (Java) - Core Utilities Example -This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes [Powertools for AWS Lambda (Java) for operational best practices](https://github.com/aws-powertools/powertools-lambda-java), and the following files and folders. +This project demonstrates the Lambda for Powertools Java module - including +[logging](https://docs.powertools.aws.dev/lambda/java/core/logging/), +[tracing](https://docs.powertools.aws.dev/lambda/java/core/tracing/), and +[metrics](https://docs.powertools.aws.dev/lambda/java/core/metrics/). -- HelloWorldFunction/src/main - Code for the application's Lambda function. -- events - Invocation events that you can use to invoke the function. -- HelloWorldFunction/src/test - Unit tests for the application code. -- template.yaml - A template that defines the application's AWS resources. +It is made up of the following: -The application uses several AWS resources, including Lambda functions and an API Gateway API. These resources are defined in the `template.yaml` file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code. - -If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit. -The AWS Toolkit is an open source plug-in for popular IDEs that uses the SAM CLI to build and deploy serverless applications on AWS. The AWS Toolkit also adds a simplified step-through debugging experience for Lambda function code. See the following links to get started. - -* [PyCharm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) -* [IntelliJ](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) -* [VS Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html) -* [Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/welcome.html) +- [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. ## Deploy the sample application -The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API. - -To use the SAM CLI, you need the following tools. - -* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) -* Java11 - [Install the Java 11](https://docs.aws.amazon.com/corretto/latest/corretto-11-ug/downloads-list.html) -* Maven - [Install Maven](https://maven.apache.org/install.html) -* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community) - -To build and deploy your application for the first time, run the following in your shell: - -```bash -Coreutilities$ sam build -Coreutilities$ sam deploy --guided -``` - -The first command will build the source of your application. The second command will package and deploy your application to AWS, with a series of prompts: - -* **Stack Name**: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name. -* **AWS Region**: The AWS region you want to deploy your app to. -* **Confirm changes before deploy**: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes. -* **Allow SAM CLI IAM role creation**: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modified IAM roles, the `CAPABILITY_IAM` value for `capabilities` must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass `--capabilities CAPABILITY_IAM` to the `sam deploy` command. -* **Save arguments to samconfig.toml**: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run `sam deploy` without parameters to deploy changes to your application. - -You can find your API Gateway Endpoint URL in the output values displayed after deployment. - -## Use the SAM CLI to build and test locally - -Build your application with the `sam build` command. - -```bash -Coreutilities$ sam build -``` - -The SAM CLI installs dependencies defined in `HelloWorldFunction/pom.xml`, creates a deployment package, and saves it in the `.aws-sam/build` folder. - -Test a single function by invoking it directly with a test event. An event is a JSON document that represents the input that the function receives from the event source. Test events are included in the `events` folder in this project. - -Run functions locally and invoke them with the `sam local invoke` command. - -```bash -Coreutilities$ sam local invoke HelloWorldFunction --event events/event.json -``` - -The SAM CLI can also emulate your application's API. Use the `sam local start-api` to run the API locally on port 3000. - -```bash -Coreutilities$ sam local start-api -Coreutilities$ curl http://localhost:3000/ -``` - -The SAM CLI reads the application template to determine the API's routes and the functions that they invoke. The `Events` property on each function's definition includes the route and method for each path. - -```yaml - Events: - HelloWorld: - Type: Api - Properties: - Path: /hello - Method: get -``` - -## Add a resource to your application -The application template uses AWS Serverless Application Model (AWS SAM) to define application resources. AWS SAM is an extension of AWS CloudFormation with a simpler syntax for configuring common serverless application resources such as functions, triggers, and APIs. For resources not included in [the SAM specification](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md), you can use standard [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html) resource types. +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) -## Fetch, tail, and filter Lambda function logs +## Test the application -To simplify troubleshooting, SAM CLI has a command called `sam logs`. `sam logs` lets you fetch logs generated by your deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find the bug. - -`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM. +Once the app is deployed, you can invoke the endpoint like this: ```bash -Coreutilities$ sam logs -n HelloWorldFunction --stack-name --tail + curl https://[REST-API-ID].execute-api.[REGION].amazonaws.com/Prod/hello/ ``` -You can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html). - -## Unit tests - -Tests are defined in the `HelloWorldFunction/src/test` folder in this project. - -```bash -Coreutilities$ cd HelloWorldFunction -HelloWorldFunction$ mvn test -``` +The response itself isn't particularly interesting - you will get back some information about your IP address. If +you go to the Lambda Console and locate the lambda you have deployed, then click the "Monitoring" tab you will +be able to find: -## Cleanup +* **View X-Ray traces** - Display the traces captured by the traces module. These include subsegments for the +different function calls within the example +* **View Cloudwatch logs** - Display the structured logging output of the example -To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following: +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 -aws cloudformation delete-stack --stack-name -``` - -# Appendix - -## Powertools - -**Tracing** - -[Tracing utility](https://docs.powertools.aws.dev/lambda-java/core/tracing/) provides functionality to reduce the overhead of performing common tracing tasks. It traces the execution of this sample code including the response and exceptions as tracing metadata - You can visualize them in AWS X-Ray. - -**Logger** - -[Logging utility](https://docs.powertools.aws.dev/lambda-java/core/logging/) creates an opinionated application Logger with structured logging as the output, dynamically samples a percentage (samplingRate) of your logs in DEBUG mode for concurrent invocations, log incoming events as your function is invoked, and injects key information from Lambda context object into your Logger - You can visualize them in Amazon CloudWatch Logs. - -**Metrics** - -[Metrics utility](https://docs.powertools.aws.dev/lambda-java/core/metrics/) captures cold start metric of your Lambda invocation, and could add additional metrics to help you understand your application KPIs - You can visualize them in Amazon CloudWatch. - -## Resources - -See the [AWS SAM developer guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) for an introduction to SAM specification, the SAM CLI, and serverless application concepts. - -Check the [Powertools for AWS Lambda (Java)](https://docs.powertools.aws.dev/lambda-java/) for more information on how to use and configure such tools +# Tail the logs +sam logs --tail $MY_STACK -Next, you can use AWS Serverless Application Repository to deploy ready to use Apps that go beyond hello world samples and learn how authors developed their applications: [AWS Serverless Application Repository main page](https://aws.amazon.com/serverless/serverlessrepo/) +# Tail the traces +sam traces --tail +``` \ No newline at end of file diff --git a/examples/powertools-examples-idempotency/README.md b/examples/powertools-examples-idempotency/README.md index 278484a92..6f6022054 100644 --- a/examples/powertools-examples-idempotency/README.md +++ b/examples/powertools-examples-idempotency/README.md @@ -1,21 +1,22 @@ -# Idempotency +# Powertools for AWS Lambda (Java) - Idempotency Example This project contains an example of Lambda function using the idempotency module of Powertools for AWS Lambda (Java). For more information on this module, please refer to the [documentation](https://docs.powertools.aws.dev/lambda-java/utilities/idempotency/). +The example exposes a HTTP POST endpoint. When the user sends the address of a webpage to it, the endpoint fetches the contents of the URL and returns them to the user: ## Deploy the sample application -This sample is based on Serverless Application Model (SAM) and you can use the SAM Command Line Interface (SAM CLI) to build it and deploy it to AWS. +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) -To use the SAM CLI, you need the following tools. - -* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) -* Java11 - [Install the Java 11](https://docs.aws.amazon.com/corretto/latest/corretto-11-ug/downloads-list.html) -* Maven - [Install Maven](https://maven.apache.org/install.html) -* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community) - -To build and deploy your application for the first time, run the following in your shell: +## Test the application ```bash -Coreutilities$ sam build -Coreutilities$ sam deploy --guided + curl -X POST https://[REST-API-ID].execute-api.[REGION].amazonaws.com/Prod/helloidem/ -H "Content-Type: application/json" -d '{"address": "https://checkip.amazonaws.com"}' ``` + +this should return the contents of the webpage, for instance: +```json +{ "message": "hello world", "location": "123.123.123.1" } +``` + +Check out [App.java](src/main/java/helloworld/App.java) to see how it works! diff --git a/examples/powertools-examples-idempotency/src/main/java/helloworld/App.java b/examples/powertools-examples-idempotency/src/main/java/helloworld/App.java index f26877c34..0a20aa3a4 100644 --- a/examples/powertools-examples-idempotency/src/main/java/helloworld/App.java +++ b/examples/powertools-examples-idempotency/src/main/java/helloworld/App.java @@ -43,7 +43,11 @@ public App(DynamoDbClient client) { } /** - * Try with: + * This is our Lambda event handler. It accepts HTTP POST requests from API gateway and returns the contents of the given URL. Requests are made idempotent + * by the idempotency library, and results are cached for the default 1h expiry time. + * + * You can test the endpoint like this: + * *
      *     curl -X POST https://[REST-API-ID].execute-api.[REGION].amazonaws.com/Prod/helloidem/ -H "Content-Type: application/json" -d '{"address": "https://checkip.amazonaws.com"}'
      * 
@@ -52,7 +56,7 @@ public App(DynamoDbClient client) { *
  • Second call (and next ones) will retrieve from the cache (if cache is enabled, which is by default) or from the store, the handler won't be called. Until the expiration happens (by default 1 hour).
  • * */ - @Idempotent // *** THE MAGIC IS HERE *** + @Idempotent // The magic is here! @Logging(logEvent = true) public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) { Map headers = new HashMap<>(); @@ -65,6 +69,7 @@ public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEv APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent() .withHeaders(headers); try { + // Read the 'address' field from the JSON post body String address = JsonConfig.get().getObjectMapper().readTree(input.getBody()).get("address").asText(); final String pageContents = this.getPageContents(address); String output = String.format("{ \"message\": \"hello world\", \"location\": \"%s\" }", pageContents); @@ -81,8 +86,18 @@ public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEv } } - // we could also put the @Idempotent annotation here, but using it on the handler avoids executing the handler (cost reduction). - // Use it on other methods to handle multiple items (with SQS batch processing for example) + + /** + * Helper to retrieve the contents of the given URL and return them as a string. + * + * We could also put the @Idempotent annotation here if we only wanted this sub-operation to be idempotent. Putting + * it on the handler, however, reduces total execution time and saves us time! + * + * @param address The URL to fetch + * @return The contents of the given URL + * + * @throws IOException + */ private String getPageContents(String address) throws IOException { URL url = new URL(address); try (BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) { diff --git a/examples/powertools-examples-parameters/README.md b/examples/powertools-examples-parameters/README.md index 0f843d455..a65307f69 100644 --- a/examples/powertools-examples-parameters/README.md +++ b/examples/powertools-examples-parameters/README.md @@ -1,21 +1,38 @@ -# Parameters +# Powertools for AWS Lambda (Java) - Parameters Example This project contains an example of Lambda function using the parameters module of Powertools for AWS Lambda (Java). For more information on this module, please refer to the [documentation](https://docs.powertools.aws.dev/lambda-java/utilities/parameters/). +The example uses the [SSM Parameter Store](https://docs.powertools.aws.dev/lambda/java/utilities/parameters/#ssm-parameter-store) +and the [Secrets Manager](https://docs.powertools.aws.dev/lambda/java/utilities/parameters/#secrets-manager) to inject +runtime parameters into the application. +Have a look at [ParametersFunction.java](src/main/java/org/demo/parameters/ParametersFunction.java) for the full details. + ## Deploy the sample application -This sample is based on Serverless Application Model (SAM) and you can use the SAM Command Line Interface (SAM CLI) to build it and deploy it to AWS. +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) -To use the SAM CLI, you need the following tools. +## Test the application -* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) -* Java11 - [Install the Java 11](https://docs.aws.amazon.com/corretto/latest/corretto-11-ug/downloads-list.html) -* Maven - [Install Maven](https://maven.apache.org/install.html) -* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community) +First, hit the URL of the application. You can do this with curl or your browser: -To build and deploy your application for the first time, run the following in your shell: +```bash + curl https://[REST-API-ID].execute-api.[REGION].amazonaws.com/Prod/params/ +``` +You will get your IP address back. The contents of the logs will be more interesting, and show you the values +of the parameters injected into the handler: ```bash -sam build -sam deploy --guided +sam logs --stack-name $MY_STACK_NAME --tail +``` + +```json +{ + ... + "thread": "main", + "level": "INFO", + "loggerName": "org.demo.parameters.ParametersFunction", + "message": "secretjsonobj=MyObject{id=23443, code='hk38543oj24kn796kp67bkb234gkj679l68'}\n", + ... +} ``` diff --git a/examples/powertools-examples-parameters/template.yaml b/examples/powertools-examples-parameters/template.yaml index 052cfcdc7..9d3bf8b0e 100644 --- a/examples/powertools-examples-parameters/template.yaml +++ b/examples/powertools-examples-parameters/template.yaml @@ -19,6 +19,9 @@ Resources: Handler: org.demo.parameters.ParametersFunction::handleRequest MemorySize: 512 Tracing: Active + Environment: + Variables: + LOG_LEVEL: INFO Policies: - AWSSecretsManagerGetSecretValuePolicy: SecretArn: !Ref UserPwd diff --git a/examples/powertools-examples-serialization/README.md b/examples/powertools-examples-serialization/README.md index 7f70da1f2..4e3f66eb0 100644 --- a/examples/powertools-examples-serialization/README.md +++ b/examples/powertools-examples-serialization/README.md @@ -1,21 +1,77 @@ -# Deserialization +# Powertools for AWS Lambda (Java) - Serialization Example This project contains an example of Lambda function using the serialization utilities module of Powertools for AWS Lambda (Java). For more information on this module, please refer to the [documentation](https://docs.powertools.aws.dev/lambda-java/utilities/serialization/). +The project contains two `RequestHandler`s - + +* [APIGatewayRequestDeserializationFunction](src/main/java/org/demo/serialization/APIGatewayRequestDeserializationFunction.java) - Uses the serialization library to deserialize an API Gateway request body +* [SQSEventDeserializationFunction](src/main/java/org/demo/serialization/SQSEventDeserializationFunction.java) - Uses the serialization library to deserialize an SQS message body + +In both cases, the output of the serialized message will be printed to the function logs. The message format +in JSON looks like this: + +```json +{ + "id":1234, + "name":"product", + "price":42 +} +``` + ## Deploy the sample application -This sample is based on Serverless Application Model (SAM) and you can use the SAM Command Line Interface (SAM CLI) to build it and deploy it to AWS. +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) + +## Test the application + +### 1. API Gateway Endpoint + +To test the HTTP endpoint, we can post a product to the test URL: + +```bash +curl -X POST https://[REST-API-ID].execute-api.[REGION].amazonaws.com/Prod/product/ -H "Content-Type: application/json" -d '{"id": 1234, "name": "product", "price": 42}' +``` + +The result will indicate that the handler has successfully deserialized the request body: -To use the SAM CLI, you need the following tools. +``` +Received request for productId: 1234 +``` + +If we look at the logs using `sam logs --tail --stack-name $MY_STACK`, we will see the full deserialized request: + +```json +{ + ... + "level": "INFO", + "loggerName": "org.demo.serialization.APIGatewayRequestDeserializationFunction", + "message": "product=Product{id=1234, name='product', price=42.0}\n", + ... +} +``` + +### 2. SQS Queue +For the SQS handler, we have to send a request to our queue. We can either construct the Queue URL (see below), or +find it from the SQS section of the AWS console. -* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) -* Java11 - [Install the Java 11](https://docs.aws.amazon.com/corretto/latest/corretto-11-ug/downloads-list.html) -* Maven - [Install Maven](https://maven.apache.org/install.html) -* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community) +```bash + aws sqs send-message --queue-url "https://sqs.[REGION].amazonaws.com/[ACCOUNT-ID]/sqs-event-deserialization-queue" --message-body '{"id": 1234, "name": "product", "price": 123}' +``` + +Here we can find the message by filtering through the logs for messages that have come back from our SQS handler: -To build and deploy your application for the first time, run the following in your shell: +```bash +sam logs --tail --stack-name $MY_STACK --filter SQS +``` ```bash -sam build -sam deploy --guided + { + ... + "level": "INFO", + "loggerName": "org.demo.serialization.SQSEventDeserializationFunction", + "message": "products=[Product{id=1234, name='product', price=42.0}]\n", + ... +} + ``` diff --git a/examples/powertools-examples-serialization/template.yaml b/examples/powertools-examples-serialization/template.yaml index 539d2d615..f330ec146 100644 --- a/examples/powertools-examples-serialization/template.yaml +++ b/examples/powertools-examples-serialization/template.yaml @@ -32,7 +32,7 @@ Resources: SQSEventDeserializationFunction: Type: AWS::Serverless::Function Properties: - CodeUri: Function + CodeUri: . Handler: org.demo.serialization.SQSEventDeserializationFunction::handleRequest Policies: - Statement: diff --git a/examples/powertools-examples-sqs/README.md b/examples/powertools-examples-sqs/README.md index 2b6da65b5..45f4a4a74 100644 --- a/examples/powertools-examples-sqs/README.md +++ b/examples/powertools-examples-sqs/README.md @@ -1,3 +1,56 @@ -## SqsBatchProcessingDemo +# Powertools for AWS Lambda (Java) - SQS Batch Processing Example -Demos setup of SQS Batch processing via Powertools +This project contains an example of Lambda function using the batch processing utilities module of Powertools for AWS Lambda (Java). +For more information on this module, please refer to the [documentation](https://docs.powertools.aws.dev/lambda/java/utilities/batch/). + +The project contains two functions: + +* [SqsMessageSender](src/main/java/org/demo/sqs/SqsMessageSender.java) - Sends a set of messages to an SQS queue. +This function is triggered every 5 minutes by an EventBridge schedule rule. +* [SqsPoller](src/main/java/org/demo/sqs/SqsPoller.java) - Listens to the same queue, processing items off in batches + +The poller intentionally fails intermittently processing messages to demonstrate the replay behaviour of the batch +module: + +
    + +SqsPoller.java + +[SqsPoller.java:43](src/main/java/org/demo/sqs/SqsPoller.java) + +```java + public String process(SQSMessage message) { + log.info("Processing message with id {}", message.getMessageId()); + + int nextInt = random.nextInt(100); + + if(nextInt <= 10) { + log.info("Randomly picked message with id {} as business validation failure.", message.getMessageId()); + throw new IllegalArgumentException("Failed business validation. No point of retrying. Move me to DLQ." + message.getMessageId()); + } + + if(nextInt > 90) { + log.info("Randomly picked message with id {} as intermittent failure.", message.getMessageId()); + throw new RuntimeException("Failed due to intermittent issue. Will be sent back for retry." + message.getMessageId()); + } + + return "Success"; + } +``` + +
    + +## Deploy the sample application + +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) + +## Test the application + +As the test is pushing through a batch every 5 minutes, we can simply watch the logs to see the batches being processed: + +```bash + sam logs --tail --stack-name $MY_STACK +``` + +As the handler intentionally introduces intermittent failures, we should expect to see error messages too! diff --git a/examples/powertools-examples-validation/README.md b/examples/powertools-examples-validation/README.md index 39afc48b9..3f6790b0c 100644 --- a/examples/powertools-examples-validation/README.md +++ b/examples/powertools-examples-validation/README.md @@ -1,21 +1,26 @@ -# Validation +# Powertools for AWS Lambda (Java) - Validation Example -This project contains an example of Lambda function using the validation module of Powertools for AWS Lambda (Java). For more information on this module, please refer to the [documentation](https://docs.powertools.aws.dev/lambda-java/utilities/validation/). +This project contains an example of Lambda function using the validation module of Powertools for AWS Lambda (Java). +For more information on this module, please refer to the [documentation](https://docs.powertools.aws.dev/lambda-java/utilities/validation/). + +The handler [InboundValidation](src/main/java/org/demo/validation/InboundValidation.java) validates incoming HTTP requests +received from the API gateway against [schema.json](src/main/resources/schema.json). ## Deploy the sample application -This sample is based on Serverless Application Model (SAM) and you can use the SAM Command Line Interface (SAM CLI) to build it and deploy it to AWS. +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) -To use the SAM CLI, you need the following tools. +## Test the application -* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) -* Java11 - [Install the Java 11](https://docs.aws.amazon.com/corretto/latest/corretto-11-ug/downloads-list.html) -* Maven - [Install Maven](https://maven.apache.org/install.html) -* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community) +To test the validation, we can POST a JSON object shaped like our schema: +```bash + curl -X POST https://[REST-API-ID].execute-api.[REGION].amazonaws.com/Prod/hello/ -H "Content-Type: application/json" -d '{"address": "https://checkip.amazonaws.com"}' +``` -To build and deploy your application for the first time, run the following in your shell: +If we break the schema - for instance, by removing one of the compulsory fields, +we will get an error back from our API and will see a `ValidationException` in the logs: ```bash -sam build -sam deploy --guided + sam logs --tail --stack-name $MY_STACK ```