Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "feature",
"category": "Lambda Maven Archetype",
"contributor": "",
"description": "Various Java Lambda Maven archetype improvements: use Java 25, use platform specific AWS CRT dependency, bump dependency version, and improve README. See [#6115](https://github.com/aws/aws-sdk-java-v2/issues/6115)"
}
32 changes: 17 additions & 15 deletions archetypes/archetype-lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

## Description
This is an Apache Maven Archetype to create a lambda function template using [AWS Java SDK 2.x][aws-java-sdk-v2]. The generated template
has the optimized configurations and follows the best practices to reduce start up time.
has the optimized configurations and follows the best practices to reduce start up time. Please check out [AWS Dev Guide][aws-dev-guide] for more information.

## Usage

You can use `mvn archetype:generate` to generate a project using this archetype. See [maven archetype usage guidance][maven-archetype-usage] for more information.
Note that you need to replace `${version}` with the latest SDK version found in [README.md](./README.md).

- Interactive mode

```
mvn archetype:generate \
-DarchetypeGroupId=software.amazon.awssdk \
-DarchetypeArtifactId=archetype-lambda \
-DarchetypeVersion=2.x
-DarchetypeVersion=${version}
```

- Batch mode
Expand All @@ -23,26 +24,26 @@ mvn archetype:generate \
mvn archetype:generate \
-DarchetypeGroupId=software.amazon.awssdk \
-DarchetypeArtifactId=archetype-lambda \
-DarchetypeVersion=2.x \
-DarchetypeVersion=${version} \
-DgroupId=com.test \
-DartifactId=sample-project \
-Dservice=s3 \
-DinteractiveMode=false
```

### Parameters
Parameter Name | Default Value | Description
---|---|---
`service` (required) | n/a | Specifies the service client to be used in the lambda function, eg: s3, dynamodb. You can find available services [here][java-sdk-v2-services].
`groupId`(required) | n/a | Specifies the group ID of the project
`artifactId`(required) | n/a | Specifies the artifact ID of the project
`region` | n/a | Specifies the region to be set for the SDK client in the application
`httpClient` | url-connection-client | Specifies the http client to be used by the SDK client. Available options are `url-connection-client` (sync), `apache-client` (sync), `netty-nio-client` (async). See [http clients][sdk-http-clients]
`handlerClassName` | `"App"`| Specifies the class name of the handler, which will be used as the lambda function name. It should be camel case.
`javaSdkVersion` | Same version as the archetype version | Specifies the version of the AWS Java SDK 2.x to be used
`version` | 1.0-SNAPSHOT | Specifies the version of the project
`package` | ${groupId} | Specifies the package name for the classes

Parameter Name | Default Value | Description
------------------------|---------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
`service` (required) | n/a | Specifies the service client to be used in the lambda function, eg: s3, dynamodb. You can find available services [here][java-sdk-v2-services].
`groupId`(required) | n/a | Specifies the group ID of the project
`artifactId`(required) | n/a | Specifies the artifact ID of the project
`region` | n/a | Specifies the region to be set for the SDK client in the application
`httpClient` | aws-crt-client | Specifies the http client to be used by the SDK client. Available options are `url-connection-client` (sync), `apache-client` (sync), `netty-nio-client` (async) and `aws-crt-client`(async). See [http clients][sdk-http-clients]
`handlerClassName` | `"App"` | Specifies the class name of the handler, which will be used as the lambda function name. It should be camel case.
`javaSdkVersion` | Same version as the archetype version | Specifies the version of the AWS Java SDK 2.x to be used
`version` | 1.0-SNAPSHOT | Specifies the version of the project
`package` | ${groupId} | Specifies the package name for the classes

### Deployment

Expand All @@ -62,3 +63,4 @@ Please refer to [deploying lambda apps][deploying-lambda-apps] for more info.
[sam-cli]:https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-getting-started.html
[maven-archetype-usage]: https://maven.apache.org/archetype/maven-archetype-plugin/usage.html
[sam-template]: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html
[aws-dev-guide]: https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/lambda-optimize-starttime.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,8 @@
<requiredProperty key="nettyOpenSslVersion">
<defaultValue>${netty-open-ssl-version}</defaultValue>
</requiredProperty>
<requiredProperty key="awsCrtVersion">
<defaultValue>${awscrt.version}</defaultValue>
</requiredProperty>
</requiredProperties>
</archetype-descriptor>
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
This project contains an AWS Lambda maven application with [AWS Java SDK 2.x](https://github.com/aws/aws-sdk-java-v2) dependencies.

#[[##]]# Prerequisites
- Java 1.8+
- Java 25+
- Apache Maven
- [AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)
- Docker

#[[##]]# Development

The generated function handler class just returns the input. The configured AWS Java SDK client is created in `DependencyFactory` class and you can
The generated function handler class just returns empty string. The configured AWS Java SDK client is created in `DependencyFactory` class and you can
add the code to interact with the SDK client based on your use case.

#[[####]]# Building the project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- TODO: change this to the Java version you use in your application -->
<maven.compiler.source>1.8</maven.compiler.source>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that we can't change this to 25 directly unfortunately because tests would fail due to 25 requirement

<maven.compiler.target>1.8</maven.compiler.target>
<maven.shade.plugin.version>3.2.1</maven.shade.plugin.version>
<maven.compiler.plugin.version>3.6.1</maven.compiler.plugin.version>
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
<maven.shade.plugin.version>3.6.1</maven.shade.plugin.version>
<maven.compiler.plugin.version>3.14.1</maven.compiler.plugin.version>
<aws.java.sdk.version>${javaSdkVersion}</aws.java.sdk.version>
<aws.lambda.java.version>1.2.3</aws.lambda.java.version>
<aws.lambda.java.version>1.4.0</aws.lambda.java.version>
<junit5.version>5.10.1</junit5.version>
#if( $httpClient == 'netty-nio-client')
<netty.openssl.version>${nettyOpenSslVersion}</netty.openssl.version>
#end
#if( $httpClient == 'aws-crt-client')
<aws.crt.version>${awsCrtVersion}</aws.crt.version>
#end
</properties>

Expand Down Expand Up @@ -53,8 +56,24 @@
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>${httpClient}</artifactId>
#if( $httpClient == 'aws-crt-client')
<exclusions>
<exclusion>
<groupId>software.amazon.awssdk.crt</groupId>
<artifactId>aws-crt</artifactId>
</exclusion>
</exclusions>
#end
</dependency>

#if( $httpClient == 'aws-crt-client')
<dependency>
<groupId>software.amazon.awssdk.crt</groupId>
<artifactId>aws-crt</artifactId>
<version>${aws.crt.version}</version>
<classifier>linux-x86_64</classifier>
</dependency>
#end
#if( $httpClient == 'netty-nio-client')
<!-- Adding netty-tcnative dependency so that netty async client uses openSSL as sslProvider if supported
See https://github.com/aws/aws-sdk-java-v2/blob/master/docs/BestPractices.md#use-openssl-for-netty-async-client -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
package ${package};

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import java.util.Map;
import software.amazon.awssdk.services.${servicePackage}.${serviceClientClassName};

/**
Expand All @@ -11,7 +13,7 @@
*
* @see <a href=https://docs.aws.amazon.com/lambda/latest/dg/java-handler.html>Lambda Java Handler</a> for more information
*/
public class ${handlerClassName} implements RequestHandler<Object, Object> {
public class ${handlerClassName} implements RequestHandler<Map<String, String>, String> {
private final ${serviceClientClassName} ${serviceClientVariable}Client;

public ${handlerClassName}() {
Expand All @@ -22,8 +24,10 @@ public class ${handlerClassName} implements RequestHandler<Object, Object> {
}

@Override
public Object handleRequest(final Object input, final Context context) {
public String handleRequest(final Map<String, String> input, final Context context) {
LambdaLogger lambdaLogger = context.getLogger();
lambdaLogger.log("Start to handle request");
// TODO: invoking the api call using ${serviceClientVariable}Client.
return input;
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,7 @@
#set( $symbol_escape = '\' )
package ${package};

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class ${handlerClassName}Test {

@Test
public void handleRequest_shouldReturnConstantValue() {
${handlerClassName} function = new ${handlerClassName}();
Object result = function.handleRequest("echo", null);
assertEquals("echo", result);
}
// TODO: Add your tests here
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Resources:
${handlerClassName}Function:
Type: AWS::Serverless::Function
Properties:
Runtime: java17
Runtime: java25
Handler: ${package}.${handlerClassName}::handleRequest
Timeout: 60
MemorySize: 512
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ httpClient=apache-client
handlerClassName=MyApacheFunction
region=us-west-2
javaSdkVersion=2.21.0
nettyOpenSslVersion=2.0.29.Final
nettyOpenSslVersion=2.0.29.Final
awsCrtVersion=0.40.3
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
This project contains an AWS Lambda maven application with [AWS Java SDK 2.x](https://github.com/aws/aws-sdk-java-v2) dependencies.

## Prerequisites
- Java 1.8+
- Java 25+
- Apache Maven
- [AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)
- Docker

## Development

The generated function handler class just returns the input. The configured AWS Java SDK client is created in `DependencyFactory` class and you can
The generated function handler class just returns empty string. The configured AWS Java SDK client is created in `DependencyFactory` class and you can
add the code to interact with the SDK client based on your use case.

#### Building the project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- TODO: change this to the Java version you use in your application -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.shade.plugin.version>3.2.1</maven.shade.plugin.version>
<maven.compiler.plugin.version>3.6.1</maven.compiler.plugin.version>
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
<maven.shade.plugin.version>3.6.1</maven.shade.plugin.version>
<maven.compiler.plugin.version>3.14.1</maven.compiler.plugin.version>
<aws.java.sdk.version>2.21.0</aws.java.sdk.version>
<aws.lambda.java.version>1.2.3</aws.lambda.java.version>
<aws.lambda.java.version>1.4.0</aws.lambda.java.version>
<junit5.version>5.10.1</junit5.version>
</properties>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package software.amazonaws.test;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import java.util.Map;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;

/**
Expand All @@ -10,7 +12,7 @@
*
* @see <a href=https://docs.aws.amazon.com/lambda/latest/dg/java-handler.html>Lambda Java Handler</a> for more information
*/
public class MyApacheFunction implements RequestHandler<Object, Object> {
public class MyApacheFunction implements RequestHandler<Map<String, String>, String> {
private final DynamoDbClient dynamoDbClient;

public MyApacheFunction() {
Expand All @@ -21,8 +23,10 @@ public MyApacheFunction() {
}

@Override
public Object handleRequest(final Object input, final Context context) {
public String handleRequest(final Map<String, String> input, final Context context) {
LambdaLogger lambdaLogger = context.getLogger();
lambdaLogger.log("Start to handle request");
// TODO: invoking the api call using dynamoDbClient.
return input;
return "";
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
package software.amazonaws.test;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class MyApacheFunctionTest {

@Test
public void handleRequest_shouldReturnConstantValue() {
MyApacheFunction function = new MyApacheFunction();
Object result = function.handleRequest("echo", null);
assertEquals("echo", result);
}
// TODO: Add your tests here
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Resources:
MyApacheFunctionFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: java17
Runtime: java25
Handler: software.amazonaws.test.MyApacheFunction::handleRequest
Timeout: 60
MemorySize: 512
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ handlerClassName=MyCrtFunction
region=us-east-1
javaSdkVersion=2.21.0
nettyOpenSslVersion=2.0.29.Final
awsCrtVersion=0.40.3

Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
This project contains an AWS Lambda maven application with [AWS Java SDK 2.x](https://github.com/aws/aws-sdk-java-v2) dependencies.

## Prerequisites
- Java 1.8+
- Java 25+
- Apache Maven
- [AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)
- Docker

## Development

The generated function handler class just returns the input. The configured AWS Java SDK client is created in `DependencyFactory` class and you can
The generated function handler class just returns empty string. The configured AWS Java SDK client is created in `DependencyFactory` class and you can
add the code to interact with the SDK client based on your use case.

#### Building the project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- TODO: change this to the Java version you use in your application -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.shade.plugin.version>3.2.1</maven.shade.plugin.version>
<maven.compiler.plugin.version>3.6.1</maven.compiler.plugin.version>
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
<maven.shade.plugin.version>3.6.1</maven.shade.plugin.version>
<maven.compiler.plugin.version>3.14.1</maven.compiler.plugin.version>
<aws.java.sdk.version>2.21.0</aws.java.sdk.version>
<aws.lambda.java.version>1.2.3</aws.lambda.java.version>
<aws.lambda.java.version>1.4.0</aws.lambda.java.version>
<junit5.version>5.10.1</junit5.version>
<aws.crt.version>0.40.3</aws.crt.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -49,8 +50,20 @@
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>aws-crt-client</artifactId>
<exclusions>
<exclusion>
<groupId>software.amazon.awssdk.crt</groupId>
<artifactId>aws-crt</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>software.amazon.awssdk.crt</groupId>
<artifactId>aws-crt</artifactId>
<version>${aws.crt.version}</version>
<classifier>linux-x86_64</classifier>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
Expand Down
Loading
Loading