Skip to content

Instantly share code, notes, and snippets.

@varokas
Created March 18, 2020 05:23
Show Gist options
  • Save varokas/327890e22a46a64c47778d940cc3fa66 to your computer and use it in GitHub Desktop.
Save varokas/327890e22a46a64c47778d940cc3fa66 to your computer and use it in GitHub Desktop.
Java Lambda
apply plugin: 'java'
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile (
'com.amazonaws:aws-lambda-java-core:1.1.0',
'com.amazonaws:aws-lambda-java-log4j:1.0.0',
'com.fasterxml.jackson.core:jackson-core:2.9.10',
'com.fasterxml.jackson.core:jackson-databind:2.9.10',
'com.fasterxml.jackson.core:jackson-annotations:2.9.10'
)
}
// Task for building the zip file for upload
task buildZip(type: Zip) {
// Using the Zip API from gradle to build a zip file of all the dependencies
//
// The path to this zip file can be set in the serverless.yml file for the
// package/artifact setting for deployment to the S3 bucket
//
// Link: https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Zip.html
// set the base name of the zip file
baseName = "hello"
from compileJava
from processResources
into('lib') {
from configurations.runtime
}
}
build.dependsOn buildZip
task wrapper(type: Wrapper) {
gradleVersion = '3.5'
}
05:20:22
START RequestId: 8506d27b-2ead-43ff-933b-7a97b7437892 Version: $LATEST
05:20:22
2020-03-18 05:20:22 <8506d27b-2ead-43ff-933b-7a97b7437892> INFO com.serverless.Handler:17 - received: {}
05:20:22
END RequestId: 8506d27b-2ead-43ff-933b-7a97b7437892
05:20:22
REPORT RequestId: 8506d27b-2ead-43ff-933b-7a97b7437892 Duration: 588.16 ms Billed Duration: 600 ms Memory Size: 1024 MB Max Memory Used: 102 MB Init Duration: 417.09 ms
package com.serverless;
import java.util.Collections;
import java.util.Map;
import org.apache.log4j.Logger;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class Handler implements RequestHandler<Map<String, Object>, ApiGatewayResponse> {
private static final Logger LOG = Logger.getLogger(Handler.class);
@Override
public ApiGatewayResponse handleRequest(Map<String, Object> input, Context context) {
LOG.info("received: " + input);
Response responseBody = new Response("Go Serverless v1.x! Your function executed successfully!", input);
return ApiGatewayResponse.builder()
.setStatusCode(200)
.setObjectBody(responseBody)
.setHeaders(Collections.singletonMap("X-Powered-By", "AWS Lambda & serverless"))
.build();
}
}
$ sls deploy ✘ 1
Serverless: Packaging service...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
........
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service hello.zip file to S3 (1.96 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
...............
Serverless: Stack update finished...
Service Information
service: java-lambda
stage: dev
region: us-east-1
stack: java-lambda-dev
resources: 6
api keys:
None
endpoints:
None
functions:
hello: java-lambda-dev-hello
layers:
None
$ sls invoke -f hello
{
"statusCode": 200,
"body": "{\"message\":\"Go Serverless v1.x! Your function executed successfully!\",\"input\":{}}",
"headers": {
"X-Powered-By": "AWS Lambda & serverless"
},
"isBase64Encoded": false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment