From 29cafa7734cd0ad5b0446ddfb83b14de1c6d62e8 Mon Sep 17 00:00:00 2001 From: Zoe Wang Date: Tue, 25 Feb 2020 17:56:16 -0800 Subject: [PATCH] Add support for maven lambda archetype --- README.md | 1 + archetypes/README.md | 9 ++ archetypes/archetype-lambda/README.md | 65 ++++++++++ archetypes/archetype-lambda/pom.xml | 122 ++++++++++++++++++ .../META-INF/maven/archetype-metadata.xml | 49 +++++++ .../resources/archetype-resources/.gitignore | 17 +++ .../resources/archetype-resources/README.md | 45 +++++++ .../resources/archetype-resources/pom.xml | 108 ++++++++++++++++ .../src/main/java/DependencyFactory.java | 27 ++++ .../src/main/java/__handlerClassName__.java | 29 +++++ .../test/java/__handlerClassName__Test.java | 18 +++ .../archetype-resources/template.yaml | 19 +++ .../src/main/resources/global.vm | 36 ++++++ .../resources/map-service-to-client-prefix | 50 +++++++ .../apachehttpclient/archetype.properties | 10 ++ .../projects/apachehttpclient/goal.txt | 1 + .../apachehttpclient/reference/README.md | 45 +++++++ .../apachehttpclient/reference/pom.xml | 95 ++++++++++++++ .../amazonaws/test/DependencyFactory.java | 26 ++++ .../amazonaws/test/MyApacheFunction.java | 28 ++++ .../amazonaws/test/MyApacheFunctionTest.java | 15 +++ .../apachehttpclient/reference/template.yaml | 19 +++ .../archetype.properties | 25 ++++ .../projects/dynamodbstreamsclient/goal.txt | 1 + .../dynamodbstreamsclient/reference/README.md | 45 +++++++ .../dynamodbstreamsclient/reference/pom.xml | 95 ++++++++++++++ .../amazonaws/test/DependencyFactory.java | 26 ++++ .../test/MyDynamoDbStreamsFunction.java | 28 ++++ .../test/MyDynamoDbStreamsFunctionTest.java | 15 +++ .../reference/template.yaml | 19 +++ .../projects/nettyclient/archetype.properties | 11 ++ .../resources/projects/nettyclient/goal.txt | 1 + .../projects/nettyclient/reference/README.md | 45 +++++++ .../projects/nettyclient/reference/pom.xml | 103 +++++++++++++++ .../amazonaws/test/DependencyFactory.java | 26 ++++ .../amazonaws/test/MyNettyFunction.java | 28 ++++ .../amazonaws/test/MyNettyFunctionTest.java | 15 +++ .../nettyclient/reference/template.yaml | 19 +++ .../urlhttpclient/archetype.properties | 10 ++ .../resources/projects/urlhttpclient/goal.txt | 1 + .../urlhttpclient/reference/README.md | 45 +++++++ .../projects/urlhttpclient/reference/pom.xml | 95 ++++++++++++++ .../java/software/amazonaws/test/App.java | 28 ++++ .../amazonaws/test/DependencyFactory.java | 26 ++++ .../java/software/amazonaws/test/AppTest.java | 15 +++ .../urlhttpclient/reference/template.yaml | 19 +++ .../wafregionalclient/archetype.properties | 25 ++++ .../projects/wafregionalclient/goal.txt | 1 + .../wafregionalclient/reference/README.md | 45 +++++++ .../wafregionalclient/reference/pom.xml | 95 ++++++++++++++ .../amazonaws/test/DependencyFactory.java | 26 ++++ .../amazonaws/test/MyWafRegionalFunction.java | 28 ++++ .../test/MyWafRegionalFunctionTest.java | 15 +++ .../wafregionalclient/reference/template.yaml | 19 +++ archetypes/pom.xml | 35 +++++ pom.xml | 1 + 56 files changed, 1865 insertions(+) create mode 100755 archetypes/README.md create mode 100755 archetypes/archetype-lambda/README.md create mode 100644 archetypes/archetype-lambda/pom.xml create mode 100644 archetypes/archetype-lambda/src/main/resources/META-INF/maven/archetype-metadata.xml create mode 100644 archetypes/archetype-lambda/src/main/resources/archetype-resources/.gitignore create mode 100644 archetypes/archetype-lambda/src/main/resources/archetype-resources/README.md create mode 100644 archetypes/archetype-lambda/src/main/resources/archetype-resources/pom.xml create mode 100644 archetypes/archetype-lambda/src/main/resources/archetype-resources/src/main/java/DependencyFactory.java create mode 100644 archetypes/archetype-lambda/src/main/resources/archetype-resources/src/main/java/__handlerClassName__.java create mode 100644 archetypes/archetype-lambda/src/main/resources/archetype-resources/src/test/java/__handlerClassName__Test.java create mode 100644 archetypes/archetype-lambda/src/main/resources/archetype-resources/template.yaml create mode 100644 archetypes/archetype-lambda/src/main/resources/global.vm create mode 100755 archetypes/archetype-lambda/src/main/resources/map-service-to-client-prefix create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/archetype.properties create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/goal.txt create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/README.md create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/pom.xml create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/src/main/java/software/amazonaws/test/DependencyFactory.java create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/src/main/java/software/amazonaws/test/MyApacheFunction.java create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/src/test/java/software/amazonaws/test/MyApacheFunctionTest.java create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/template.yaml create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/archetype.properties create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/goal.txt create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/reference/README.md create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/reference/pom.xml create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/reference/src/main/java/software/amazonaws/test/DependencyFactory.java create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/reference/src/main/java/software/amazonaws/test/MyDynamoDbStreamsFunction.java create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/reference/src/test/java/software/amazonaws/test/MyDynamoDbStreamsFunctionTest.java create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/reference/template.yaml create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/nettyclient/archetype.properties create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/nettyclient/goal.txt create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/nettyclient/reference/README.md create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/nettyclient/reference/pom.xml create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/nettyclient/reference/src/main/java/software/amazonaws/test/DependencyFactory.java create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/nettyclient/reference/src/main/java/software/amazonaws/test/MyNettyFunction.java create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/nettyclient/reference/src/test/java/software/amazonaws/test/MyNettyFunctionTest.java create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/nettyclient/reference/template.yaml create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/archetype.properties create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/goal.txt create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/reference/README.md create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/reference/pom.xml create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/reference/src/main/java/software/amazonaws/test/App.java create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/reference/src/main/java/software/amazonaws/test/DependencyFactory.java create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/reference/src/test/java/software/amazonaws/test/AppTest.java create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/reference/template.yaml create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/archetype.properties create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/goal.txt create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/reference/README.md create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/reference/pom.xml create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/reference/src/main/java/software/amazonaws/test/DependencyFactory.java create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/reference/src/main/java/software/amazonaws/test/MyWafRegionalFunction.java create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/reference/src/test/java/software/amazonaws/test/MyWafRegionalFunctionTest.java create mode 100644 archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/reference/template.yaml create mode 100644 archetypes/pom.xml diff --git a/README.md b/README.md index b02865ca1258..6193a57ad3bc 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/archetypes/README.md b/archetypes/README.md new file mode 100755 index 000000000000..70980eb12890 --- /dev/null +++ b/archetypes/README.md @@ -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 + diff --git a/archetypes/archetype-lambda/README.md b/archetypes/archetype-lambda/README.md new file mode 100755 index 000000000000..1992b020ddfa --- /dev/null +++ b/archetypes/archetype-lambda/README.md @@ -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 diff --git a/archetypes/archetype-lambda/pom.xml b/archetypes/archetype-lambda/pom.xml new file mode 100644 index 000000000000..57a717fd7e67 --- /dev/null +++ b/archetypes/archetype-lambda/pom.xml @@ -0,0 +1,122 @@ + + + + + + archetypes + software.amazon.awssdk + 2.11.4-SNAPSHOT + + 4.0.0 + archetype-lambda + maven-archetype + AWS Java SDK :: Archetype Lambda + + The AWS SDK for Java - Maven archetype for Java lambda function using AWS Java SDK 2.x + + + + 3.1.2 + 1.6.0 + + + + + + software.amazon.awssdk + aws-sdk-java + ${awsjavasdk.version} + test + + + + + + + src/main/resources + true + + META-INF/maven/archetype-metadata.xml + + + + src/main/resources + false + + META-INF/maven/archetype-metadata.xml + + + + + + org.apache.maven.archetype + archetype-packaging + ${maven.archetype.version} + + + + + + exec-maven-plugin + org.codehaus.mojo + ${exec-maven-plugin.version} + + + map-service-to-client-prefix + generate-resources + + exec + + + ${basedir}/src/main/resources/map-service-to-client-prefix + + + + + + maven-archetype-plugin + ${maven.archetype.version} + + true + true + ${skip.unit.tests} + + + + integration-test + verify + + integration-test + + + + + + + + org.apache.maven.plugins + maven-dependency-plugin + ${maven-dependency-plugin.version} + + true + + + + + \ No newline at end of file diff --git a/archetypes/archetype-lambda/src/main/resources/META-INF/maven/archetype-metadata.xml b/archetypes/archetype-lambda/src/main/resources/META-INF/maven/archetype-metadata.xml new file mode 100644 index 000000000000..cae4983caaf1 --- /dev/null +++ b/archetypes/archetype-lambda/src/main/resources/META-INF/maven/archetype-metadata.xml @@ -0,0 +1,49 @@ + + + + + src/main/java + + **/*.java + + + + src/test/java + + **/*.java + + + + + + .gitignore + template.yaml + README.md + + + + + + App + + + ${project.version} + \d+\.\d+.\d+ + + + + + url-connection-client + (url-connection-client|apache-client|netty-nio-client) + + + ^\w+-(\w+-)+\d+$ + + + + ${netty-open-ssl-version} + + + diff --git a/archetypes/archetype-lambda/src/main/resources/archetype-resources/.gitignore b/archetypes/archetype-lambda/src/main/resources/archetype-resources/.gitignore new file mode 100644 index 000000000000..a500caff54ac --- /dev/null +++ b/archetypes/archetype-lambda/src/main/resources/archetype-resources/.gitignore @@ -0,0 +1,17 @@ +# Eclipse +.classpath +.project +.settings/ + +# Intellij +.idea/ +*.iml +*.iws + +# Mac +.DS_Store + +# Maven +target/ + +**/dependency-reduced-pom.xml diff --git a/archetypes/archetype-lambda/src/main/resources/archetype-resources/README.md b/archetypes/archetype-lambda/src/main/resources/archetype-resources/README.md new file mode 100644 index 000000000000..f8effbaa17ad --- /dev/null +++ b/archetypes/archetype-lambda/src/main/resources/archetype-resources/README.md @@ -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. + + + diff --git a/archetypes/archetype-lambda/src/main/resources/archetype-resources/pom.xml b/archetypes/archetype-lambda/src/main/resources/archetype-resources/pom.xml new file mode 100644 index 000000000000..035954af7d98 --- /dev/null +++ b/archetypes/archetype-lambda/src/main/resources/archetype-resources/pom.xml @@ -0,0 +1,108 @@ +#parse ( "global.vm") + + + 4.0.0 + + ${groupId} + ${artifactId} + ${version} + jar + + UTF-8 + 1.8 + 1.8 + 3.1.1 + 3.6.1 + 1.6.0 + ${javaSdkVersion} + 1.2.0 + 5.4.2 +#if( $httpClient == 'netty-nio-client') + ${nettyOpenSslVersion} +#end + + + + + + software.amazon.awssdk + bom + ${aws.java.sdk.version} + pom + import + + + + + + + software.amazon.awssdk + ${moduleName} + + + software.amazon.awssdk + netty-nio-client + + + software.amazon.awssdk + apache-client + + + + + + software.amazon.awssdk + ${httpClient} + + +#if( $httpClient == 'netty-nio-client') + + + io.netty + netty-tcnative-boringssl-static + ${netty.openssl.version} + +#end + + com.amazonaws + aws-lambda-java-core + ${aws.lambda.java.version} + + + + + org.junit.jupiter + junit-jupiter + ${junit5.version} + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven.compiler.plugin.version} + + + org.apache.maven.plugins + maven-shade-plugin + ${maven.shade.plugin.version} + + false + ${artifactId} + + + + package + + shade + + + + + + + diff --git a/archetypes/archetype-lambda/src/main/resources/archetype-resources/src/main/java/DependencyFactory.java b/archetypes/archetype-lambda/src/main/resources/archetype-resources/src/main/java/DependencyFactory.java new file mode 100644 index 000000000000..f9f4ed2f2e3b --- /dev/null +++ b/archetypes/archetype-lambda/src/main/resources/archetype-resources/src/main/java/DependencyFactory.java @@ -0,0 +1,27 @@ +#parse ( "global.vm") + +package ${package}; + +import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider; +import software.amazon.awssdk.http.${httpClientPackageName}; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.${servicePackage}.${serviceClientClassName}; + +/** + * The module containing all dependencies required by the {@link ${handlerClassName}}. + */ +public class DependencyFactory { + + private DependencyFactory() {} + + /** + * @return an instance of ${serviceClientClassName} + */ + public static ${serviceClientClassName} ${serviceClientVariable}Client() { + return ${serviceClientClassName}.builder() + .credentialsProvider(EnvironmentVariableCredentialsProvider.create()) + .region(Region.${regionEnum}) + .httpClientBuilder(${httpClientClassName}.builder()) + .build(); + } +} diff --git a/archetypes/archetype-lambda/src/main/resources/archetype-resources/src/main/java/__handlerClassName__.java b/archetypes/archetype-lambda/src/main/resources/archetype-resources/src/main/java/__handlerClassName__.java new file mode 100644 index 000000000000..8a66c4aa925d --- /dev/null +++ b/archetypes/archetype-lambda/src/main/resources/archetype-resources/src/main/java/__handlerClassName__.java @@ -0,0 +1,29 @@ +#parse ( "global.vm") +package ${package}; + +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; +import software.amazon.awssdk.services.${servicePackage}.${serviceClientClassName}; + +/** + * Lambda function entry point. You can change to use other pojo type or implement + * a different RequestHandler. + * + * @see Lambda Java Handler for more information + */ +public class ${handlerClassName} implements RequestHandler { + private final ${serviceClientClassName} ${serviceClientVariable}Client; + + public ${handlerClassName}() { + // Initialize the SDK client outside of the handler method so that it can be reused for subsequent invocations. + // It is initialized when the class is loaded. + ${serviceClientVariable}Client = DependencyFactory.${serviceClientVariable}Client(); + // Consider invoking a simple api here to pre-warm up the application, eg: dynamodb#listTables + } + + @Override + public Object handleRequest(final Object input, final Context context) { + // TODO: invoking the api call using ${serviceClientVariable}Client. + return input; + } +} diff --git a/archetypes/archetype-lambda/src/main/resources/archetype-resources/src/test/java/__handlerClassName__Test.java b/archetypes/archetype-lambda/src/main/resources/archetype-resources/src/test/java/__handlerClassName__Test.java new file mode 100644 index 000000000000..beecf7a3c67a --- /dev/null +++ b/archetypes/archetype-lambda/src/main/resources/archetype-resources/src/test/java/__handlerClassName__Test.java @@ -0,0 +1,18 @@ +#set( $symbol_pound = '#' ) +#set( $symbol_dollar = '$' ) +#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); + } +} diff --git a/archetypes/archetype-lambda/src/main/resources/archetype-resources/template.yaml b/archetypes/archetype-lambda/src/main/resources/archetype-resources/template.yaml new file mode 100644 index 000000000000..5d6bb1592002 --- /dev/null +++ b/archetypes/archetype-lambda/src/main/resources/archetype-resources/template.yaml @@ -0,0 +1,19 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Resources: + # See https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html + # for more info to see how to tune the lambda function configs based on your use case. + ${handlerClassName}Function: + Type: AWS::Serverless::Function + Properties: + Runtime: java8 + Handler: ${package}.${handlerClassName}::handleRequest + Timeout: 60 + MemorySize: 512 + CodeUri: ./target/${artifactId}.jar + # Attach policies here to give the function permission to access other AWS resources if needed + # See: https://github.com/awslabs/serverless-application-model/blob/master/docs/policy_templates.rst + # eg: + #Policies: + # - S3ReadPolicy: + # BucketName: test-bucket \ No newline at end of file diff --git a/archetypes/archetype-lambda/src/main/resources/global.vm b/archetypes/archetype-lambda/src/main/resources/global.vm new file mode 100644 index 000000000000..610b556958ff --- /dev/null +++ b/archetypes/archetype-lambda/src/main/resources/global.vm @@ -0,0 +1,36 @@ +## global variables used by the project +#parse ("serviceMapping.vm") +#set( $symbol_pound = '#' ) +#set( $symbol_dollar = '$' ) +#set( $symbol_escape = '\' ) +## customize wafregional and dynamodbstreams because they reside in waf and dynamodb modules respectively. +#if ($service == 'dynamodbstreams') + #set ($moduleName = 'dynamodb') + #set ($serviceClientPrefix = 'DynamoDbStreams') + #set ($servicePackage = 'dynamodb.streams') +#elseif ($service == 'wafregional') + #set ($moduleName = 'waf') + #set ($serviceClientPrefix = 'WafRegional') + #set ($servicePackage = 'waf.regional') +#else +## map the serviceId to service package and service client class name + #set ( $servicePackage = $service) + #set ($moduleName = $service) + #set ( $serviceClientPrefix = $serviceMapping[$service]) +#end +#set ( $serviceClientVariable = $serviceClientPrefix.substring(0,1).toLowerCase() + $serviceClientPrefix.substring(1)) +#set( $regionEnum = $region.replace("-", "_").toUpperCase() ) +## map the client module name to the client class name and pacakge name +#if( $httpClient == 'url-connection-client') + #set ($httpClientClassName = 'UrlConnectionHttpClient') + #set ($httpClientPackageName = 'urlconnection.' + $httpClientClassName) + #set ($serviceClientClassName = $serviceClientPrefix + 'Client') +#elseif ( $httpClient == 'apache-client') + #set ($httpClientClassName = 'ApacheHttpClient') + #set ($httpClientPackageName = 'apache.' + $httpClientClassName) + #set ($serviceClientClassName = $serviceClientPrefix + 'Client') +#elseif ( $httpClient == 'netty-nio-client') + #set ($httpClientClassName = 'NettyNioAsyncHttpClient') + #set ($httpClientPackageName = 'nio.netty.' + $httpClientClassName) + #set ($serviceClientClassName = $serviceClientPrefix + 'AsyncClient') +#end diff --git a/archetypes/archetype-lambda/src/main/resources/map-service-to-client-prefix b/archetypes/archetype-lambda/src/main/resources/map-service-to-client-prefix new file mode 100755 index 000000000000..8574844e7283 --- /dev/null +++ b/archetypes/archetype-lambda/src/main/resources/map-service-to-client-prefix @@ -0,0 +1,50 @@ +#!/usr/bin/env python + +import json +import os +import string + +MAPPING_FILE_NAME = 'serviceMapping.vm' +RESOURCES_ROOT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__))) +ARCHETYPE_LAMBDA_ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(os.path.join(RESOURCES_ROOT_DIR, "../")))) +SERVICE_DIR = os.path.join( + os.path.dirname(os.path.dirname(os.path.abspath(os.path.join(__file__, "../../../../")))), + 'services' +) + +def load_all_service_modules(): + service_mapping = {} + for f in [f for f in os.listdir(SERVICE_DIR) if os.path.isdir(os.path.join(SERVICE_DIR, f)) & os.path.exists(os.path.join(SERVICE_DIR, f, 'target'))]: + for s in [s for s in os.listdir(os.path.join(SERVICE_DIR, f, 'target', 'generated-sources/sdk/software/amazon/awssdk/services', f)) if s.endswith('AsyncClient.java') & s.startswith('Default')]: + service_mapping[f] = find_client_prefix(s) + return service_mapping + +def find_client_prefix(d): + index = d.find('AsyncClient.java') + return d[7:index] + +def write_to_vm_file(service_mapping): + target = os.path.join(ARCHETYPE_LAMBDA_ROOT_DIR, 'target') + + if not os.path.exists(target): + os.mkdir(target) + + target = os.path.join(ARCHETYPE_LAMBDA_ROOT_DIR, 'target', 'classes') + + if not os.path.exists(target): + os.mkdir(target) + + filename = os.path.join(target, MAPPING_FILE_NAME) + + with open(filename, 'w') as f: + f.write('#set ( $serviceMapping =') + f.write(json.dumps(service_mapping)) + f.write(')') + return filename + +def main(): + service_mapping = load_all_service_modules() + write_to_vm_file(service_mapping) + +if __name__ == '__main__': + main() diff --git a/archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/archetype.properties b/archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/archetype.properties new file mode 100644 index 000000000000..682c1bdf1f84 --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/archetype.properties @@ -0,0 +1,10 @@ +groupId=software.amazonaws.test +artifactId=test-apache-artifact +version=1.0-SNAPSHOT +package=software.amazonaws.test +service=dynamodb +httpClient=apache-client +handlerClassName=MyApacheFunction +region=ap-southeast-1 +javaSdkVersion=2.11.0 +nettyOpenSslVersion=2.0.29.Final \ No newline at end of file diff --git a/archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/goal.txt b/archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/goal.txt new file mode 100644 index 000000000000..4a1a71d3364c --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/goal.txt @@ -0,0 +1 @@ +verify \ No newline at end of file diff --git a/archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/README.md b/archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/README.md new file mode 100644 index 000000000000..30bf8a316d7e --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/README.md @@ -0,0 +1,45 @@ +# MyApacheFunction + +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 dynamoDbClient. + +## 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. + + + diff --git a/archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/pom.xml b/archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/pom.xml new file mode 100644 index 000000000000..2f1c10b9e1e4 --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/pom.xml @@ -0,0 +1,95 @@ + + + 4.0.0 + + software.amazonaws.test + test-apache-artifact + 1.0-SNAPSHOT + jar + + UTF-8 + 1.8 + 1.8 + 3.1.1 + 3.6.1 + 1.6.0 + 2.11.0 + 1.2.0 + 5.4.2 + + + + + + software.amazon.awssdk + bom + ${aws.java.sdk.version} + pom + import + + + + + + + software.amazon.awssdk + dynamodb + + + software.amazon.awssdk + netty-nio-client + + + software.amazon.awssdk + apache-client + + + + + + software.amazon.awssdk + apache-client + + + + com.amazonaws + aws-lambda-java-core + ${aws.lambda.java.version} + + + + + org.junit.jupiter + junit-jupiter + ${junit5.version} + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven.compiler.plugin.version} + + + org.apache.maven.plugins + maven-shade-plugin + ${maven.shade.plugin.version} + + false + test-apache-artifact + + + + package + + shade + + + + + + + diff --git a/archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/src/main/java/software/amazonaws/test/DependencyFactory.java b/archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/src/main/java/software/amazonaws/test/DependencyFactory.java new file mode 100644 index 000000000000..f6ecbd48a31f --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/src/main/java/software/amazonaws/test/DependencyFactory.java @@ -0,0 +1,26 @@ + +package software.amazonaws.test; + +import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider; +import software.amazon.awssdk.http.apache.ApacheHttpClient; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; + +/** + * The module containing all dependencies required by the {@link MyApacheFunction}. + */ +public class DependencyFactory { + + private DependencyFactory() {} + + /** + * @return an instance of DynamoDbClient + */ + public static DynamoDbClient dynamoDbClient() { + return DynamoDbClient.builder() + .credentialsProvider(EnvironmentVariableCredentialsProvider.create()) + .region(Region.AP_SOUTHEAST_1) + .httpClientBuilder(ApacheHttpClient.builder()) + .build(); + } +} diff --git a/archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/src/main/java/software/amazonaws/test/MyApacheFunction.java b/archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/src/main/java/software/amazonaws/test/MyApacheFunction.java new file mode 100644 index 000000000000..8f9f860811c6 --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/src/main/java/software/amazonaws/test/MyApacheFunction.java @@ -0,0 +1,28 @@ +package software.amazonaws.test; + +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; + +/** + * Lambda function entry point. You can change to use other pojo type or implement + * a different RequestHandler. + * + * @see Lambda Java Handler for more information + */ +public class MyApacheFunction implements RequestHandler { + private final DynamoDbClient dynamoDbClient; + + public MyApacheFunction() { + // Initialize the SDK client outside of the handler method so that it can be reused for subsequent invocations. + // It is initialized when the class is loaded. + dynamoDbClient = DependencyFactory.dynamoDbClient(); + // Consider invoking a simple api here to pre-warm up the application, eg: dynamodb#listTables + } + + @Override + public Object handleRequest(final Object input, final Context context) { + // TODO: invoking the api call using dynamoDbClient. + return input; + } +} diff --git a/archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/src/test/java/software/amazonaws/test/MyApacheFunctionTest.java b/archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/src/test/java/software/amazonaws/test/MyApacheFunctionTest.java new file mode 100644 index 000000000000..b55287348392 --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/src/test/java/software/amazonaws/test/MyApacheFunctionTest.java @@ -0,0 +1,15 @@ +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); + } +} diff --git a/archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/template.yaml b/archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/template.yaml new file mode 100644 index 000000000000..513c0aed185b --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/apachehttpclient/reference/template.yaml @@ -0,0 +1,19 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Resources: + # See https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html + # for more info to see how to tune the lambda function configs based on your use case. + MyApacheFunctionFunction: + Type: AWS::Serverless::Function + Properties: + Runtime: java8 + Handler: software.amazonaws.test.MyApacheFunction::handleRequest + Timeout: 60 + MemorySize: 512 + CodeUri: ./target/test-apache-artifact.jar + # Attach policies here to give the function permission to access other AWS resources if needed + # See: https://github.com/awslabs/serverless-application-model/blob/master/docs/policy_templates.rst + # eg: + #Policies: + # - S3ReadPolicy: + # BucketName: test-bucket \ No newline at end of file diff --git a/archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/archetype.properties b/archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/archetype.properties new file mode 100644 index 000000000000..148e110f2ee0 --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/archetype.properties @@ -0,0 +1,25 @@ +# +# Copyright 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. +# + +groupId=software.amazonaws.test +artifactId=test-dynamodbstreams-artifact +version=1.0-SNAPSHOT +package=software.amazonaws.test +service=dynamodbstreams +httpClient=apache-client +handlerClassName=MyDynamoDbStreamsFunction +region=ap-southeast-1 +javaSdkVersion=2.11.0 +nettyOpenSslVersion=2.0.29.Final \ No newline at end of file diff --git a/archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/goal.txt b/archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/goal.txt new file mode 100644 index 000000000000..4a1a71d3364c --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/goal.txt @@ -0,0 +1 @@ +verify \ No newline at end of file diff --git a/archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/reference/README.md b/archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/reference/README.md new file mode 100644 index 000000000000..2fc00d2e4ac0 --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/reference/README.md @@ -0,0 +1,45 @@ +# MyDynamoDbStreamsFunction + +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 dynamoDbStreamsClient. + +## 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. + + + diff --git a/archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/reference/pom.xml b/archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/reference/pom.xml new file mode 100644 index 000000000000..fa7e7cce3210 --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/reference/pom.xml @@ -0,0 +1,95 @@ + + + 4.0.0 + + software.amazonaws.test + test-dynamodbstreams-artifact + 1.0-SNAPSHOT + jar + + UTF-8 + 1.8 + 1.8 + 3.1.1 + 3.6.1 + 1.6.0 + 2.11.0 + 1.2.0 + 5.4.2 + + + + + + software.amazon.awssdk + bom + ${aws.java.sdk.version} + pom + import + + + + + + + software.amazon.awssdk + dynamodb + + + software.amazon.awssdk + netty-nio-client + + + software.amazon.awssdk + apache-client + + + + + + software.amazon.awssdk + apache-client + + + + com.amazonaws + aws-lambda-java-core + ${aws.lambda.java.version} + + + + + org.junit.jupiter + junit-jupiter + ${junit5.version} + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven.compiler.plugin.version} + + + org.apache.maven.plugins + maven-shade-plugin + ${maven.shade.plugin.version} + + false + test-dynamodbstreams-artifact + + + + package + + shade + + + + + + + diff --git a/archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/reference/src/main/java/software/amazonaws/test/DependencyFactory.java b/archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/reference/src/main/java/software/amazonaws/test/DependencyFactory.java new file mode 100644 index 000000000000..b8bdd17acc85 --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/reference/src/main/java/software/amazonaws/test/DependencyFactory.java @@ -0,0 +1,26 @@ + +package software.amazonaws.test; + +import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider; +import software.amazon.awssdk.http.apache.ApacheHttpClient; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.dynamodb.streams.DynamoDbStreamsClient; + +/** + * The module containing all dependencies required by the {@link MyDynamoDbStreamsFunction}. + */ +public class DependencyFactory { + + private DependencyFactory() {} + + /** + * @return an instance of DynamoDbStreamsClient + */ + public static DynamoDbStreamsClient dynamoDbStreamsClient() { + return DynamoDbStreamsClient.builder() + .credentialsProvider(EnvironmentVariableCredentialsProvider.create()) + .region(Region.AP_SOUTHEAST_1) + .httpClientBuilder(ApacheHttpClient.builder()) + .build(); + } +} diff --git a/archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/reference/src/main/java/software/amazonaws/test/MyDynamoDbStreamsFunction.java b/archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/reference/src/main/java/software/amazonaws/test/MyDynamoDbStreamsFunction.java new file mode 100644 index 000000000000..2722fbfdba68 --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/reference/src/main/java/software/amazonaws/test/MyDynamoDbStreamsFunction.java @@ -0,0 +1,28 @@ +package software.amazonaws.test; + +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; +import software.amazon.awssdk.services.dynamodb.streams.DynamoDbStreamsClient; + +/** + * Lambda function entry point. You can change to use other pojo type or implement + * a different RequestHandler. + * + * @see Lambda Java Handler for more information + */ +public class MyDynamoDbStreamsFunction implements RequestHandler { + private final DynamoDbStreamsClient dynamoDbStreamsClient; + + public MyDynamoDbStreamsFunction() { + // Initialize the SDK client outside of the handler method so that it can be reused for subsequent invocations. + // It is initialized when the class is loaded. + dynamoDbStreamsClient = DependencyFactory.dynamoDbStreamsClient(); + // Consider invoking a simple api here to pre-warm up the application, eg: dynamodb#listTables + } + + @Override + public Object handleRequest(final Object input, final Context context) { + // TODO: invoking the api call using dynamoDbStreamsClient. + return input; + } +} diff --git a/archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/reference/src/test/java/software/amazonaws/test/MyDynamoDbStreamsFunctionTest.java b/archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/reference/src/test/java/software/amazonaws/test/MyDynamoDbStreamsFunctionTest.java new file mode 100644 index 000000000000..7553684cf11a --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/reference/src/test/java/software/amazonaws/test/MyDynamoDbStreamsFunctionTest.java @@ -0,0 +1,15 @@ +package software.amazonaws.test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class MyDynamoDbStreamsFunctionTest { + + @Test + public void handleRequest_shouldReturnConstantValue() { + MyDynamoDbStreamsFunction function = new MyDynamoDbStreamsFunction(); + Object result = function.handleRequest("echo", null); + assertEquals("echo", result); + } +} diff --git a/archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/reference/template.yaml b/archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/reference/template.yaml new file mode 100644 index 000000000000..797f24dc0751 --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/dynamodbstreamsclient/reference/template.yaml @@ -0,0 +1,19 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Resources: + # See https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html + # for more info to see how to tune the lambda function configs based on your use case. + MyDynamoDbStreamsFunctionFunction: + Type: AWS::Serverless::Function + Properties: + Runtime: java8 + Handler: software.amazonaws.test.MyDynamoDbStreamsFunction::handleRequest + Timeout: 60 + MemorySize: 512 + CodeUri: ./target/test-dynamodbstreams-artifact.jar + # Attach policies here to give the function permission to access other AWS resources if needed + # See: https://github.com/awslabs/serverless-application-model/blob/master/docs/policy_templates.rst + # eg: + #Policies: + # - S3ReadPolicy: + # BucketName: test-bucket \ No newline at end of file diff --git a/archetypes/archetype-lambda/src/test/resources/projects/nettyclient/archetype.properties b/archetypes/archetype-lambda/src/test/resources/projects/nettyclient/archetype.properties new file mode 100644 index 000000000000..b5ef44cbd770 --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/nettyclient/archetype.properties @@ -0,0 +1,11 @@ +groupId=software.amazonaws.test +artifactId=test-netty-artifact +version=1.0-SNAPSHOT +package=software.amazonaws.test +service=kinesis +httpClient=netty-nio-client +handlerClassName=MyNettyFunction +region=us-east-1 +javaSdkVersion=2.11.0 +nettyOpenSslVersion=2.0.29.Final + diff --git a/archetypes/archetype-lambda/src/test/resources/projects/nettyclient/goal.txt b/archetypes/archetype-lambda/src/test/resources/projects/nettyclient/goal.txt new file mode 100644 index 000000000000..4a1a71d3364c --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/nettyclient/goal.txt @@ -0,0 +1 @@ +verify \ No newline at end of file diff --git a/archetypes/archetype-lambda/src/test/resources/projects/nettyclient/reference/README.md b/archetypes/archetype-lambda/src/test/resources/projects/nettyclient/reference/README.md new file mode 100644 index 000000000000..e265f49bbdc4 --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/nettyclient/reference/README.md @@ -0,0 +1,45 @@ +# MyNettyFunction + +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 kinesisClient. + +## 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. + + + diff --git a/archetypes/archetype-lambda/src/test/resources/projects/nettyclient/reference/pom.xml b/archetypes/archetype-lambda/src/test/resources/projects/nettyclient/reference/pom.xml new file mode 100644 index 000000000000..003cb97c99c7 --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/nettyclient/reference/pom.xml @@ -0,0 +1,103 @@ + + + 4.0.0 + + software.amazonaws.test + test-netty-artifact + 1.0-SNAPSHOT + jar + + UTF-8 + 1.8 + 1.8 + 3.1.1 + 3.6.1 + 1.6.0 + 2.11.0 + 1.2.0 + 5.4.2 + 2.0.29.Final + + + + + + software.amazon.awssdk + bom + ${aws.java.sdk.version} + pom + import + + + + + + + software.amazon.awssdk + kinesis + + + software.amazon.awssdk + netty-nio-client + + + software.amazon.awssdk + apache-client + + + + + + software.amazon.awssdk + netty-nio-client + + + + + io.netty + netty-tcnative-boringssl-static + ${netty.openssl.version} + + + com.amazonaws + aws-lambda-java-core + ${aws.lambda.java.version} + + + + + org.junit.jupiter + junit-jupiter + ${junit5.version} + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven.compiler.plugin.version} + + + org.apache.maven.plugins + maven-shade-plugin + ${maven.shade.plugin.version} + + false + test-netty-artifact + + + + package + + shade + + + + + + + diff --git a/archetypes/archetype-lambda/src/test/resources/projects/nettyclient/reference/src/main/java/software/amazonaws/test/DependencyFactory.java b/archetypes/archetype-lambda/src/test/resources/projects/nettyclient/reference/src/main/java/software/amazonaws/test/DependencyFactory.java new file mode 100644 index 000000000000..ab3f3792bb7e --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/nettyclient/reference/src/main/java/software/amazonaws/test/DependencyFactory.java @@ -0,0 +1,26 @@ + +package software.amazonaws.test; + +import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider; +import software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.kinesis.KinesisAsyncClient; + +/** + * The module containing all dependencies required by the {@link MyNettyFunction}. + */ +public class DependencyFactory { + + private DependencyFactory() {} + + /** + * @return an instance of KinesisAsyncClient + */ + public static KinesisAsyncClient kinesisClient() { + return KinesisAsyncClient.builder() + .credentialsProvider(EnvironmentVariableCredentialsProvider.create()) + .region(Region.US_EAST_1) + .httpClientBuilder(NettyNioAsyncHttpClient.builder()) + .build(); + } +} diff --git a/archetypes/archetype-lambda/src/test/resources/projects/nettyclient/reference/src/main/java/software/amazonaws/test/MyNettyFunction.java b/archetypes/archetype-lambda/src/test/resources/projects/nettyclient/reference/src/main/java/software/amazonaws/test/MyNettyFunction.java new file mode 100644 index 000000000000..0016c30c56e0 --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/nettyclient/reference/src/main/java/software/amazonaws/test/MyNettyFunction.java @@ -0,0 +1,28 @@ +package software.amazonaws.test; + +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; +import software.amazon.awssdk.services.kinesis.KinesisAsyncClient; + +/** + * Lambda function entry point. You can change to use other pojo type or implement + * a different RequestHandler. + * + * @see Lambda Java Handler for more information + */ +public class MyNettyFunction implements RequestHandler { + private final KinesisAsyncClient kinesisClient; + + public MyNettyFunction() { + // Initialize the SDK client outside of the handler method so that it can be reused for subsequent invocations. + // It is initialized when the class is loaded. + kinesisClient = DependencyFactory.kinesisClient(); + // Consider invoking a simple api here to pre-warm up the application, eg: dynamodb#listTables + } + + @Override + public Object handleRequest(final Object input, final Context context) { + // TODO: invoking the api call using kinesisClient. + return input; + } +} diff --git a/archetypes/archetype-lambda/src/test/resources/projects/nettyclient/reference/src/test/java/software/amazonaws/test/MyNettyFunctionTest.java b/archetypes/archetype-lambda/src/test/resources/projects/nettyclient/reference/src/test/java/software/amazonaws/test/MyNettyFunctionTest.java new file mode 100644 index 000000000000..46e9272647aa --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/nettyclient/reference/src/test/java/software/amazonaws/test/MyNettyFunctionTest.java @@ -0,0 +1,15 @@ +package software.amazonaws.test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class MyNettyFunctionTest { + + @Test + public void handleRequest_shouldReturnConstantValue() { + MyNettyFunction function = new MyNettyFunction(); + Object result = function.handleRequest("echo", null); + assertEquals("echo", result); + } +} diff --git a/archetypes/archetype-lambda/src/test/resources/projects/nettyclient/reference/template.yaml b/archetypes/archetype-lambda/src/test/resources/projects/nettyclient/reference/template.yaml new file mode 100644 index 000000000000..e674e2599da3 --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/nettyclient/reference/template.yaml @@ -0,0 +1,19 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Resources: + # See https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html + # for more info to see how to tune the lambda function configs based on your use case. + MyNettyFunctionFunction: + Type: AWS::Serverless::Function + Properties: + Runtime: java8 + Handler: software.amazonaws.test.MyNettyFunction::handleRequest + Timeout: 60 + MemorySize: 512 + CodeUri: ./target/test-netty-artifact.jar + # Attach policies here to give the function permission to access other AWS resources if needed + # See: https://github.com/awslabs/serverless-application-model/blob/master/docs/policy_templates.rst + # eg: + #Policies: + # - S3ReadPolicy: + # BucketName: test-bucket \ No newline at end of file diff --git a/archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/archetype.properties b/archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/archetype.properties new file mode 100644 index 000000000000..4b987f49a274 --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/archetype.properties @@ -0,0 +1,10 @@ +groupId=software.amazonaws.test +artifactId=test-url-connection-client-artifact +version=1.0-SNAPSHOT +package=software.amazonaws.test +service=s3 +httpClient=url-connection-client +handlerClassName=App +region=us-west-2 +javaSdkVersion=2.11.0 +nettyOpenSslVersion=2.0.29.Final \ No newline at end of file diff --git a/archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/goal.txt b/archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/goal.txt new file mode 100644 index 000000000000..4a1a71d3364c --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/goal.txt @@ -0,0 +1 @@ +verify \ No newline at end of file diff --git a/archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/reference/README.md b/archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/reference/README.md new file mode 100644 index 000000000000..6b17a7840213 --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/reference/README.md @@ -0,0 +1,45 @@ +# App + +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 s3Client. + +## 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. + + + diff --git a/archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/reference/pom.xml b/archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/reference/pom.xml new file mode 100644 index 000000000000..addcc8788bcb --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/reference/pom.xml @@ -0,0 +1,95 @@ + + + 4.0.0 + + software.amazonaws.test + test-url-connection-client-artifact + 1.0-SNAPSHOT + jar + + UTF-8 + 1.8 + 1.8 + 3.1.1 + 3.6.1 + 1.6.0 + 2.11.0 + 1.2.0 + 5.4.2 + + + + + + software.amazon.awssdk + bom + ${aws.java.sdk.version} + pom + import + + + + + + + software.amazon.awssdk + s3 + + + software.amazon.awssdk + netty-nio-client + + + software.amazon.awssdk + apache-client + + + + + + software.amazon.awssdk + url-connection-client + + + + com.amazonaws + aws-lambda-java-core + ${aws.lambda.java.version} + + + + + org.junit.jupiter + junit-jupiter + ${junit5.version} + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven.compiler.plugin.version} + + + org.apache.maven.plugins + maven-shade-plugin + ${maven.shade.plugin.version} + + false + test-url-connection-client-artifact + + + + package + + shade + + + + + + + diff --git a/archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/reference/src/main/java/software/amazonaws/test/App.java b/archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/reference/src/main/java/software/amazonaws/test/App.java new file mode 100644 index 000000000000..3d3588fee28a --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/reference/src/main/java/software/amazonaws/test/App.java @@ -0,0 +1,28 @@ +package software.amazonaws.test; + +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; +import software.amazon.awssdk.services.s3.S3Client; + +/** + * Lambda function entry point. You can change to use other pojo type or implement + * a different RequestHandler. + * + * @see Lambda Java Handler for more information + */ +public class App implements RequestHandler { + private final S3Client s3Client; + + public App() { + // Initialize the SDK client outside of the handler method so that it can be reused for subsequent invocations. + // It is initialized when the class is loaded. + s3Client = DependencyFactory.s3Client(); + // Consider invoking a simple api here to pre-warm up the application, eg: dynamodb#listTables + } + + @Override + public Object handleRequest(final Object input, final Context context) { + // TODO: invoking the api call using s3Client. + return input; + } +} diff --git a/archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/reference/src/main/java/software/amazonaws/test/DependencyFactory.java b/archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/reference/src/main/java/software/amazonaws/test/DependencyFactory.java new file mode 100644 index 000000000000..926269195a94 --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/reference/src/main/java/software/amazonaws/test/DependencyFactory.java @@ -0,0 +1,26 @@ + +package software.amazonaws.test; + +import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider; +import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.s3.S3Client; + +/** + * The module containing all dependencies required by the {@link App}. + */ +public class DependencyFactory { + + private DependencyFactory() {} + + /** + * @return an instance of S3Client + */ + public static S3Client s3Client() { + return S3Client.builder() + .credentialsProvider(EnvironmentVariableCredentialsProvider.create()) + .region(Region.US_WEST_2) + .httpClientBuilder(UrlConnectionHttpClient.builder()) + .build(); + } +} diff --git a/archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/reference/src/test/java/software/amazonaws/test/AppTest.java b/archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/reference/src/test/java/software/amazonaws/test/AppTest.java new file mode 100644 index 000000000000..8400b37496b7 --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/reference/src/test/java/software/amazonaws/test/AppTest.java @@ -0,0 +1,15 @@ +package software.amazonaws.test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class AppTest { + + @Test + public void handleRequest_shouldReturnConstantValue() { + App function = new App(); + Object result = function.handleRequest("echo", null); + assertEquals("echo", result); + } +} diff --git a/archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/reference/template.yaml b/archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/reference/template.yaml new file mode 100644 index 000000000000..ca0bb619fd4e --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/urlhttpclient/reference/template.yaml @@ -0,0 +1,19 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Resources: + # See https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html + # for more info to see how to tune the lambda function configs based on your use case. + AppFunction: + Type: AWS::Serverless::Function + Properties: + Runtime: java8 + Handler: software.amazonaws.test.App::handleRequest + Timeout: 60 + MemorySize: 512 + CodeUri: ./target/test-url-connection-client-artifact.jar + # Attach policies here to give the function permission to access other AWS resources if needed + # See: https://github.com/awslabs/serverless-application-model/blob/master/docs/policy_templates.rst + # eg: + #Policies: + # - S3ReadPolicy: + # BucketName: test-bucket \ No newline at end of file diff --git a/archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/archetype.properties b/archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/archetype.properties new file mode 100644 index 000000000000..de95a87a7f91 --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/archetype.properties @@ -0,0 +1,25 @@ +# +# Copyright 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. +# + +groupId=software.amazonaws.test +artifactId=test-wafregional-artifact +version=1.0-SNAPSHOT +package=software.amazonaws.test +service=wafregional +httpClient=apache-client +handlerClassName=MyWafRegionalFunction +region=ap-southeast-1 +javaSdkVersion=2.11.0 +nettyOpenSslVersion=2.0.29.Final \ No newline at end of file diff --git a/archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/goal.txt b/archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/goal.txt new file mode 100644 index 000000000000..4a1a71d3364c --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/goal.txt @@ -0,0 +1 @@ +verify \ No newline at end of file diff --git a/archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/reference/README.md b/archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/reference/README.md new file mode 100644 index 000000000000..cdff80c819bd --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/reference/README.md @@ -0,0 +1,45 @@ +# MyWafRegionalFunction + +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 wafRegionalClient. + +## 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. + + + diff --git a/archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/reference/pom.xml b/archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/reference/pom.xml new file mode 100644 index 000000000000..68fe5c93a944 --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/reference/pom.xml @@ -0,0 +1,95 @@ + + + 4.0.0 + + software.amazonaws.test + test-wafregional-artifact + 1.0-SNAPSHOT + jar + + UTF-8 + 1.8 + 1.8 + 3.1.1 + 3.6.1 + 1.6.0 + 2.11.0 + 1.2.0 + 5.4.2 + + + + + + software.amazon.awssdk + bom + ${aws.java.sdk.version} + pom + import + + + + + + + software.amazon.awssdk + waf + + + software.amazon.awssdk + netty-nio-client + + + software.amazon.awssdk + apache-client + + + + + + software.amazon.awssdk + apache-client + + + + com.amazonaws + aws-lambda-java-core + ${aws.lambda.java.version} + + + + + org.junit.jupiter + junit-jupiter + ${junit5.version} + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven.compiler.plugin.version} + + + org.apache.maven.plugins + maven-shade-plugin + ${maven.shade.plugin.version} + + false + test-wafregional-artifact + + + + package + + shade + + + + + + + diff --git a/archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/reference/src/main/java/software/amazonaws/test/DependencyFactory.java b/archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/reference/src/main/java/software/amazonaws/test/DependencyFactory.java new file mode 100644 index 000000000000..31bacbfa33ef --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/reference/src/main/java/software/amazonaws/test/DependencyFactory.java @@ -0,0 +1,26 @@ + +package software.amazonaws.test; + +import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider; +import software.amazon.awssdk.http.apache.ApacheHttpClient; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.waf.regional.WafRegionalClient; + +/** + * The module containing all dependencies required by the {@link MyWafRegionalFunction}. + */ +public class DependencyFactory { + + private DependencyFactory() {} + + /** + * @return an instance of WafRegionalClient + */ + public static WafRegionalClient wafRegionalClient() { + return WafRegionalClient.builder() + .credentialsProvider(EnvironmentVariableCredentialsProvider.create()) + .region(Region.AP_SOUTHEAST_1) + .httpClientBuilder(ApacheHttpClient.builder()) + .build(); + } +} diff --git a/archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/reference/src/main/java/software/amazonaws/test/MyWafRegionalFunction.java b/archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/reference/src/main/java/software/amazonaws/test/MyWafRegionalFunction.java new file mode 100644 index 000000000000..9f02b456977a --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/reference/src/main/java/software/amazonaws/test/MyWafRegionalFunction.java @@ -0,0 +1,28 @@ +package software.amazonaws.test; + +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; +import software.amazon.awssdk.services.waf.regional.WafRegionalClient; + +/** + * Lambda function entry point. You can change to use other pojo type or implement + * a different RequestHandler. + * + * @see Lambda Java Handler for more information + */ +public class MyWafRegionalFunction implements RequestHandler { + private final WafRegionalClient wafRegionalClient; + + public MyWafRegionalFunction() { + // Initialize the SDK client outside of the handler method so that it can be reused for subsequent invocations. + // It is initialized when the class is loaded. + wafRegionalClient = DependencyFactory.wafRegionalClient(); + // Consider invoking a simple api here to pre-warm up the application, eg: dynamodb#listTables + } + + @Override + public Object handleRequest(final Object input, final Context context) { + // TODO: invoking the api call using wafRegionalClient. + return input; + } +} diff --git a/archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/reference/src/test/java/software/amazonaws/test/MyWafRegionalFunctionTest.java b/archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/reference/src/test/java/software/amazonaws/test/MyWafRegionalFunctionTest.java new file mode 100644 index 000000000000..adc0157faf76 --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/reference/src/test/java/software/amazonaws/test/MyWafRegionalFunctionTest.java @@ -0,0 +1,15 @@ +package software.amazonaws.test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class MyWafRegionalFunctionTest { + + @Test + public void handleRequest_shouldReturnConstantValue() { + MyWafRegionalFunction function = new MyWafRegionalFunction(); + Object result = function.handleRequest("echo", null); + assertEquals("echo", result); + } +} diff --git a/archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/reference/template.yaml b/archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/reference/template.yaml new file mode 100644 index 000000000000..70ee17fae8a3 --- /dev/null +++ b/archetypes/archetype-lambda/src/test/resources/projects/wafregionalclient/reference/template.yaml @@ -0,0 +1,19 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Resources: + # See https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html + # for more info to see how to tune the lambda function configs based on your use case. + MyWafRegionalFunctionFunction: + Type: AWS::Serverless::Function + Properties: + Runtime: java8 + Handler: software.amazonaws.test.MyWafRegionalFunction::handleRequest + Timeout: 60 + MemorySize: 512 + CodeUri: ./target/test-wafregional-artifact.jar + # Attach policies here to give the function permission to access other AWS resources if needed + # See: https://github.com/awslabs/serverless-application-model/blob/master/docs/policy_templates.rst + # eg: + #Policies: + # - S3ReadPolicy: + # BucketName: test-bucket \ No newline at end of file diff --git a/archetypes/pom.xml b/archetypes/pom.xml new file mode 100644 index 000000000000..d33afe56ea97 --- /dev/null +++ b/archetypes/pom.xml @@ -0,0 +1,35 @@ + + + + + + aws-sdk-java-pom + software.amazon.awssdk + 2.11.4-SNAPSHOT + + 4.0.0 + archetypes + AWS Java SDK :: Archetypes + + archetype-lambda + + pom + + Maven Archetypes for applications using Java SDK 2.x + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 94041067bca3..f996545c8e08 100644 --- a/pom.xml +++ b/pom.xml @@ -66,6 +66,7 @@ utils codegen-lite codegen-lite-maven-plugin + archetypes test/http-client-tests test/protocol-tests test/protocol-tests-core