Skip to content

Add support for maven lambda archetype #1678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2020
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ artifact source.
* [Sample Code](#sample-code)
* [API Docs][docs-api]
* [Developer Guide][docs-guide] ([source][docs-guide-source])
* [Maven Archetypes](archetypes/README.md)
* [Issues][sdk-issues]
* [SDK Blog][blog]
* [Giving Feedback](#giving-feedback)
Expand Down
9 changes: 9 additions & 0 deletions archetypes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Maven Archetypes for AWS SDK for Java 2.x

## Description
This module contains maven archetypes for AWS Java SDK 2.x.

## Archetypes

- [archetype-lambda](archetype-lambda/README.md) - a lambda function template using AWS Java SDK 2.x

65 changes: 65 additions & 0 deletions archetypes/archetype-lambda/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Maven Archetype for lambda function using AWS SDK for Java 2.x

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

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

- Interactive mode

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

- Batch mode

```
mvn archetype:generate \
-DarchetypeGroupId=software.amazon.awssdk \
-DarchetypeArtifactId=archetype-lambda \
-DarchetypeVersion=2.x\
-DgroupId=com.test \
-DartifactId=sample-project \
-Dservice=s3 \
-Dregion=us-west-2 \
-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].
`region` (required) | n/a | Specifies the region to be set for the SDK client in the application
`groupId`(required) | n/a | Specifies the group ID of the project
`artifactId`(required) | n/a | Specifies the artifact ID of the project
`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

### Deployment

To deploy the lambda function, you can use [SAM CLI][sam-cli]. The generated project contains a default [SAM template][sam-template] file `template.yaml` where you can
configure different properties of your lambda function such as memory size and timeout.

```
sam deploy --guided
```

Please refer to [deploying lambda apps][deploying-lambda-apps] for more info.

[aws-java-sdk-v2]: https://github.com/aws/aws-sdk-java-v2
[java-sdk-v2-services]: https://github.com/aws/aws-sdk-java-v2/tree/master/services
[sdk-http-clients]: https://github.com/aws/aws-sdk-java-v2/tree/master/http-clients
[deploying-lambda-apps]: https://docs.aws.amazon.com/lambda/latest/dg/deploying-lambda-apps.html
[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
122 changes: 122 additions & 0 deletions archetypes/archetype-lambda/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
~
~ Licensed under the Apache License, Version 2.0 (the "License").
~ You may not use this file except in compliance with the License.
~ A copy of the License is located at
~
~ http://aws.amazon.com/apache2.0
~
~ or in the "license" file accompanying this file. This file is distributed
~ on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
~ express or implied. See the License for the specific language governing
~ permissions and limitations under the License.
-->

<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">
<parent>
<artifactId>archetypes</artifactId>
<groupId>software.amazon.awssdk</groupId>
<version>2.11.4-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>archetype-lambda</artifactId>
<packaging>maven-archetype</packaging>
<name>AWS Java SDK :: Archetype Lambda</name>
<description>
The AWS SDK for Java - Maven archetype for Java lambda function using AWS Java SDK 2.x
</description>

<properties>
<maven.archetype.version>3.1.2</maven.archetype.version>
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
</properties>

<dependencies>
<!-- Depends on the artifacts of all services to generate serviceMapping.vm -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>aws-sdk-java</artifactId>
<version>${awsjavasdk.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!-- Filtering the resource properties to get ${project.version} from archetype metadata.
See https://stackoverflow.com/a/22300149 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>META-INF/maven/archetype-metadata.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>META-INF/maven/archetype-metadata.xml</exclude>
</excludes>
</resource>
</resources>
<extensions>
<extension>
<groupId>org.apache.maven.archetype</groupId>
<artifactId>archetype-packaging</artifactId>
<version>${maven.archetype.version}</version>
</extension>
</extensions>

<plugins>
<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<version>${exec-maven-plugin.version}</version>
<executions>
<execution>
<id>map-service-to-client-prefix</id>
<phase>generate-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${basedir}/src/main/resources/map-service-to-client-prefix</executable>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-archetype-plugin</artifactId>
<version>${maven.archetype.version}</version>
<configuration>
<noLog>true</noLog>
<ignoreEOLStyle>true</ignoreEOLStyle>
<skip>${skip.unit.tests}</skip>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<phase>verify</phase>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin>

<!-- Skip dependency analysis because it's unnecessary for this module -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-dependency-plugin.version}</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<fileSets>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/test/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory/>
<includes>
<include>.gitignore</include>
<include>template.yaml</include>
<include>README.md</include>
</includes>
</fileSet>
</fileSets>
<requiredProperties>
<requiredProperty key="handlerClassName">
<defaultValue>App</defaultValue>
</requiredProperty>
<requiredProperty key="javaSdkVersion">
<defaultValue>${project.version}</defaultValue>
<validationRegex>\d+\.\d+.\d+</validationRegex>
</requiredProperty>
<requiredProperty key="service">
</requiredProperty>
<requiredProperty key="httpClient">
<defaultValue>url-connection-client</defaultValue>
<validationRegex>(url-connection-client|apache-client|netty-nio-client)</validationRegex>
</requiredProperty>
<requiredProperty key="region">
<validationRegex>^\w+-(\w+-)+\d+$</validationRegex>
</requiredProperty>
<!-- Required to pass the netty-open-ssl-version property from parent pom to velocity-->
<requiredProperty key="nettyOpenSslVersion">
<defaultValue>${netty-open-ssl-version}</defaultValue>
</requiredProperty>
</requiredProperties>
</archetype-descriptor>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Eclipse
.classpath
.project
.settings/

# Intellij
.idea/
*.iml
*.iws

# Mac
.DS_Store

# Maven
target/

**/dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#[[#]]# ${handlerClassName}

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+
- 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
add the code to interact with the SDK client based on your use case.

#[[####]]# Building the project
```
mvn clean install
```

#[[####]]# Testing it locally
```
sam local invoke
```

#[[####]]# Adding more SDK clients
To add more service clients, you need to add the specific services modules in `pom.xml` and create the clients in `DependencyFactory` following the same
pattern as ${serviceClientVariable}Client.

#[[##]]# Deployment

The generated project contains a default [SAM template](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html) file `template.yaml` where you can
configure different properties of your lambda function such as memory size and timeout. You might also need to add specific policies to the lambda function
so that it can access other AWS resources.

To deploy the application, you can run the following command:

```
sam deploy --guided
```

See [Deploying Serverless Applications](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-deploying.html) for more info.



Loading