Skip to content

Files

Latest commit

Oct 31, 2023
c5f6ee4 · Oct 31, 2023

History

History

java-events

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Feb 23, 2023
May 4, 2020
Mar 2, 2023
Mar 24, 2020
Mar 24, 2020
Oct 31, 2023
May 4, 2020
Oct 31, 2023
Mar 2, 2023
Mar 26, 2020
Mar 2, 2023
Mar 2, 2023
Mar 2, 2023

README.md

Basic function with event library types (Java)

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.

Architecture

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.

Requirements

Setup

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

Deploy

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 ]---------------------------------
...

Test

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.

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.

Trace

Configure Handler Class

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 - Takes APIGatewayV2ProxyRequestEvent as input and returns APIGatewayV2ProxyResponseEvent.
  • HandlerApiGateway.java - Takes APIGatewayProxyRequestEvent as input and returns APIGatewayProxyResponseEvent.
  • HandlerCloudFront.java - Takes CloudFrontEvent as input.
  • HandlerCodeCommit.java - Takes CodeCommitEvent as input.
  • HandlerCognito.java - Takes CognitoEvent as input.
  • HandlerCWEvents.java - Takes ScheduledEventEvent as input.
  • HandlerCWLogs.java - Takes CloudWatchLogsEvent as input.
  • HandlerDynamoDB.java - Takes DynamodbEvent as input.
  • HandlerFirehose.java - Takes KinesisFirehoseEvent as input.
  • HandlerKinesis.java - Takes KinesisEvent as input.
  • HandlerLex.java - Takes LexEvent as input.
  • HandlerS3.java - Takes S3Event as input.
  • HandlerSNS.java - Takes SNSEvent as input.
  • HandlerSQS.java - Takes SQSEvent 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)

Cleanup

To delete the application, run 4-cleanup.sh.

java-events$ ./4-cleanup.sh