Skip to content

Commit 4762b88

Browse files
committed
Add support for maven lambda archetype
1 parent 2c22928 commit 4762b88

File tree

33 files changed

+1025
-0
lines changed

33 files changed

+1025
-0
lines changed

archetypes/archetype-lambda/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Maven Archetype for lambda function using AWS SDK for Java 2.x
2+
3+
## Description
4+
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.
6+
7+
## Usage
8+
9+
You can use the following command to generate a project:
10+
11+
```
12+
mvn archetype:generate \
13+
-DarchetypeGroupId=software.amazon.awssdk \
14+
-DarchetypeArtifactId=lambda-archetypes \
15+
-DarchetypeVersion=2.x\
16+
```
17+
18+
To deploy the function, you can use [SAM CLI][sam-cli].
19+
20+
```
21+
sam deploy --guided
22+
```
23+
Please refer to [deploying lambda apps][deploying-lambda-apps] for more info.
24+
25+
## Parameters
26+
27+
Parameter Name | Default Value | Description
28+
---|---|---
29+
`service` (required) | n/a | Specifies service client to be used in the lambda function. You can find the eg: s3, dynamodb
30+
`region` (required) | n/a | Specifies region to be set for the SDK client in the application
31+
`groupId`(required) | n/a | Specifies the group ID of the project
32+
`artifactId`(required) | n/a | Specifies the artifact ID of the project
33+
`httpClient` | url-connection-client | Specifies which http client to be used by the SDK client, available options are `url-connection-client`, `apache-client`, `netty-nio-client`
34+
`handlerClassName` | `"App"`| Specifies the class name of the handler, which will be used as the lambda function name. It should use camel case.
35+
`version` | 1.0-SNAPSHOT | Specifies the version of the project
36+
`package` | ${groupId} | Specifies the package name for the classes
37+
38+
39+
[aws-java-sdk-v2]: https://github.com/aws/aws-sdk-java-v2
40+
[deploying-lambda-apps]: https://docs.aws.amazon.com/lambda/latest/dg/deploying-lambda-apps.html
41+
[sam-cli]:https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-getting-started.html
42+
43+
## References
44+
Maven archetype: https://maven.apache.org/archetype/maven-archetype-plugin/usage.html

archetypes/archetype-lambda/pom.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License").
6+
~ You may not use this file except in compliance with the License.
7+
~ A copy of the License is located at
8+
~
9+
~ http://aws.amazon.com/apache2.0
10+
~
11+
~ or in the "license" file accompanying this file. This file is distributed
12+
~ on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13+
~ express or implied. See the License for the specific language governing
14+
~ permissions and limitations under the License.
15+
-->
16+
17+
<project xmlns="http://maven.apache.org/POM/4.0.0"
18+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<parent>
21+
<artifactId>archetypes</artifactId>
22+
<groupId>software.amazon.awssdk</groupId>
23+
<version>2.10.71-SNAPSHOT</version>
24+
</parent>
25+
<modelVersion>4.0.0</modelVersion>
26+
<artifactId>archetype-lambda</artifactId>
27+
<packaging>maven-archetype</packaging>
28+
<name>AWS Java SDK :: Archetype Lambda</name>
29+
<description>
30+
The AWS SDK for Java - Maven archetype for Java lambda function using AWS Java SDK v2
31+
</description>
32+
33+
</project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<archetype-descriptor xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0 http://maven.apache.org/xsd/archetype-descriptor-1.0.0.xsd" name="zoewanglambdatesting"
3+
xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<fileSets>
6+
<fileSet filtered="true" packaged="true" encoding="UTF-8">
7+
<directory>src/main/java</directory>
8+
<includes>
9+
<include>**/*.java</include>
10+
</includes>
11+
</fileSet>
12+
<fileSet filtered="true" packaged="true" encoding="UTF-8">
13+
<directory>src/test/java</directory>
14+
<includes>
15+
<include>**/*.java</include>
16+
</includes>
17+
</fileSet>
18+
<fileSet filtered="true" encoding="UTF-8">
19+
<directory/>
20+
<includes>
21+
<include>.gitignore</include>
22+
<include>template.yaml</include>
23+
</includes>
24+
</fileSet>
25+
</fileSets>
26+
<requiredProperties>
27+
<requiredProperty key="handlerClassName">
28+
<defaultValue>App</defaultValue>
29+
</requiredProperty>
30+
<requiredProperty key="service">
31+
</requiredProperty>
32+
<requiredProperty key="httpClient">
33+
<defaultValue>url-connection-client</defaultValue>
34+
<validationRegex>(url-connection-client|apache-client|netty-nio-client)</validationRegex>
35+
</requiredProperty>
36+
<requiredProperty key="region">
37+
<validationRegex>^\w+-(\w+-)+\d+$</validationRegex>
38+
</requiredProperty>
39+
</requiredProperties>
40+
</archetype-descriptor>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Eclipse
2+
.classpath
3+
.project
4+
.settings/
5+
6+
# Intellij
7+
.idea/
8+
*.iml
9+
*.iws
10+
11+
# Mac
12+
.DS_Store
13+
14+
# Maven
15+
target/
16+
17+
**/dependency-reduced-pom.xml
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License").
6+
~ You may not use this file except in compliance with the License.
7+
~ A copy of the License is located at
8+
~
9+
~ http://aws.amazon.com/apache2.0
10+
~
11+
~ or in the "license" file accompanying this file. This file is distributed
12+
~ on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13+
~ express or implied. See the License for the specific language governing
14+
~ permissions and limitations under the License.
15+
-->
16+
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
18+
<modelVersion>4.0.0</modelVersion>
19+
20+
<groupId>${groupId}</groupId>
21+
<artifactId>${artifactId}</artifactId>
22+
<version>${version}</version>
23+
<packaging>jar</packaging>
24+
<properties>
25+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
26+
<maven.compiler.source>1.8</maven.compiler.source>
27+
<maven.compiler.target>1.8</maven.compiler.target>
28+
<maven.shade.plugin.version>3.1.1</maven.shade.plugin.version>
29+
<maven.compiler.plugin.version>3.6.1</maven.compiler.plugin.version>
30+
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
31+
<aws.java.sdk.version>2.10.66</aws.java.sdk.version>
32+
<aws.lambda.java.version>1.2.0</aws.lambda.java.version>
33+
<junit5.version>5.4.2</junit5.version>
34+
</properties>
35+
36+
<dependencyManagement>
37+
<dependencies>
38+
<dependency>
39+
<groupId>software.amazon.awssdk</groupId>
40+
<artifactId>bom</artifactId>
41+
<version>${aws.java.sdk.version}</version>
42+
<type>pom</type>
43+
<scope>import</scope>
44+
</dependency>
45+
</dependencies>
46+
</dependencyManagement>
47+
48+
<dependencies>
49+
<dependency>
50+
<groupId>software.amazon.awssdk</groupId>
51+
<artifactId>${service}</artifactId>
52+
<exclusions>
53+
<exclusion>
54+
<groupId>software.amazon.awssdk</groupId>
55+
<artifactId>netty-nio-client</artifactId>
56+
</exclusion>
57+
<exclusion>
58+
<groupId>software.amazon.awssdk</groupId>
59+
<artifactId>apache-client</artifactId>
60+
</exclusion>
61+
</exclusions>
62+
</dependency>
63+
64+
<dependency>
65+
<groupId>software.amazon.awssdk</groupId>
66+
<artifactId>${httpClient}</artifactId>
67+
</dependency>
68+
69+
<dependency>
70+
<groupId>com.amazonaws</groupId>
71+
<artifactId>aws-lambda-java-core</artifactId>
72+
<version>${aws.lambda.java.version}</version>
73+
</dependency>
74+
75+
<!-- Test Dependencies -->
76+
<dependency>
77+
<groupId>org.junit.jupiter</groupId>
78+
<artifactId>junit-jupiter</artifactId>
79+
<version>${junit5.version}</version>
80+
<scope>test</scope>
81+
</dependency>
82+
</dependencies>
83+
84+
<build>
85+
<plugins>
86+
<plugin>
87+
<groupId>org.apache.maven.plugins</groupId>
88+
<artifactId>maven-compiler-plugin</artifactId>
89+
<version>${maven.compiler.plugin.version}</version>
90+
</plugin>
91+
<plugin>
92+
<groupId>org.apache.maven.plugins</groupId>
93+
<artifactId>maven-shade-plugin</artifactId>
94+
<version>${maven.shade.plugin.version}</version>
95+
<configuration>
96+
<createDependencyReducedPom>false</createDependencyReducedPom>
97+
<finalName>${artifactId}</finalName>
98+
</configuration>
99+
<executions>
100+
<execution>
101+
<phase>package</phase>
102+
<goals>
103+
<goal>shade</goal>
104+
</goals>
105+
</execution>
106+
</executions>
107+
</plugin>
108+
</plugins>
109+
</build>
110+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#parse ( "global.vm")
2+
3+
package ${package};
4+
5+
import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider;
6+
import software.amazon.awssdk.http.${httpClientPackageName};
7+
import software.amazon.awssdk.regions.Region;
8+
import software.amazon.awssdk.services.${servicePackage}.${serviceClientClassName};
9+
10+
/**
11+
* The module containing all dependencies required by the {@link ${handlerClassName}}.
12+
*/
13+
public class DependencyFactory {
14+
15+
private DependencyFactory() {}
16+
17+
/**
18+
* @return an instance of ${serviceClientClassName}
19+
*/
20+
public static ${serviceClientClassName} ${service}Client() {
21+
return ${serviceClientClassName}.builder()
22+
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
23+
.region(Region.${regionEnum})
24+
.httpClientBuilder(${httpClientClassName}.builder())
25+
.build();
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#parse ( "global.vm")
2+
package ${package};
3+
4+
import com.amazonaws.services.lambda.runtime.Context;
5+
import com.amazonaws.services.lambda.runtime.RequestHandler;
6+
import software.amazon.awssdk.services.${servicePackage}.${serviceClientClassName};
7+
8+
/**
9+
* Lambda function entry point. You can change to use other pojo type or implement
10+
* a different RequestHandler.
11+
*
12+
* @see <a href=https://docs.aws.amazon.com/lambda/latest/dg/java-handler.html>Lambda Java Handler</a> for more information
13+
*/
14+
public class ${handlerClassName} implements RequestHandler<Object, Object> {
15+
private final ${serviceClientClassName} ${service}Client;
16+
17+
public ${handlerClassName}() {
18+
// Initialize the SDK client outside of the handler method so that it can be reused for subsequent invocations.
19+
// It is initialized when the class is loaded.
20+
${service}Client = DependencyFactory.${service}Client();
21+
// Consider invoking a simple api here to pre-warm up the application, eg: dynamodb#listTables
22+
}
23+
24+
@Override
25+
public Object handleRequest(final Object input, final Context context) {
26+
// TODO: invoking the api call using ${service}Client.
27+
return input;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#set( $symbol_pound = '#' )
2+
#set( $symbol_dollar = '$' )
3+
#set( $symbol_escape = '\' )
4+
package ${package};
5+
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
public class ${handlerClassName}Test {
11+
12+
@Test
13+
public void handleRequest_shouldReturnConstantValue() {
14+
${handlerClassName} function = new ${handlerClassName}();
15+
Object result = function.handleRequest("echo", null);
16+
assertEquals("echo", result);
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Resources:
4+
${handlerClassName}Function:
5+
Type: AWS::Serverless::Function
6+
Properties:
7+
Runtime: java8
8+
Handler: ${package}.${handlerClassName}::handleRequest
9+
Timeout: 60
10+
MemorySize: 512
11+
CodeUri: ./target/${artifactId}.jar
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## global variables used by the project
2+
#set( $symbol_pound = '#' )
3+
#set( $symbol_dollar = '$' )
4+
#set( $symbol_escape = '\' )
5+
## TODO: find a better way to handle this
6+
#if( $service == 'dynamodb')
7+
#set ( $service = 'dynamoDb')
8+
#end
9+
#set ( $servicePackage = $service.toLowerCase())
10+
#set( $serviceClientPrefix = $service.substring(0,1).toUpperCase() + $service.substring(1))
11+
#set( $regionEnum = $region.replace("-", "_").toUpperCase() )
12+
#if( $httpClient == 'url-connection-client')
13+
#set ($httpClientClassName = 'UrlConnectionHttpClient')
14+
#set ($httpClientPackageName = 'urlconnection.' + $httpClientClassName)
15+
#set ($serviceClientClassName = $serviceClientPrefix + 'Client')
16+
#elseif ( $httpClient == 'apache-client')
17+
#set ($httpClientClassName = 'ApacheHttpClient')
18+
#set ($httpClientPackageName = 'apache.' + $httpClientClassName)
19+
#set ($serviceClientClassName = $serviceClientPrefix + 'Client')
20+
#elseif ( $httpClient == 'netty-nio-client')
21+
#set ($httpClientClassName = 'NettyNioAsyncHttpClient')
22+
#set ($httpClientPackageName = 'nio.netty.' + $httpClientClassName)
23+
#set ($serviceClientClassName = $serviceClientPrefix + 'AsyncClient')
24+
#end
25+
#set( $servicePackage = $service.toLowerCase())
26+
#set( $serviceClientPrefix = $service.substring(0,1).toUpperCase() + $service.substring(1))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
groupId=software.amazonaws.test
2+
artifactId=test-apache-artifact
3+
version=1.0-SNAPSHOT
4+
package=software.amazonaws.test
5+
service=dynamodb
6+
httpClient=apache-client
7+
handlerClassName=MyApacheFunction
8+
region=ap-southeast-1
9+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
verify

0 commit comments

Comments
 (0)