serveless implementation using aws compute engine and serverless framework

This commit is contained in:
Dheeraj Mummareddy
2018-02-08 12:03:00 -05:00
parent e7b119c95c
commit 7a7ba871dc
16 changed files with 755 additions and 0 deletions

View File

@ -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);
}
}
}

View 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;
}
}

View File

@ -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;
}
}

View 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

View File

@ -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());
}
}