serveless implementation using aws compute engine and serverless framework
4
pom.xml
@ -48,6 +48,9 @@
|
|||||||
<mongo-java-driver.version>3.3.0</mongo-java-driver.version>
|
<mongo-java-driver.version>3.3.0</mongo-java-driver.version>
|
||||||
<slf4j.version>1.7.21</slf4j.version>
|
<slf4j.version>1.7.21</slf4j.version>
|
||||||
<logback.version>1.1.7</logback.version>
|
<logback.version>1.1.7</logback.version>
|
||||||
|
<aws-lambda-core.version>1.1.0</aws-lambda-core.version>
|
||||||
|
<aws-lambda-log4j.version>1.0.0</aws-lambda-log4j.version>
|
||||||
|
<jackson.version>2.8.5</jackson.version>
|
||||||
</properties>
|
</properties>
|
||||||
<modules>
|
<modules>
|
||||||
<module>abstract-factory</module>
|
<module>abstract-factory</module>
|
||||||
@ -154,6 +157,7 @@
|
|||||||
<module>eip-splitter</module>
|
<module>eip-splitter</module>
|
||||||
<module>eip-aggregator</module>
|
<module>eip-aggregator</module>
|
||||||
<module>retry</module>
|
<module>retry</module>
|
||||||
|
<module>serverless</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<repositories>
|
<repositories>
|
||||||
|
122
serverless/README.md
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
---
|
||||||
|
layout: pattern
|
||||||
|
title: serverless
|
||||||
|
folder: serverless
|
||||||
|
permalink: /patterns/serverless/
|
||||||
|
categories: Architectural
|
||||||
|
tags:
|
||||||
|
- Java
|
||||||
|
- Difficulty-Intermittent
|
||||||
|
---
|
||||||
|
|
||||||
|
## Serverless
|
||||||
|
|
||||||
|
Serverless eliminates the need to plan for infrastructure and let's you focus on your
|
||||||
|
application.
|
||||||
|
|
||||||
|
## Intent
|
||||||
|
|
||||||
|
Whether to reduce your infrastructure costs, shrink the time you spend on ops tasks,
|
||||||
|
simplify your deployment processes, reach infinite scalability, serverless cuts time
|
||||||
|
to market in half.
|
||||||
|
|
||||||
|
## Explanation
|
||||||
|
|
||||||
|
Serverless computing is a cloud computing execution model in which the cloud provider
|
||||||
|
dynamically manages the allocation of machine resources. Pricing is based on the
|
||||||
|
actual amount of resources consumed by an application, rather than on pre-purchased
|
||||||
|
units of capacity.
|
||||||
|
|
||||||
|
## Serverless framework
|
||||||
|
|
||||||
|
[Serverless](https://serverless.com/) is a toolkit for deploying and operating serverless architectures.
|
||||||
|
|
||||||
|
## (Function as a Service or "FaaS")
|
||||||
|
|
||||||
|
The term ‘Serverless’ is confusing since with such applications there are both server
|
||||||
|
hardware and server processes running somewhere, but the difference to normal
|
||||||
|
approaches is that the organization building and supporting a ‘Serverless’ application
|
||||||
|
is not looking after the hardware or the processes - they are outsourcing this to a vendor.
|
||||||
|
|
||||||
|
Some of the Serverless Cloud Providers are
|
||||||
|
|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|
...
|
||||||
|
|
||||||
|
Anything that triggers an Lambda Function to execute is regarded by the Framework as
|
||||||
|
an Event. Most of the Serverless Cloud Providers support following Events
|
||||||
|
- Http
|
||||||
|
- PubSub Events
|
||||||
|
- scheduled
|
||||||
|
|
||||||
|
AWS supports processing event generated from AWS Services (S3/Cloudwatch/etc) and
|
||||||
|
using aws as a compute engine is our first choice.
|
||||||
|
|
||||||
|
## AWS lambda function implementation
|
||||||
|
|
||||||
|
AWS lambda SDK provides pre-defined interface `com.amazonaws.services.lambda.runtime
|
||||||
|
.RequestHandler` to implement our lambda function.
|
||||||
|
|
||||||
|
```java
|
||||||
|
public class LambdaInfoApiHandler implements RequestHandler<Map<String, Object>, ApiGatewayResponse> {
|
||||||
|
|
||||||
|
private static final Logger LOG = Logger.getLogger(LambdaInfoApiHandler.class);
|
||||||
|
private static final Integer SUCCESS_STATUS_CODE = 200;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ApiGatewayResponse handleRequest(Map<String, Object> input, Context context) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
handleRequest method is where the function code is implemented. Context provides
|
||||||
|
useful information about Lambda execution environment. AWS Lambda function needs a
|
||||||
|
deployment package. This package is either a .zip or .jar file that contains all the
|
||||||
|
dependencies of the function.
|
||||||
|
|
||||||
|
`serverless.yml` contains configuration to manage deployments for your functions.
|
||||||
|
|
||||||
|
## Run example in local
|
||||||
|
|
||||||
|
# Pre-requisites
|
||||||
|
* Node.js v6.5.0 or later.
|
||||||
|
* Serverless CLI v1.9.0 or later. You can run npm install -g serverless to install it.
|
||||||
|
* An AWS account. If you don't already have one, you can sign up for a free trial that includes 1 million free Lambda requests per month.
|
||||||
|
* [Set-up](https://serverless.com/framework/docs/providers/aws/guide/credentials/) your Provider Credentials. Watch the video on setting up credentials
|
||||||
|
|
||||||
|
# build and deploy
|
||||||
|
|
||||||
|
* `cd serverless`
|
||||||
|
* `mvn clean package`
|
||||||
|
* `serverless deploy --stage=dev --verbose`
|
||||||
|
|
||||||
|
Based on the configuration in `serverless.yml` serverless framework creates a
|
||||||
|
cloud formation stack for S3 (ServerlessDeploymentBucket), IAM Role
|
||||||
|
(IamRoleLambdaExecution), cloud watch (log groups), API Gateway (ApiGatewayRestApi)
|
||||||
|
and the Lambda function.
|
||||||
|
|
||||||
|
The command will print out Stack Outputs which looks something like this
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
endpoints:
|
||||||
|
GET - https://xxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/info
|
||||||
|
```
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
CurrentTimeLambdaFunctionQualifiedArn: arn:aws:lambda:us-east-1:xxxxxxxxxxx:function:lambda-info-http-endpoint-dev-currentTime:4
|
||||||
|
ServiceEndpoint: https://xxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev
|
||||||
|
ServerlessDeploymentBucketName: lambda-info-http-endpoin-serverlessdeploymentbuck-2u8uz2i7cap2
|
||||||
|
```
|
||||||
|
access the endpoint to invoke the function.
|
||||||
|
|
||||||
|
## Credits
|
||||||
|
|
||||||
|
* [serverless docs](https://serverless.com/framework/docs/)
|
||||||
|
* [Serverless Architectures](https://martinfowler.com/articles/serverless.html)
|
BIN
serverless/etc/aws-black.png
Normal file
After Width: | Height: | Size: 9.0 KiB |
BIN
serverless/etc/azure-black.png
Normal file
After Width: | Height: | Size: 4.6 KiB |
BIN
serverless/etc/gcf-black.png
Normal file
After Width: | Height: | Size: 7.5 KiB |
BIN
serverless/etc/kubeless-logos-black.png
Normal file
After Width: | Height: | Size: 8.7 KiB |
BIN
serverless/etc/openwhisk-black.png
Normal file
After Width: | Height: | Size: 5.6 KiB |
BIN
serverless/etc/spotinst-logos-black-small.png
Normal file
After Width: | Height: | Size: 5.3 KiB |
BIN
serverless/etc/webtask-small-grayscale.png
Normal file
After Width: | Height: | Size: 8.2 KiB |
113
serverless/pom.xml
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!--
|
||||||
|
The MIT License
|
||||||
|
Copyright (c) 2014-2017 Ilkka Seppälä
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
-->
|
||||||
|
<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">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>serverless</artifactId>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.iluwatar</groupId>
|
||||||
|
<artifactId>java-design-patterns</artifactId>
|
||||||
|
<version>1.19.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.amazonaws</groupId>
|
||||||
|
<artifactId>aws-lambda-java-core</artifactId>
|
||||||
|
<version>${aws-lambda-core.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.amazonaws</groupId>
|
||||||
|
<artifactId>aws-lambda-java-log4j</artifactId>
|
||||||
|
<version>${aws-lambda-log4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-core</artifactId>
|
||||||
|
<version>${jackson.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
<version>${jackson.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-annotations</artifactId>
|
||||||
|
<version>${jackson.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<!--
|
||||||
|
Using the Apache Maven Shade plugin to package the jar
|
||||||
|
|
||||||
|
"This plugin provides the capability to package the artifact
|
||||||
|
in an uber-jar, including its dependencies and to shade - i.e. rename -
|
||||||
|
the packages of some of the dependencies."
|
||||||
|
|
||||||
|
Link: https://maven.apache.org/plugins/maven-shade-plugin/
|
||||||
|
-->
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
|
<version>2.3</version>
|
||||||
|
<configuration>
|
||||||
|
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>shade</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<finalName>${project.artifactId}</finalName>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
49
serverless/serverless.yml
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#
|
||||||
|
# The MIT License
|
||||||
|
# Copyright (c) 2014 Ilkka Seppälä
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
# THE SOFTWARE.
|
||||||
|
#
|
||||||
|
|
||||||
|
service: lambda-info-http-endpoint
|
||||||
|
|
||||||
|
frameworkVersion: ">=1.2.0 <2.0.0"
|
||||||
|
|
||||||
|
provider:
|
||||||
|
name: aws
|
||||||
|
runtime: java8
|
||||||
|
usagePlan:
|
||||||
|
quota:
|
||||||
|
limit: 500
|
||||||
|
offset: 2
|
||||||
|
period: MONTH
|
||||||
|
throttle:
|
||||||
|
burstLimit: 20
|
||||||
|
rateLimit: 10
|
||||||
|
|
||||||
|
package:
|
||||||
|
artifact: target/serverless.jar
|
||||||
|
|
||||||
|
functions:
|
||||||
|
currentTime:
|
||||||
|
handler: com.iluwatar.serverless.api.LambdaInfoApiHandler
|
||||||
|
events:
|
||||||
|
- http:
|
||||||
|
path: info
|
||||||
|
method: get
|
@ -0,0 +1,166 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
package com.iluwatar.serverless;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Api gateway response
|
||||||
|
*
|
||||||
|
* @param <T> serializable object
|
||||||
|
*/
|
||||||
|
public class ApiGatewayResponse<T extends Serializable> implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1181159426782844892L;
|
||||||
|
|
||||||
|
private final Integer statusCode;
|
||||||
|
private final String body;
|
||||||
|
private final Map<String, String> headers;
|
||||||
|
private final Boolean isBase64Encoded;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* api gateway response
|
||||||
|
*
|
||||||
|
* @param statusCode - http status code
|
||||||
|
* @param body - response body
|
||||||
|
* @param headers - response headers
|
||||||
|
* @param isBase64Encoded - base64Encoded flag
|
||||||
|
*/
|
||||||
|
public ApiGatewayResponse(Integer statusCode, String body, Map<String, String> headers,
|
||||||
|
Boolean isBase64Encoded) {
|
||||||
|
this.statusCode = statusCode;
|
||||||
|
this.body = body;
|
||||||
|
this.headers = headers;
|
||||||
|
this.isBase64Encoded = isBase64Encoded;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* http status code
|
||||||
|
*
|
||||||
|
* @return statusCode - http status code
|
||||||
|
*/
|
||||||
|
public Integer getStatusCode() {
|
||||||
|
return statusCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* response body
|
||||||
|
*
|
||||||
|
* @return string body
|
||||||
|
*/
|
||||||
|
public String getBody() {
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* response headers
|
||||||
|
*
|
||||||
|
* @return response headers
|
||||||
|
*/
|
||||||
|
public Map<String, String> getHeaders() {
|
||||||
|
return headers;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* base64Encoded flag, API Gateway expects the property to be called "isBase64Encoded"
|
||||||
|
*
|
||||||
|
* @return base64Encoded flag
|
||||||
|
*/
|
||||||
|
public Boolean isBase64Encoded() {
|
||||||
|
return isBase64Encoded;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ApiGatewayResponse Builder class
|
||||||
|
* @param <T> Serializable object
|
||||||
|
*/
|
||||||
|
public static class ApiGatewayResponseBuilder<T extends Serializable> {
|
||||||
|
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||||
|
private Integer statusCode;
|
||||||
|
private T body;
|
||||||
|
private Map<String, String> headers;
|
||||||
|
private Boolean isBase64Encoded;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* http status code
|
||||||
|
* @param statusCode - http status code
|
||||||
|
* @return ApiGatewayResponseBuilder
|
||||||
|
*/
|
||||||
|
public ApiGatewayResponseBuilder statusCode(Integer statusCode) {
|
||||||
|
this.statusCode = statusCode;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Serializable body
|
||||||
|
* @param body - Serializable object
|
||||||
|
* @return ApiGatewayResponseBuilder
|
||||||
|
*/
|
||||||
|
public ApiGatewayResponseBuilder body(T body) {
|
||||||
|
this.body = body;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* response headers
|
||||||
|
* @param headers - response headers
|
||||||
|
* @return ApiGatewayResponseBuilder
|
||||||
|
*/
|
||||||
|
public ApiGatewayResponseBuilder headers(Map<String, String> headers) {
|
||||||
|
this.headers = headers;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* base64Encoded glag
|
||||||
|
* @param isBase64Encoded - base64Encoded flag
|
||||||
|
* @return ApiGatewayResponseBuilder
|
||||||
|
*/
|
||||||
|
public ApiGatewayResponseBuilder base64Encoded(Boolean isBase64Encoded) {
|
||||||
|
this.isBase64Encoded = isBase64Encoded;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* build ApiGatewayResponse
|
||||||
|
*
|
||||||
|
* @return ApiGatewayResponse
|
||||||
|
*/
|
||||||
|
public ApiGatewayResponse build() {
|
||||||
|
String strBody = null;
|
||||||
|
if (this.body != null) {
|
||||||
|
try {
|
||||||
|
strBody = OBJECT_MAPPER.writeValueAsString(body);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ApiGatewayResponse(this.statusCode, strBody, this.headers, this.isBase64Encoded);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
140
serverless/src/main/java/com/iluwatar/serverless/LambdaInfo.java
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
package com.iluwatar.serverless;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lambda context information
|
||||||
|
*/
|
||||||
|
public class LambdaInfo implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 3936130599040848923L;
|
||||||
|
|
||||||
|
private String awsRequestId;
|
||||||
|
private String logGroupName;
|
||||||
|
private String logStreamName;
|
||||||
|
private String functionName;
|
||||||
|
private String functionVersion;
|
||||||
|
private Integer memoryLimitInMb;
|
||||||
|
|
||||||
|
public String getAwsRequestId() {
|
||||||
|
return awsRequestId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAwsRequestId(String awsRequestId) {
|
||||||
|
this.awsRequestId = awsRequestId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLogGroupName() {
|
||||||
|
return logGroupName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLogGroupName(String logGroupName) {
|
||||||
|
this.logGroupName = logGroupName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLogStreamName() {
|
||||||
|
return logStreamName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLogStreamName(String logStreamName) {
|
||||||
|
this.logStreamName = logStreamName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFunctionName() {
|
||||||
|
return functionName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFunctionName(String functionName) {
|
||||||
|
this.functionName = functionName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFunctionVersion() {
|
||||||
|
return functionVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFunctionVersion(String functionVersion) {
|
||||||
|
this.functionVersion = functionVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getMemoryLimitInMb() {
|
||||||
|
return memoryLimitInMb;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMemoryLimitInMb(Integer memoryLimitInMb) {
|
||||||
|
this.memoryLimitInMb = memoryLimitInMb;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "LambdaInfo{"
|
||||||
|
+ "awsRequestId='" + awsRequestId + '\''
|
||||||
|
+ ", logGroupName='" + logGroupName + '\''
|
||||||
|
+ ", logStreamName='" + logStreamName + '\''
|
||||||
|
+ ", functionName='" + functionName + '\''
|
||||||
|
+ ", functionVersion='" + functionVersion + '\''
|
||||||
|
+ ", memoryLimitInMb=" + memoryLimitInMb
|
||||||
|
+ '}';
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (o == null || getClass() != o.getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
LambdaInfo that = (LambdaInfo) o;
|
||||||
|
|
||||||
|
if (awsRequestId != null ? !awsRequestId.equals(that.awsRequestId) : that.awsRequestId != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (logGroupName != null ? !logGroupName.equals(that.logGroupName) : that.logGroupName != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (logStreamName != null ? !logStreamName.equals(that.logStreamName) : that.logStreamName != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (functionName != null ? !functionName.equals(that.functionName) : that.functionName != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (functionVersion != null ? !functionVersion.equals(that.functionVersion) : that.functionVersion != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return memoryLimitInMb != null ? memoryLimitInMb.equals(that.memoryLimitInMb) : that.memoryLimitInMb == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
int result = awsRequestId != null ? awsRequestId.hashCode() : 0;
|
||||||
|
result = 31 * result + (logGroupName != null ? logGroupName.hashCode() : 0);
|
||||||
|
result = 31 * result + (logStreamName != null ? logStreamName.hashCode() : 0);
|
||||||
|
result = 31 * result + (functionName != null ? functionName.hashCode() : 0);
|
||||||
|
result = 31 * result + (functionVersion != null ? functionVersion.hashCode() : 0);
|
||||||
|
result = 31 * result + (memoryLimitInMb != null ? memoryLimitInMb.hashCode() : 0);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,83 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
package com.iluwatar.serverless.api;
|
||||||
|
|
||||||
|
import com.iluwatar.serverless.ApiGatewayResponse;
|
||||||
|
|
||||||
|
import com.amazonaws.services.lambda.runtime.Context;
|
||||||
|
import com.amazonaws.services.lambda.runtime.RequestHandler;
|
||||||
|
import com.iluwatar.serverless.LambdaInfo;
|
||||||
|
import org.apache.log4j.BasicConfigurator;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* LambdaInfoApiHandler - simple api to get lambda context
|
||||||
|
* Created by dheeraj.mummar on 2/5/18.
|
||||||
|
*/
|
||||||
|
public class LambdaInfoApiHandler implements RequestHandler<Map<String, Object>, ApiGatewayResponse> {
|
||||||
|
|
||||||
|
private static final Logger LOG = Logger.getLogger(LambdaInfoApiHandler.class);
|
||||||
|
private static final Integer SUCCESS_STATUS_CODE = 200;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ApiGatewayResponse handleRequest(Map<String, Object> input, Context context) {
|
||||||
|
BasicConfigurator.configure();
|
||||||
|
LOG.info("received: " + input);
|
||||||
|
|
||||||
|
return new ApiGatewayResponse
|
||||||
|
.ApiGatewayResponseBuilder<LambdaInfo>()
|
||||||
|
.headers(headers())
|
||||||
|
.statusCode(SUCCESS_STATUS_CODE)
|
||||||
|
.body(lambdaInfo(context))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* lambdaInfo
|
||||||
|
* @param context - Lambda context
|
||||||
|
* @return LambdaInfo
|
||||||
|
*/
|
||||||
|
private LambdaInfo lambdaInfo(Context context) {
|
||||||
|
LambdaInfo lambdaInfo = new LambdaInfo();
|
||||||
|
lambdaInfo.setAwsRequestId(context.getAwsRequestId());
|
||||||
|
lambdaInfo.setFunctionName(context.getFunctionName());
|
||||||
|
lambdaInfo.setFunctionVersion(context.getFunctionVersion());
|
||||||
|
lambdaInfo.setLogGroupName(context.getLogGroupName());
|
||||||
|
lambdaInfo.setLogStreamName(context.getLogStreamName());
|
||||||
|
lambdaInfo.setMemoryLimitInMb(context.getMemoryLimitInMB());
|
||||||
|
|
||||||
|
return lambdaInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, String> headers() {
|
||||||
|
Map<String, String> headers = new HashMap<>();
|
||||||
|
headers.put("Content-Type", "application/json");
|
||||||
|
|
||||||
|
return headers;
|
||||||
|
}
|
||||||
|
}
|
29
serverless/src/main/resources/log4j.properties
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#
|
||||||
|
# The MIT License
|
||||||
|
# Copyright (c) 2014 Ilkka Seppälä
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
# THE SOFTWARE.
|
||||||
|
#
|
||||||
|
|
||||||
|
log = .
|
||||||
|
log4j.rootLogger = DEBUG, LAMBDA
|
||||||
|
|
||||||
|
log4j.appender.LAMBDA=com.amazonaws.services.lambda.runtime.log4j.LambdaAppender
|
||||||
|
log4j.appender.LAMBDA.layout=org.apache.log4j.PatternLayout
|
||||||
|
log4j.appender.LAMBDA.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss} <%X{AWSRequestId}> %-5p %c:%L - %m%n
|
@ -0,0 +1,49 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 Ilkka Seppälä
|
||||||
|
* <p>
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
* <p>
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
* <p>
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
package com.iluwatar.serverless.api;
|
||||||
|
|
||||||
|
import com.amazonaws.services.lambda.runtime.Context;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.runners.MockitoJUnitRunner;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.core.IsNull.notNullValue;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* LambdaInfoApiHandlerTest
|
||||||
|
*/
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class LambdaInfoApiHandlerTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void handleRequestWithMockContext() {
|
||||||
|
LambdaInfoApiHandler lambdaInfoApiHandler = new LambdaInfoApiHandler();
|
||||||
|
Context context = mock(Context.class);
|
||||||
|
when(context.getAwsRequestId()).thenReturn("mock aws request id");
|
||||||
|
|
||||||
|
assertThat(lambdaInfoApiHandler.handleRequest(null, context), notNullValue());
|
||||||
|
}
|
||||||
|
}
|