Skip to content

Commit 70e7838

Browse files
committed
Java events sample
1 parent fa1368a commit 70e7838

17 files changed

+667
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
BUCKET_ID=$(dd if=/dev/random bs=8 count=1 2>/dev/null | od -An -tx1 | tr -d ' \t\n')
3+
BUCKET_NAME=lambda-artifacts-$BUCKET_ID
4+
echo $BUCKET_NAME > bucket-name.txt
5+
aws s3 mb s3://$BUCKET_NAME

sample-apps/java-events/2-deploy.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
set -eo pipefail
3+
ARTIFACT_BUCKET=$(cat bucket-name.txt)
4+
TEMPLATE=template.yml
5+
if [ $1 ]
6+
then
7+
if [ $1 = mvn ]
8+
then
9+
TEMPLATE=template-mvn.yml
10+
mvn package
11+
fi
12+
else
13+
gradle build -i
14+
fi
15+
aws cloudformation package --template-file $TEMPLATE --s3-bucket $ARTIFACT_BUCKET --output-template-file out.yml
16+
aws cloudformation deploy --template-file out.yml --stack-name java-events --capabilities CAPABILITY_NAMED_IAM

sample-apps/java-events/3-invoke.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
set -eo pipefail
3+
FUNCTION=$(aws cloudformation describe-stack-resource --stack-name java-events --logical-resource-id function --query 'StackResourceDetail.PhysicalResourceId' --output text)
4+
if [ $1 ]
5+
then
6+
case $1 in
7+
apig)
8+
PAYLOAD='file://event-apigv1.json'
9+
;;
10+
*)
11+
echo -n "Unknown event type"
12+
;;
13+
esac
14+
fi
15+
while true; do
16+
if [ $PAYLOAD ]
17+
then
18+
aws lambda invoke --function-name $FUNCTION --payload $PAYLOAD out.json
19+
else
20+
aws lambda invoke --function-name $FUNCTION --payload file://event.json out.json
21+
fi
22+
cat out.json
23+
echo ""
24+
sleep 2
25+
done

sample-apps/java-events/4-cleanup.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
set -eo pipefail
3+
aws cloudformation delete-stack --stack-name java-events
4+
echo "Deleted function stack"
5+
if [ -f bucket-name.txt ]; then
6+
ARTIFACT_BUCKET=$(cat bucket-name.txt)
7+
while true; do
8+
read -p "Delete deployment artifacts and bucket ($ARTIFACT_BUCKET)?" response
9+
case $response in
10+
[Yy]* ) aws s3 rb --force s3://$ARTIFACT_BUCKET; rm bucket-name.txt; break;;
11+
[Nn]* ) break;;
12+
* ) echo "Response must start with y or n.";;
13+
esac
14+
done
15+
fi
16+
rm -f out.yml out.json
17+
rm -rf build .gradle target

sample-apps/java-events/README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Basic Function with Minimal Dependencies (Java)
2+
This project includes function code and supporting resources:
3+
- src/main - A Java function.
4+
- src/test - A unit test and helper classes.
5+
- template.yml - An AWS CloudFormation template that creates an application.
6+
- build.gradle - A Gradle build file.
7+
- pom.xml - A Maven build file.
8+
- 1-create-bucket.sh, 2-deploy.sh, etc. - Shell scripts that use the AWS CLI to deploy and manage the application.
9+
10+
![Architecture](/sample-apps/java-events/images/sample-java-events.png)
11+
12+
Use the following instructions to deploy the sample application.
13+
14+
# Requirements
15+
- [Java 8 runtime environment (SE JRE)](https://www.oracle.com/java/technologies/javase-downloads.html)
16+
- [Gradle 5](https://gradle.org/releases/) or [Maven 3](https://maven.apache.org/docs/history.html)
17+
- The Bash shell. For Linux and macOS, this is included by default. In Windows 10, you can install the [Windows Subsystem for Linux](https://docs.microsoft.com/en-us/windows/wsl/install-win10) to get a Windows-integrated version of Ubuntu and Bash.
18+
- [The AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html).
19+
20+
# Setup
21+
Download or clone this repository.
22+
23+
$ git clone [email protected]:awsdocs/aws-lambda-developer-guide.git
24+
$ cd aws-lambda-developer-guide/sample-apps/java-events
25+
26+
Run `1-create-bucket.sh` to create a new bucket for deployment artifacts. Or, if you already have a bucket, create a file named `bucket-name.txt` that contains the name of your bucket.
27+
28+
java-events$ ./1-create-bucket.sh
29+
make_bucket: lambda-artifacts-a5e4xmplb5b22e0d
30+
31+
# Deploy
32+
Run `2-deploy.sh` to build the application with Gradle and deploy it.
33+
34+
java-events$ ./2-deploy.sh
35+
BUILD SUCCESSFUL in 1s
36+
Successfully packaged artifacts and wrote output template to file out.yml.
37+
Waiting for changeset to be created..
38+
Successfully created/updated stack - java-events
39+
40+
This script uses AWS CloudFormation to deploy the Lambda functions and an IAM role. If the AWS CloudFormation stack that contains the resources already exists, the script updates it with any changes to the template or function code.
41+
42+
You can also build the application with Maven. To use maven, add `mvn` to the command.
43+
44+
java-events$ ./2-deploy.sh mvn
45+
[INFO] Scanning for projects...
46+
[INFO] -----------------------< com.example:java-events >-----------------------
47+
[INFO] Building java-events-function 1.0-SNAPSHOT
48+
[INFO] --------------------------------[ jar ]---------------------------------
49+
...
50+
51+
# Test
52+
Run `3-invoke.sh` to invoke the function.
53+
54+
java-events$ ./3-invoke.sh
55+
{
56+
"StatusCode": 200,
57+
"ExecutedVersion": "$LATEST"
58+
}
59+
{"isBase64Encoded":false,"statusCode":200,"headers":{"Content-Type":"text/html"},"body":"<!DOCTYPE html><html><head><title>AWS Lambda sample</title></head><body><h1>Welcome</h1><p>Page generated by a Lambda function.</p></body></html>"}
60+
61+
The functions in this application are instrumented with AWS X-Ray. Open the [X-Ray console](https://console.aws.amazon.com/xray/home#/service-map) to view the service map.
62+
63+
![Service Map](/sample-apps/java-events/images/java-events-servicemap.png)
64+
65+
Choose a node in the main function graph. Then choose **View traces** to see a list of traces. Choose any trace to view a timeline that breaks down the work done by the function.
66+
67+
![Trace](/sample-apps/java-events/images/java-events-trace.png)
68+
69+
Finally, view the application in the Lambda console.
70+
71+
*To view the output*
72+
1. Open the [applications page](https://console.aws.amazon.com/lambda/home#/applications) in the Lambda console.
73+
2. Choose **java-events**.
74+
75+
![Application](/sample-apps/java-events/images/java-events-application.png)
76+
77+
# Configure Handler Class
78+
79+
By default, the function uses a handler class named `Handler` that takes a map as input and returns a string. The project also includes handlers that use other input and output types. These are defined in the following files under src/main/java/example:
80+
81+
- `Handler.java` – Takes `APIGatewayV2ProxyRequestEvent` as input and returns `APIGatewayV2ProxyResponseEvent`.
82+
- `HandlerApiG.java` – Takes `APIGatewayProxyRequestEvent` as input and returns `APIGatewayProxyResponseEvent`.
83+
84+
To use a different handler, change the value of the Handler setting in the application template (`template.yml` or `template-mvn.yaml`). For example, to use the API Gateway v1 handler:
85+
86+
Properties:
87+
CodeUri: build/distributions/java-events.zip
88+
Handler: example.HandlerApiG
89+
90+
Deploy the change, and then use the invoke script to test the new configuration. For handlers, that don't take a JSON object as input, pass the type (e.g. `apig`) as an argument to the invoke script.
91+
92+
./3-invoke.sh apig
93+
{
94+
"StatusCode": 200,
95+
"ExecutedVersion": "$LATEST"
96+
}
97+
9979
98+
99+
# Cleanup
100+
To delete the application, run `4-cleanup.sh`.
101+
102+
java-events$ ./4-cleanup.sh

sample-apps/java-events/build.gradle

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
implementation 'com.amazonaws:aws-lambda-java-core:1.2.0'
11+
implementation 'com.amazonaws:aws-lambda-java-events:2.2.7'
12+
implementation 'com.google.code.gson:gson:2.8.6'
13+
testImplementation 'org.apache.logging.log4j:log4j-api:2.13.0'
14+
testImplementation 'org.apache.logging.log4j:log4j-core:2.13.0'
15+
testImplementation 'org.apache.logging.log4j:log4j-slf4j18-impl:2.13.0'
16+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
17+
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.0'
18+
}
19+
20+
test {
21+
useJUnitPlatform()
22+
}
23+
24+
task buildZip(type: Zip) {
25+
from compileJava
26+
from processResources
27+
into('lib') {
28+
from configurations.runtimeClasspath
29+
}
30+
}
31+
32+
java {
33+
sourceCompatibility = JavaVersion.VERSION_1_8
34+
targetCompatibility = JavaVersion.VERSION_1_8
35+
}
36+
37+
build.dependsOn buildZip
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{
2+
"resource": "/",
3+
"path": "/",
4+
"httpMethod": "GET",
5+
"headers": {
6+
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
7+
"accept-encoding": "gzip, deflate, br",
8+
"accept-language": "en-US,en;q=0.9",
9+
"cookie": "s_fid=7AAB6XMPLAFD9BBF-0643XMPL09956DE2; regStatus=pre-register",
10+
"Host": "70ixmpl4fl.execute-api.us-east-2.amazonaws.com",
11+
"sec-fetch-dest": "document",
12+
"sec-fetch-mode": "navigate",
13+
"sec-fetch-site": "none",
14+
"upgrade-insecure-requests": "1",
15+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
16+
"X-Amzn-Trace-Id": "Root=1-5e66d96f-7491f09xmpl79d18acf3d050",
17+
"X-Forwarded-For": "52.255.255.12",
18+
"X-Forwarded-Port": "443",
19+
"X-Forwarded-Proto": "https"
20+
},
21+
"multiValueHeaders": {
22+
"accept": [
23+
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
24+
],
25+
"accept-encoding": [
26+
"gzip, deflate, br"
27+
],
28+
"accept-language": [
29+
"en-US,en;q=0.9"
30+
],
31+
"cookie": [
32+
"s_fid=7AABXMPL1AFD9BBF-0643XMPL09956DE2; regStatus=pre-register;"
33+
],
34+
"Host": [
35+
"70ixmpl4fl.execute-api.ca-central-1.amazonaws.com"
36+
],
37+
"sec-fetch-dest": [
38+
"document"
39+
],
40+
"sec-fetch-mode": [
41+
"navigate"
42+
],
43+
"sec-fetch-site": [
44+
"none"
45+
],
46+
"upgrade-insecure-requests": [
47+
"1"
48+
],
49+
"User-Agent": [
50+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"
51+
],
52+
"X-Amzn-Trace-Id": [
53+
"Root=1-5e66d96f-7491f09xmpl79d18acf3d050"
54+
],
55+
"X-Forwarded-For": [
56+
"52.255.255.12"
57+
],
58+
"X-Forwarded-Port": [
59+
"443"
60+
],
61+
"X-Forwarded-Proto": [
62+
"https"
63+
]
64+
},
65+
"queryStringParameters": null,
66+
"multiValueQueryStringParameters": null,
67+
"pathParameters": null,
68+
"stageVariables": null,
69+
"requestContext": {
70+
"resourceId": "2gxmpl",
71+
"resourcePath": "/",
72+
"httpMethod": "GET",
73+
"extendedRequestId": "JJbxmplHYosFVYQ=",
74+
"requestTime": "10/Mar/2020:00:03:59 +0000",
75+
"path": "/Prod/",
76+
"accountId": "123456789012",
77+
"protocol": "HTTP/1.1",
78+
"stage": "Prod",
79+
"domainPrefix": "70ixmpl4fl",
80+
"requestTimeEpoch": 1583798639428,
81+
"requestId": "77375676-xmpl-4b79-853a-f982474efe18",
82+
"identity": {
83+
"cognitoIdentityPoolId": null,
84+
"accountId": null,
85+
"cognitoIdentityId": null,
86+
"caller": null,
87+
"sourceIp": "52.255.255.12",
88+
"principalOrgId": null,
89+
"accessKey": null,
90+
"cognitoAuthenticationType": null,
91+
"cognitoAuthenticationProvider": null,
92+
"userArn": null,
93+
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
94+
"user": null
95+
},
96+
"domainName": "70ixmpl4fl.execute-api.us-east-2.amazonaws.com",
97+
"apiId": "70ixmpl4fl"
98+
},
99+
"body": null,
100+
"isBase64Encoded": false
101+
}

sample-apps/java-events/event.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"version": "2.0",
3+
"routeKey": "ANY /nodejs-apig-function-1G3XMPLZXVXYI",
4+
"rawPath": "/default/nodejs-apig-function-1G3XMPLZXVXYI",
5+
"rawQueryString": "",
6+
"cookies": [
7+
"s_fid=7AABXMPL1AFD9BBF-0643XMPL09956DE2",
8+
"regStatus=pre-register"
9+
],
10+
"headers": {
11+
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
12+
"accept-encoding": "gzip, deflate, br",
13+
"accept-language": "en-US,en;q=0.9",
14+
"content-length": "0",
15+
"host": "r3pmxmplak.execute-api.us-east-2.amazonaws.com",
16+
"sec-fetch-dest": "document",
17+
"sec-fetch-mode": "navigate",
18+
"sec-fetch-site": "cross-site",
19+
"sec-fetch-user": "?1",
20+
"upgrade-insecure-requests": "1",
21+
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
22+
"x-amzn-trace-id": "Root=1-5e6722a7-cc56xmpl46db7ae02d4da47e",
23+
"x-forwarded-for": "205.255.255.176",
24+
"x-forwarded-port": "443",
25+
"x-forwarded-proto": "https"
26+
},
27+
"requestContext": {
28+
"accountId": "123456789012",
29+
"apiId": "r3pmxmplak",
30+
"domainName": "r3pmxmplak.execute-api.us-east-2.amazonaws.com",
31+
"domainPrefix": "r3pmxmplak",
32+
"http": {
33+
"method": "GET",
34+
"path": "/default/nodejs-apig-function-1G3XMPLZXVXYI",
35+
"protocol": "HTTP/1.1",
36+
"sourceIp": "205.255.255.176",
37+
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"
38+
},
39+
"requestId": "JKJaXmPLvHcESHA=",
40+
"routeId": null,
41+
"routeKey": "ANY /nodejs-apig-function-1G3XMPLZXVXYI",
42+
"stage": "default",
43+
"time": "10/Mar/2020:05:16:23 +0000",
44+
"timeEpoch": 1583817383220
45+
},
46+
"isBase64Encoded": true
47+
}

0 commit comments

Comments
 (0)