Skip to content

Latest commit

 

History

History

java-example-extension

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Example Extension in Java 11

The project source includes function code and supporting resources:

  • src/main - A Java extension.
  • pom.xml - A Maven build file.
  • run.sh - Shell script that will build the extension, create zip file, publish the lambda layer and update the lambda function with the latest version of this extension
  • zip.sh - Shell script that builds the code and packages it extension.zip

Use the following instructions to setup and deploy the sample extension.

Requirements

  • Corretto 11
  • 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.
  • Since this is an external java extension we would require a lambda function with java 11 runtime published to test this extension. For more information on publishing a blank java lambda function please refer here

If you use the AWS CLI v2, add the following to your configuration file (~/.aws/config):

cli_binary_format=raw-in-base64-out

This setting enables the AWS CLI v2 to load JSON events from a file, matching the v1 behavior.

Setup

Download or clone this repository.

$ git clone https://github.com/aws-samples/aws-lambda-extensions.git
$ cd java-example-extension

To build a Lambda layer that contains the extension dependencies, run mvn clean install. Packaging dependencies in a layer reduces the size of the deployment package that you upload when you modify your code.

java-example-extension$ mvn clean install

Deployment instruction

You can deploy the extension by either following step-by-step instruction or using the shell script

Step-by-step instruction

  1. Build and create extension.zip
  • Change the permission on the executable - Run the following command to change the permission of zip.sh

    java-example-extension$ chmod +x zip.sh
    
  • Execute zip.sh - Run the following command to build the project and create extension.zip

    java-example-extension$ ./zip.sh
    
  1. Set the environment variables - Run the following command to update environment variables so they can point to actual artifacts
LAMBDA_FUNCTION=<<LambdaFunctionName>>
  1. Deploy the extension - Run the following command to deploy the extension as a lambda layer
java-example-extension$ aws lambda publish-layer-version \
--layer-name "java-example-extension" \
--zip-file "fileb://extension.zip"
  1. Update the lambda function with the layer - Run the following command to update the lambda function to point to the latest version of the lambda layer that we uploaded in the previous step
java-example-extension$ aws lambda update-function-configuration \
--function-name ${LAMBDA_FUNCTION} \
--layers $(aws lambda list-layer-versions --layer-name java-example-extension --max-items 1 \
--no-paginate --query 'LayerVersions[0].LayerVersionArn' --output text)

Deploy using the shell script

To deploy the extension using the bash script do the following:

  • Change the permission of the executable - Run the following command to change the permission of run.sh

    java-example-extension$ chmod +x run.sh
    
  • Execute run.sh - Run the following command run.sh <<extension-name>> <<function-name>> with the extension name and function name as parameters.

    java-example-extension$ ./run.sh java-example-extension blank-java
    

This script uses AWS CLI to perform the following:

  • Builds the lambda extension
  • Creates deployment zip file named as "extension.zip"
  • Push the latest extension.zip to lambda layer
  • Update the lambda function with the latest version of the lambda layer published in the previous step

Testing

  • To invoke the function, running the following command using AWS CLI
 java-example-extension$ aws lambda invoke \
 --function-name "${LAMBDA_FUNCTION}" \
 --payload '{"payload": "hello"}' /tmp/invoke-result \
 --cli-binary-format raw-in-base64-out --log-type Tail