Skip to content

Commit 0182484

Browse files

23 files changed

+723
-0
lines changed

1-create-bucket.sh

+5
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

2-build-layer.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
set -eo pipefail
3+
gradle -q packageLibs
4+
mv build/distributions/blank-java.zip build/blank-java-lib.zip

3-deploy.sh

+16
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 blank-java --capabilities CAPABILITY_NAMED_IAM

4-invoke.sh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
set -eo pipefail
3+
FUNCTION=$(aws cloudformation describe-stack-resource --stack-name blank-java --logical-resource-id function --query 'StackResourceDetail.PhysicalResourceId' --output text)
4+
5+
while true; do
6+
aws lambda invoke --function-name $FUNCTION --payload file://event.json out.json
7+
cat out.json
8+
echo ""
9+
sleep 2
10+
done

5-cleanup.sh

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/bash
2+
set -eo pipefail
3+
STACK=blank-java
4+
if [[ $# -eq 1 ]] ; then
5+
STACK=$1
6+
echo "Deleting stack $STACK"
7+
fi
8+
FUNCTION=$(aws cloudformation describe-stack-resource --stack-name $STACK --logical-resource-id function --query 'StackResourceDetail.PhysicalResourceId' --output text)
9+
aws cloudformation delete-stack --stack-name $STACK
10+
echo "Deleted $STACK stack."
11+
12+
if [ -f bucket-name.txt ]; then
13+
ARTIFACT_BUCKET=$(cat bucket-name.txt)
14+
if [[ ! $ARTIFACT_BUCKET =~ lambda-artifacts-[a-z0-9]{16} ]] ; then
15+
echo "Bucket was not created by this application. Skipping."
16+
else
17+
while true; do
18+
read -p "Delete deployment artifacts and bucket ($ARTIFACT_BUCKET)? (y/n)" response
19+
case $response in
20+
[Yy]* ) aws s3 rb --force s3://$ARTIFACT_BUCKET; rm bucket-name.txt; break;;
21+
[Nn]* ) break;;
22+
* ) echo "Response must start with y or n.";;
23+
esac
24+
done
25+
fi
26+
fi
27+
28+
while true; do
29+
read -p "Delete function log group (/aws/lambda/$FUNCTION)? (y/n)" response
30+
case $response in
31+
[Yy]* ) aws logs delete-log-group --log-group-name /aws/lambda/$FUNCTION; break;;
32+
[Nn]* ) break;;
33+
* ) echo "Response must start with y or n.";;
34+
esac
35+
done
36+
37+
rm -f out.yml out.json
38+
rm -rf build .gradle target

README.md

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Blank function (Java)
2+
3+
![Architecture](/sample-apps/blank-java/images/sample-blank-java.png)
4+
5+
The project source includes function code and supporting resources:
6+
7+
- `src/main` - A Java function.
8+
- `src/test` - A unit test and helper classes.
9+
- `template.yml` - An AWS CloudFormation template that creates an application.
10+
- `build.gradle` - A Gradle build file.
11+
- `pom.xml` - A Maven build file.
12+
- `1-create-bucket.sh`, `2-build-layer.sh`, etc. - Shell scripts that use the AWS CLI to deploy and manage the application.
13+
14+
Use the following instructions to deploy the sample application.
15+
16+
# Requirements
17+
- [Java 8 runtime environment (SE JRE)](https://www.oracle.com/java/technologies/javase-downloads.html)
18+
- [Gradle 5](https://gradle.org/releases/) or [Maven 3](https://maven.apache.org/docs/history.html)
19+
- 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.
20+
- [The AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) v1.17 or newer.
21+
22+
If you use the AWS CLI v2, add the following to your [configuration file](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html) (`~/.aws/config`):
23+
24+
```
25+
cli_binary_format=raw-in-base64-out
26+
```
27+
28+
This setting enables the AWS CLI v2 to load JSON events from a file, matching the v1 behavior.
29+
30+
# Setup
31+
Download or clone this repository.
32+
33+
$ git clone https://github.com/awsdocs/aws-lambda-developer-guide.git
34+
$ cd aws-lambda-developer-guide/sample-apps/blank-java
35+
36+
To create a new bucket for deployment artifacts, run `1-create-bucket.sh`.
37+
38+
blank-java$ ./1-create-bucket.sh
39+
make_bucket: lambda-artifacts-a5e491dbb5b22e0d
40+
41+
To build a Lambda layer that contains the function's runtime dependencies, run `2-build-layer.sh`. Packaging dependencies in a layer reduces the size of the deployment package that you upload when you modify your code.
42+
43+
blank-java$ ./2-build-layer.sh
44+
45+
# Deploy
46+
47+
To deploy the application, run `3-deploy.sh`.
48+
49+
blank-java$ ./3-deploy.sh
50+
BUILD SUCCESSFUL in 1s
51+
Successfully packaged artifacts and wrote output template to file out.yml.
52+
Waiting for changeset to be created..
53+
Successfully created/updated stack - blank-java
54+
55+
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.
56+
57+
You can also build the application with Maven. To use maven, add `mvn` to the command.
58+
59+
java-basic$ ./3-deploy.sh mvn
60+
[INFO] Scanning for projects...
61+
[INFO] -----------------------< com.example:blank-java >-----------------------
62+
[INFO] Building blank-java-function 1.0-SNAPSHOT
63+
[INFO] --------------------------------[ jar ]---------------------------------
64+
...
65+
66+
# Test
67+
To invoke the function, run `4-invoke.sh`.
68+
69+
blank-java$ ./4-invoke.sh
70+
{
71+
"StatusCode": 200,
72+
"ExecutedVersion": "$LATEST"
73+
}
74+
75+
Let the script invoke the function a few times and then press `CRTL+C` to exit.
76+
77+
The application uses AWS X-Ray to trace requests. Open the [X-Ray console](https://console.aws.amazon.com/xray/home#/service-map) to view the service map.
78+
79+
![Service Map](/sample-apps/blank-java/images/blank-java-servicemap.png)
80+
81+
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.
82+
83+
![Trace](/sample-apps/blank-java/images/blank-java-trace.png)
84+
85+
Finally, view the application in the Lambda console.
86+
87+
*To view the application*
88+
1. Open the [applications page](https://console.aws.amazon.com/lambda/home#/applications) in the Lambda console.
89+
2. Choose **blank-java**.
90+
91+
![Application](/sample-apps/blank-java/images/blank-java-application.png)
92+
93+
# Cleanup
94+
To delete the application, run `5-cleanup.sh`.
95+
96+
blank$ ./5-cleanup.sh

build.gradle

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
implementation platform('software.amazon.awssdk:bom:2.10.73')
11+
implementation platform('com.amazonaws:aws-xray-recorder-sdk-bom:2.4.0')
12+
implementation 'software.amazon.awssdk:lambda'
13+
implementation 'com.amazonaws:aws-xray-recorder-sdk-core'
14+
implementation 'com.amazonaws:aws-xray-recorder-sdk-aws-sdk-core'
15+
implementation 'com.amazonaws:aws-xray-recorder-sdk-aws-sdk-v2'
16+
implementation 'com.amazonaws:aws-xray-recorder-sdk-aws-sdk-v2-instrumentor'
17+
implementation 'com.amazonaws:aws-lambda-java-core:1.2.1'
18+
implementation 'com.amazonaws:aws-lambda-java-events:2.2.9'
19+
implementation 'com.google.code.gson:gson:2.8.6'
20+
implementation 'org.apache.logging.log4j:log4j-api:[2.17.1,)'
21+
implementation 'org.apache.logging.log4j:log4j-core:[2.17.1,)'
22+
runtimeOnly 'org.apache.logging.log4j:log4j-slf4j18-impl:[2.17.1,)'
23+
runtimeOnly 'com.amazonaws:aws-lambda-java-log4j2:1.5.0'
24+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
25+
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.0'
26+
}
27+
28+
test {
29+
useJUnitPlatform()
30+
}
31+
32+
task packageFat(type: Zip) {
33+
from compileJava
34+
from processResources
35+
into('lib') {
36+
from configurations.runtimeClasspath
37+
}
38+
dirMode = 0755
39+
fileMode = 0755
40+
}
41+
42+
task packageLibs(type: Zip) {
43+
into('java/lib') {
44+
from configurations.runtimeClasspath
45+
}
46+
dirMode = 0755
47+
fileMode = 0755
48+
}
49+
50+
task packageSkinny(type: Zip) {
51+
from compileJava
52+
from processResources
53+
}
54+
55+
java {
56+
sourceCompatibility = JavaVersion.VERSION_1_8
57+
targetCompatibility = JavaVersion.VERSION_1_8
58+
}
59+
60+
build.dependsOn packageSkinny

event.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"Records": [
3+
{
4+
"messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78",
5+
"receiptHandle": "MessageReceiptHandle",
6+
"body": "Hello from SQS!",
7+
"attributes": {
8+
"ApproximateReceiveCount": "1",
9+
"SentTimestamp": "1523232000000",
10+
"SenderId": "123456789012",
11+
"ApproximateFirstReceiveTimestamp": "1523232000001"
12+
},
13+
"messageAttributes": {},
14+
"md5OfBody": "7b270e59b47ff90a553787216d55d91d",
15+
"eventSource": "aws:sqs",
16+
"eventSourceARN": "arn:aws:sqs:us-west-2:123456789012:MyQueue",
17+
"awsRegion": "us-west-2"
18+
}
19+
]
20+
}

images/blank-java-application.png

40.2 KB
Loading

images/blank-java-servicemap.png

19.6 KB
Loading

images/blank-java-trace.png

29.2 KB
Loading

images/sample-blank-java.png

48.5 KB
Loading

pom.xml

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.example</groupId>
5+
<artifactId>blank-java</artifactId>
6+
<packaging>jar</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>blank-java-function</name>
9+
<properties>
10+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11+
<maven.compiler.source>1.8</maven.compiler.source>
12+
<maven.compiler.target>1.8</maven.compiler.target>
13+
</properties>
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.amazonaws</groupId>
17+
<artifactId>aws-lambda-java-core</artifactId>
18+
<version>1.2.1</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>com.amazonaws</groupId>
22+
<artifactId>aws-lambda-java-events</artifactId>
23+
<version>2.2.9</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.amazonaws</groupId>
27+
<artifactId>aws-lambda-java-log4j2</artifactId>
28+
<version>1.5.0</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>com.google.code.gson</groupId>
32+
<artifactId>gson</artifactId>
33+
<version>2.8.6</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.apache.logging.log4j</groupId>
37+
<artifactId>log4j-api</artifactId>
38+
<version>[2.17.1,)</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.apache.logging.log4j</groupId>
42+
<artifactId>log4j-core</artifactId>
43+
<version>[2.17.1,)</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.apache.logging.log4j</groupId>
47+
<artifactId>log4j-slf4j18-impl</artifactId>
48+
<version>[2.17.1,)</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>software.amazon.awssdk</groupId>
52+
<artifactId>lambda</artifactId>
53+
<version>2.10.72</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>com.amazonaws</groupId>
57+
<artifactId>aws-xray-recorder-sdk-core</artifactId>
58+
<version>2.4.0</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>com.amazonaws</groupId>
62+
<artifactId>aws-xray-recorder-sdk-aws-sdk-core</artifactId>
63+
<version>2.4.0</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>com.amazonaws</groupId>
67+
<artifactId>aws-xray-recorder-sdk-aws-sdk-v2</artifactId>
68+
<version>2.4.0</version>
69+
</dependency>
70+
<dependency>
71+
<groupId>com.amazonaws</groupId>
72+
<artifactId>aws-xray-recorder-sdk-aws-sdk-v2-instrumentor</artifactId>
73+
<version>2.4.0</version>
74+
</dependency>
75+
<dependency>
76+
<groupId>org.junit.jupiter</groupId>
77+
<artifactId>junit-jupiter-api</artifactId>
78+
<version>5.6.0</version>
79+
<scope>test</scope>
80+
</dependency>
81+
<dependency>
82+
<groupId>org.junit.jupiter</groupId>
83+
<artifactId>junit-jupiter-engine</artifactId>
84+
<version>5.6.0</version>
85+
<scope>test</scope>
86+
</dependency>
87+
88+
</dependencies>
89+
90+
<build>
91+
<plugins>
92+
<plugin>
93+
<artifactId>maven-surefire-plugin</artifactId>
94+
<version>2.22.2</version>
95+
</plugin>
96+
<plugin>
97+
<groupId>org.apache.maven.plugins</groupId>
98+
<artifactId>maven-shade-plugin</artifactId>
99+
<version>3.2.2</version>
100+
<configuration>
101+
<createDependencyReducedPom>false</createDependencyReducedPom>
102+
</configuration>
103+
<executions>
104+
<execution>
105+
<phase>package</phase>
106+
<goals>
107+
<goal>shade</goal>
108+
</goals>
109+
<configuration>
110+
<transformers>
111+
<transformer implementation="com.github.edwgiz.maven_shade_plugin.log4j2_cache_transformer.PluginsCacheFileTransformer">
112+
</transformer>
113+
</transformers>
114+
</configuration>
115+
</execution>
116+
</executions>
117+
<dependencies>
118+
<dependency>
119+
<groupId>com.github.edwgiz</groupId>
120+
<artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
121+
<version>[2.17.1,)</version>
122+
</dependency>
123+
</dependencies>
124+
</plugin>
125+
<plugin>
126+
<groupId>org.apache.maven.plugins</groupId>
127+
<artifactId>maven-compiler-plugin</artifactId>
128+
<version>3.8.1</version>
129+
<configuration>
130+
<source>1.8</source>
131+
<target>1.8</target>
132+
</configuration>
133+
</plugin>
134+
</plugins>
135+
</build>
136+
</project>

0 commit comments

Comments
 (0)