Skip to content

Commit dce7419

Browse files
committed
Improve archetype-lambda
1 parent e05589d commit dce7419

File tree

46 files changed

+156
-401
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+156
-401
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "feature",
3+
"category": "Lambda Maven Archetype",
4+
"contributor": "",
5+
"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)"
6+
}

archetypes/archetype-lambda/README.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22

33
## Description
44
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
5-
has the optimized configurations and follows the best practices to reduce start up time.
5+
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.
66

77
## Usage
88

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

1112
- Interactive mode
1213

1314
```
1415
mvn archetype:generate \
1516
-DarchetypeGroupId=software.amazon.awssdk \
1617
-DarchetypeArtifactId=archetype-lambda \
17-
-DarchetypeVersion=2.x
18+
-DarchetypeVersion=${version}
1819
```
1920

2021
- Batch mode
@@ -23,26 +24,26 @@ mvn archetype:generate \
2324
mvn archetype:generate \
2425
-DarchetypeGroupId=software.amazon.awssdk \
2526
-DarchetypeArtifactId=archetype-lambda \
26-
-DarchetypeVersion=2.x \
27+
-DarchetypeVersion=${version} \
2728
-DgroupId=com.test \
2829
-DartifactId=sample-project \
2930
-Dservice=s3 \
3031
-DinteractiveMode=false
3132
```
3233

3334
### Parameters
34-
35-
Parameter Name | Default Value | Description
36-
---|---|---
37-
`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].
38-
`groupId`(required) | n/a | Specifies the group ID of the project
39-
`artifactId`(required) | n/a | Specifies the artifact ID of the project
40-
`region` | n/a | Specifies the region to be set for the SDK client in the application
41-
`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]
42-
`handlerClassName` | `"App"`| Specifies the class name of the handler, which will be used as the lambda function name. It should be camel case.
43-
`javaSdkVersion` | Same version as the archetype version | Specifies the version of the AWS Java SDK 2.x to be used
44-
`version` | 1.0-SNAPSHOT | Specifies the version of the project
45-
`package` | ${groupId} | Specifies the package name for the classes
35+
36+
Parameter Name | Default Value | Description
37+
------------------------|---------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
38+
`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].
39+
`groupId`(required) | n/a | Specifies the group ID of the project
40+
`artifactId`(required) | n/a | Specifies the artifact ID of the project
41+
`region` | n/a | Specifies the region to be set for the SDK client in the application
42+
`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]
43+
`handlerClassName` | `"App"` | Specifies the class name of the handler, which will be used as the lambda function name. It should be camel case.
44+
`javaSdkVersion` | Same version as the archetype version | Specifies the version of the AWS Java SDK 2.x to be used
45+
`version` | 1.0-SNAPSHOT | Specifies the version of the project
46+
`package` | ${groupId} | Specifies the package name for the classes
4647

4748
### Deployment
4849

@@ -62,3 +63,4 @@ Please refer to [deploying lambda apps][deploying-lambda-apps] for more info.
6263
[sam-cli]:https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-getting-started.html
6364
[maven-archetype-usage]: https://maven.apache.org/archetype/maven-archetype-plugin/usage.html
6465
[sam-template]: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html
66+
[aws-dev-guide]: https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/lambda-optimize-starttime.html

archetypes/archetype-lambda/src/main/resources/META-INF/maven/archetype-metadata.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,8 @@
4646
<requiredProperty key="nettyOpenSslVersion">
4747
<defaultValue>${netty-open-ssl-version}</defaultValue>
4848
</requiredProperty>
49+
<requiredProperty key="awsCrtVersion">
50+
<defaultValue>${awscrt.version}</defaultValue>
51+
</requiredProperty>
4952
</requiredProperties>
5053
</archetype-descriptor>

archetypes/archetype-lambda/src/main/resources/archetype-resources/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
This project contains an AWS Lambda maven application with [AWS Java SDK 2.x](https://github.com/aws/aws-sdk-java-v2) dependencies.
44

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

1111
#[[##]]# Development
1212

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

1616
#[[####]]# Building the project

archetypes/archetype-lambda/src/main/resources/archetype-resources/pom.xml

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@
99
<packaging>jar</packaging>
1010
<properties>
1111
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<!-- TODO: change this to the Java version you use in your application -->
1213
<maven.compiler.source>1.8</maven.compiler.source>
1314
<maven.compiler.target>1.8</maven.compiler.target>
14-
<maven.shade.plugin.version>3.2.1</maven.shade.plugin.version>
15-
<maven.compiler.plugin.version>3.6.1</maven.compiler.plugin.version>
16-
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
15+
<maven.shade.plugin.version>3.6.1</maven.shade.plugin.version>
16+
<maven.compiler.plugin.version>3.14.1</maven.compiler.plugin.version>
1717
<aws.java.sdk.version>${javaSdkVersion}</aws.java.sdk.version>
18-
<aws.lambda.java.version>1.2.3</aws.lambda.java.version>
18+
<aws.lambda.java.version>1.4.0</aws.lambda.java.version>
1919
<junit5.version>5.10.1</junit5.version>
2020
#if( $httpClient == 'netty-nio-client')
2121
<netty.openssl.version>${nettyOpenSslVersion}</netty.openssl.version>
22+
#end
23+
#if( $httpClient == 'aws-crt-client')
24+
<aws.crt.version>${awsCrtVersion}</aws.crt.version>
2225
#end
2326
</properties>
2427

@@ -53,8 +56,24 @@
5356
<dependency>
5457
<groupId>software.amazon.awssdk</groupId>
5558
<artifactId>${httpClient}</artifactId>
59+
#if( $httpClient == 'aws-crt-client')
60+
<exclusions>
61+
<exclusion>
62+
<groupId>software.amazon.awssdk.crt</groupId>
63+
<artifactId>aws-crt</artifactId>
64+
</exclusion>
65+
</exclusions>
66+
#end
5667
</dependency>
5768

69+
#if( $httpClient == 'aws-crt-client')
70+
<dependency>
71+
<groupId>software.amazon.awssdk.crt</groupId>
72+
<artifactId>aws-crt</artifactId>
73+
<version>${aws.crt.version}</version>
74+
<classifier>linux-x86_64</classifier>
75+
</dependency>
76+
#end
5877
#if( $httpClient == 'netty-nio-client')
5978
<!-- Adding netty-tcnative dependency so that netty async client uses openSSL as sslProvider if supported
6079
See https://github.com/aws/aws-sdk-java-v2/blob/master/docs/BestPractices.md#use-openssl-for-netty-async-client -->

archetypes/archetype-lambda/src/main/resources/archetype-resources/src/main/java/__handlerClassName__.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
package ${package};
33

44
import com.amazonaws.services.lambda.runtime.Context;
5+
import com.amazonaws.services.lambda.runtime.LambdaLogger;
56
import com.amazonaws.services.lambda.runtime.RequestHandler;
7+
import java.util.Map;
68
import software.amazon.awssdk.services.${servicePackage}.${serviceClientClassName};
79

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

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

2426
@Override
25-
public Object handleRequest(final Object input, final Context context) {
27+
public String handleRequest(final Map<String, String> input, final Context context) {
28+
LambdaLogger lambdaLogger = context.getLogger();
29+
lambdaLogger.log("Start to handle request");
2630
// TODO: invoking the api call using ${serviceClientVariable}Client.
27-
return input;
31+
return "";
2832
}
2933
}

archetypes/archetype-lambda/src/main/resources/archetype-resources/src/test/java/__handlerClassName__Test.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,7 @@
33
#set( $symbol_escape = '\' )
44
package ${package};
55

6-
import static org.junit.jupiter.api.Assertions.assertEquals;
7-
8-
import org.junit.jupiter.api.Test;
9-
106
public class ${handlerClassName}Test {
117

12-
@Test
13-
public void handleRequest_shouldReturnConstantValue() {
14-
${handlerClassName} function = new ${handlerClassName}();
15-
Object result = function.handleRequest("echo", null);
16-
assertEquals("echo", result);
17-
}
8+
// TODO: Add your tests here
189
}

archetypes/archetype-lambda/src/main/resources/archetype-resources/template.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Resources:
66
${handlerClassName}Function:
77
Type: AWS::Serverless::Function
88
Properties:
9-
Runtime: java17
9+
Runtime: java25
1010
Handler: ${package}.${handlerClassName}::handleRequest
1111
Timeout: 60
1212
MemorySize: 512

archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/archetype.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ httpClient=apache-client
77
handlerClassName=MyApacheFunction
88
region=us-west-2
99
javaSdkVersion=2.21.0
10-
nettyOpenSslVersion=2.0.29.Final
10+
nettyOpenSslVersion=2.0.29.Final
11+
awsCrtVersion=0.40.3

archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
This project contains an AWS Lambda maven application with [AWS Java SDK 2.x](https://github.com/aws/aws-sdk-java-v2) dependencies.
44

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

1111
## Development
1212

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

1616
#### Building the project

0 commit comments

Comments
 (0)