Skip to content

Commit 33fe108

Browse files
authored
[feat] Import examples from aws-samples/aws-lambda-powertools-examples (#1051)
1 parent e72e82c commit 33fe108

File tree

54 files changed

+2821
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2821
-4
lines changed

.github/workflows/build.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ on:
1616
- 'powertools-parameters/**'
1717
- 'powertools-metrics/**'
1818
- 'powertools-test-suite/**'
19+
- 'examples/**'
1920
- 'pom.xml'
21+
- 'examples/pom.xml'
2022
- '.github/workflows/**'
2123
push:
2224
branches:
@@ -33,7 +35,9 @@ on:
3335
- 'powertools-parameters/**'
3436
- 'powertools-metrics/**'
3537
- 'powertools-test-suite/**'
38+
- 'examples/**'
3639
- 'pom.xml'
40+
- 'examples/pom.xml'
3741
- '.github/workflows/**'
3842
jobs:
3943
build:

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ And configure the aspectj-maven-plugin to compile-time weave (CTW) the aws-lambd
7979

8080
## Example
8181

82-
See **[example](https://github.com/aws-samples/aws-lambda-powertools-examples/tree/main/java)** for example project showcasing usage of different utilities.
83-
Have a demo project to contribute which showcase usage of different utilities from powertools? We are happy to accept it [here](https://github.com/aws-samples/aws-lambda-powertools-examples/blob/main/CONTRIBUTING.md#security-issue-notifications).
82+
See the **[examples](examples)** directory for example projects showcasing usage of different utilities.
83+
84+
Have a demo project to contribute which showcase usage of different utilities from powertools? We are happy to accept it [here](CONTRIBUTING.md#security-issue-notifications).
8485

8586
## Credits
8687

examples/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dependency-reduced-pom.xml
2+
.aws-sam

examples/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## aws-lambda-powertools-examples
2+
3+
This directory holds example projects demoing different components of the Lambda Powertools for Java.
4+
5+

examples/pom.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<artifactId>powertools-examples</artifactId>
8+
<packaging>pom</packaging>
9+
10+
<parent>
11+
<artifactId>powertools-parent</artifactId>
12+
<groupId>software.amazon.lambda</groupId>
13+
<version>1.14.0</version>
14+
</parent>
15+
16+
<name>AWS Lambda Powertools for Java library Examples</name>
17+
<description>
18+
A suite of examples accompanying for AWS Lambda Powertools.
19+
</description>
20+
21+
<modules>
22+
<module>powertools-examples-core</module>
23+
<module>powertools-examples-idempotency</module>
24+
<module>powertools-examples-parameters</module>
25+
<module>powertools-examples-serialization</module>
26+
<module>powertools-examples-sqs</module>
27+
<module>powertools-examples-validation</module>
28+
</modules>
29+
30+
<!-- Don't deploy the examples -->
31+
<properties>
32+
<maven.deploy.skip>true</maven.deploy.skip>
33+
</properties>
34+
35+
</project>

examples/powertools-examples-core/.gitignore

Whitespace-only changes.
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# CoreUtilities
2+
3+
This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes [Lambda Powertools for operational best practices](https://github.com/awslabs/aws-lambda-powertools-java), and the following files and folders.
4+
5+
- HelloWorldFunction/src/main - Code for the application's Lambda function.
6+
- events - Invocation events that you can use to invoke the function.
7+
- HelloWorldFunction/src/test - Unit tests for the application code.
8+
- template.yaml - A template that defines the application's AWS resources.
9+
10+
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.
11+
12+
If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit.
13+
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.
14+
15+
* [PyCharm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)
16+
* [IntelliJ](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)
17+
* [VS Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html)
18+
* [Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/welcome.html)
19+
20+
## Deploy the sample application
21+
22+
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.
23+
24+
To use the SAM CLI, you need the following tools.
25+
26+
* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)
27+
* Java11 - [Install the Java 11](https://docs.aws.amazon.com/corretto/latest/corretto-11-ug/downloads-list.html)
28+
* Maven - [Install Maven](https://maven.apache.org/install.html)
29+
* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community)
30+
31+
To build and deploy your application for the first time, run the following in your shell:
32+
33+
```bash
34+
Coreutilities$ sam build
35+
Coreutilities$ sam deploy --guided
36+
```
37+
38+
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:
39+
40+
* **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.
41+
* **AWS Region**: The AWS region you want to deploy your app to.
42+
* **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.
43+
* **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.
44+
* **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.
45+
46+
You can find your API Gateway Endpoint URL in the output values displayed after deployment.
47+
48+
## Use the SAM CLI to build and test locally
49+
50+
Build your application with the `sam build` command.
51+
52+
```bash
53+
Coreutilities$ sam build
54+
```
55+
56+
The SAM CLI installs dependencies defined in `HelloWorldFunction/pom.xml`, creates a deployment package, and saves it in the `.aws-sam/build` folder.
57+
58+
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.
59+
60+
Run functions locally and invoke them with the `sam local invoke` command.
61+
62+
```bash
63+
Coreutilities$ sam local invoke HelloWorldFunction --event events/event.json
64+
```
65+
66+
The SAM CLI can also emulate your application's API. Use the `sam local start-api` to run the API locally on port 3000.
67+
68+
```bash
69+
Coreutilities$ sam local start-api
70+
Coreutilities$ curl http://localhost:3000/
71+
```
72+
73+
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.
74+
75+
```yaml
76+
Events:
77+
HelloWorld:
78+
Type: Api
79+
Properties:
80+
Path: /hello
81+
Method: get
82+
```
83+
84+
## Add a resource to your application
85+
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.
86+
87+
## Fetch, tail, and filter Lambda function logs
88+
89+
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.
90+
91+
`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM.
92+
93+
```bash
94+
Coreutilities$ sam logs -n HelloWorldFunction --stack-name <Name-of-your-deployed-stack> --tail
95+
```
96+
97+
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).
98+
99+
## Unit tests
100+
101+
Tests are defined in the `HelloWorldFunction/src/test` folder in this project.
102+
103+
```bash
104+
Coreutilities$ cd HelloWorldFunction
105+
HelloWorldFunction$ mvn test
106+
```
107+
108+
## Cleanup
109+
110+
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:
111+
112+
```bash
113+
aws cloudformation delete-stack --stack-name <Name-of-your-deployed-stack>
114+
```
115+
116+
# Appendix
117+
118+
## Powertools
119+
120+
**Tracing**
121+
122+
[Tracing utility](https://awslabs.github.io/aws-lambda-powertools-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.
123+
124+
**Logger**
125+
126+
[Logging utility](https://awslabs.github.io/aws-lambda-powertools-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.
127+
128+
**Metrics**
129+
130+
[Metrics utility](https://awslabs.github.io/aws-lambda-powertools-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.
131+
132+
## Resources
133+
134+
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.
135+
136+
Check the [AWS Lambda Powertools Java](https://awslabs.github.io/aws-lambda-powertools-java/) for more information on how to use and configure such tools
137+
138+
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/)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"body": "{\"message\": \"hello world\"}",
3+
"resource": "/{proxy+}",
4+
"path": "/path/to/resource",
5+
"httpMethod": "POST",
6+
"isBase64Encoded": false,
7+
"queryStringParameters": {
8+
"foo": "bar"
9+
},
10+
"pathParameters": {
11+
"proxy": "/path/to/resource"
12+
},
13+
"stageVariables": {
14+
"baz": "qux"
15+
},
16+
"headers": {
17+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
18+
"Accept-Encoding": "gzip, deflate, sdch",
19+
"Accept-Language": "en-US,en;q=0.8",
20+
"Cache-Control": "max-age=0",
21+
"CloudFront-Forwarded-Proto": "https",
22+
"CloudFront-Is-Desktop-Viewer": "true",
23+
"CloudFront-Is-Mobile-Viewer": "false",
24+
"CloudFront-Is-SmartTV-Viewer": "false",
25+
"CloudFront-Is-Tablet-Viewer": "false",
26+
"CloudFront-Viewer-Country": "US",
27+
"Host": "1234567890.execute-api.us-east-1.amazonaws.com",
28+
"Upgrade-Insecure-Requests": "1",
29+
"User-Agent": "Custom User Agent String",
30+
"Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)",
31+
"X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==",
32+
"X-Forwarded-For": "127.0.0.1, 127.0.0.2",
33+
"X-Forwarded-Port": "443",
34+
"X-Forwarded-Proto": "https"
35+
},
36+
"requestContext": {
37+
"accountId": "123456789012",
38+
"resourceId": "123456",
39+
"stage": "prod",
40+
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
41+
"requestTime": "09/Apr/2015:12:34:56 +0000",
42+
"requestTimeEpoch": 1428582896000,
43+
"identity": {
44+
"cognitoIdentityPoolId": null,
45+
"accountId": null,
46+
"cognitoIdentityId": null,
47+
"caller": null,
48+
"accessKey": null,
49+
"sourceIp": "127.0.0.1",
50+
"cognitoAuthenticationType": null,
51+
"cognitoAuthenticationProvider": null,
52+
"userArn": null,
53+
"userAgent": "Custom User Agent String",
54+
"user": null
55+
},
56+
"path": "/prod/path/to/resource",
57+
"resourcePath": "/{proxy+}",
58+
"httpMethod": "POST",
59+
"apiId": "1234567890",
60+
"protocol": "HTTP/1.1"
61+
}
62+
}
63+

0 commit comments

Comments
 (0)