Skip to content

Instantly share code, notes, and snippets.

@potfur
Last active December 20, 2018 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save potfur/780c4aa555dcbd65125eaac481e23580 to your computer and use it in GitHub Desktop.
Save potfur/780c4aa555dcbd65125eaac481e23580 to your computer and use it in GitHub Desktop.
AWS Lambda : Kotlin
{
"requestContext": {
"elb": {
"targetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/lambda-target/d6190d154bc908a5"
}
},
"httpMethod": "GET",
"path": "/health",
"queryStringParameters": {
"price": 1000,
"weight": 500
},
"headers": {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"accept-encoding": "gzip",
"accept-language": "en-US,en;q=0.5",
"connection": "keep-alive",
"cookie": "cookie",
"host": "lambda-846800462.elb.amazonaws.com",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:60.0) Gecko/20100101 Firefox/60.0",
"x-amzn-trace-id": "Root=1-5bdb40ca-556d8b0c50dc66f0511bf520",
"x-forwarded-for": "72.21.198.66",
"x-forwarded-port": "80",
"x-forwarded-proto": "http"
},
"body": "",
"isBase64Encoded": false
}
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.11'
}
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'jars', include: '*.jar')
compile 'com.amazonaws:aws-lambda-java-core:1.1.0'
compile 'com.amazonaws:aws-lambda-java-events:1.1.0'
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
compile "org.jetbrains.kotlin:kotlin-reflect"
compile "com.fasterxml.jackson.module:jackson-module-kotlin:2.9.7"
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
task buildZip(type: Zip) {
from compileKotlin
from processResources
into('lib') {
from configurations.compileClasspath
}
}
build.dependsOn buildZip
package example
import com.amazonaws.services.lambda.runtime.Context
import java.io.*
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.module.kotlin.*
@JsonIgnoreProperties(ignoreUnknown = true)
data class ALBRequest(
val requestContext: Map<String, Map<String, String>>,
val httpMethod: String,
val path: String,
val queryStringParameters: Map<String, String>,
val headers: Map<String, String>,
val body: String
)
data class Response(val message: String, val status: Int = 200)
class Main {
private val mapper = jacksonObjectMapper()
fun handler(input: InputStream, output: OutputStream, context: Context): Unit {
val request = mapper.readValue<ALBRequest>(input)
mapper.writeValue(output, Response(request.toString(), 400))
}
}
.PHONY: clear build test
build:
gradle build
test: build
rm -rf runtime/
unzip -d runtime build/distributions/lambda.kt.zip
cat $(payload) | docker run --rm -v $(PWD)/runtime:/var/task -i -e DOCKER_LAMBDA_USE_STDIN=1 lambci/lambda:java8 example.Main::handler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment