This sample application shows the use of the aws-lambda-java-events
library with various event types. To keep the deployment size minimal, it includes only types that can be used without adding the AWS SDK as a dependency. A separate handler class is defined for each input type.
Note: To use these examples, you must be using version 3.0.0 or newer of the aws-lambda-java-events
dependency. If you are on an older version, see the java-events-v1sdk
package for deprecated examples. If possible, update your aws-lambda-java-events
dependency to version 3.0.0 or newer.
The project includes function code and supporting resources:
src/main
- A Java function.src/test
- A unit test and helper classes.template.yml
- An AWS CloudFormation template that creates an application.build.gradle
- A Gradle build file.pom.xml
- A Maven build file.1-create-bucket.sh
,2-deploy.sh
, etc. - Shell scripts that use the AWS CLI to deploy and manage the application.
Use the following instructions to deploy the sample application.
- Java 8 runtime environment (SE JRE)
- Gradle 5 or Maven 3
- The Bash shell. For Linux and macOS, this is included by default. In Windows 10, you can install the Windows Subsystem for Linux to get a Windows-integrated version of Ubuntu and Bash.
- The AWS CLI v1.17 or newer.
Download or clone this repository.
$ git clone https://github.com/awsdocs/aws-lambda-developer-guide.git
$ cd aws-lambda-developer-guide/sample-apps/java-events
Run 1-create-bucket.sh
to create a new bucket for deployment artifacts.
java-events$ ./1-create-bucket.sh
make_bucket: lambda-artifacts-a5e4xmplb5b22e0d
Run 2-deploy.sh
to build the application with Gradle and deploy it.
java-events$ ./2-deploy.sh
BUILD SUCCESSFUL in 1s
Successfully packaged artifacts and wrote output template to file out.yml.
Waiting for changeset to be created..
Successfully created/updated stack - java-events
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.
You can also build the application with Maven. To use maven, add mvn
to the command.
java-events$ ./2-deploy.sh mvn
[INFO] Scanning for projects...
[INFO] -----------------------< com.example:java-events >-----------------------
[INFO] Building java-events-function 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
...
Run 3-invoke.sh
to invoke the function. The default handler (Handler.java
) processes an event from an Amazon API Gateway HTTP API and returns a JSON representation of an HTTP response.
java-events$ ./3-invoke.sh
{
"StatusCode": 200,
"ExecutedVersion": "$LATEST"
}
{"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>"}
Let the script invoke the function a few times and then press CRTL+C
to exit.
The application uses AWS X-Ray to trace requests. Open the X-Ray console to view the service map.
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.
By default, the function uses a handler class named Handler
that takes an API Gateway proxy event as input and returns a string. The project also includes handlers that use other input and output types. The handlers are defined in the following files under src/main/java/example
:
Handler.java
- TakesAPIGatewayV2ProxyRequestEvent
as input and returnsAPIGatewayV2ProxyResponseEvent
.HandlerApiGateway.java
- TakesAPIGatewayProxyRequestEvent
as input and returnsAPIGatewayProxyResponseEvent
.HandlerCloudFront.java
- TakesCloudFrontEvent
as input.HandlerCodeCommit.java
- TakesCodeCommitEvent
as input.HandlerCognito.java
- TakesCognitoEvent
as input.HandlerCWEvents.java
- TakesScheduledEventEvent
as input.HandlerCWLogs.java
- TakesCloudWatchLogsEvent
as input.HandlerDynamoDB.java
- TakesDynamodbEvent
as input.HandlerFirehose.java
- TakesKinesisFirehoseEvent
as input.HandlerKinesis.java
- TakesKinesisEvent
as input.HandlerLex.java
- TakesLexEvent
as input.HandlerS3.java
- TakesS3Event
as input.HandlerSNS.java
- TakesSNSEvent
as input.HandlerSQS.java
- TakesSQSEvent
as input.
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 Amazon Lex handler:
Properties:
CodeUri: build/distributions/java-events.zip
Handler: example.HandlerLex
Deploy the change, and then use the invoke script to test the new configuration. Pass the handler type key as an argument to the invoke script.
./3-invoke.sh lex
{
"StatusCode": 200,
"ExecutedVersion": "$LATEST"
}
"200 OK"
The following event type keys are supported:
- none - API Gateway HTTP API (
events/apigateway-v2.json
) apig
- API Gateway REST API (events/apigateway-v1.json
)cws
- CloudWatch scheduled event (events/cloudwatch-scheduled.json
)cwl
- CloudWatch Logs (events/cloudwatch-logs.json
)sns
- SNS notification (events/sns-notification.json
)cfg
- Config rule (events/config-rule.json
)cc
- CodeCommit push (events/codecommit-push.json
)cog
- Cognito Sync (events/cognito-sync.json
)kin
- Kinesis record (events/kinesis-record.json
)fh
- Kinesis Firehose record (events/firehose-record.json
)lex
- Lex dialog (events/lex-flowers.json
)ddb
- DynamoDB record (events/dynamodb-record.json
)s3
- S3Event record (events/s3-notification.json
)sqs
- SQSEvent record (events/sqs-record.json
)
To delete the application, run 4-cleanup.sh
.
java-events$ ./4-cleanup.sh