Gradle AWS Plugin
For Gradle projects, deployment can be made even more straightforward using the Gradle AWS Plugin. This plugin provides a deploy
task which can push your function to AWS Lambda directly, as well as a AWSLambdaInvokeTask
which can be used to invoke your function when it is deployed.
Example build.gradle
import com.amazonaws.services.lambda.model.InvocationType
import jp.classmethod.aws.gradle.lambda.AWSLambdaInvokeTask
import jp.classmethod.aws.gradle.lambda.AWSLambdaMigrateFunctionTask
import com.amazonaws.services.lambda.model.Runtime
buildscript {
repositories {
...
maven { url "https://plugins.gradle.org/m2/" } (1)
}
dependencies {
classpath "jp.classmethod.aws:gradle-aws-plugin:0.22"
}
}
apply plugin: 'jp.classmethod.aws.lambda' (2)
...
task deploy(type: AWSLambdaMigrateFunctionTask, dependsOn: shadowJar) {
functionName = "hello-world"
handler = "example.HelloWorldFunction::hello"
role = "arn:aws:iam::${aws.accountId}:role/lambda_basic_execution" (3)
runtime = Runtime.Java8
zipFile = shadowJar.archivePath
memorySize = 256
timeout = 60
}
task invoke(type: AWSLambdaInvokeTask) {
functionName = "hello-world"
invocationType = InvocationType.RequestResponse
payload = '{"name":"Fred"}'
doLast {
println "Lambda function result: " + new String(invokeResult.payload.array(), "UTF-8")
}
}
1 | The AWS Gradle plugin is hosted from the https://plugins.gradle.org repository |
2 | Apply the Gradle AWS plugin |
3 | The Gradle AWS plugin will resolve your AWS credentials from .aws/credentials file, which is the default location used by the AWS CLI to set up your environment |
Note that the value of the handler
property of the deploy
task should be either:
In this case of Java or Kotlin:
io.micronaut.function.aws.MicronautRequestStreamHandler
In the case of Groovy function definitions: A reference to the function (in the above case
example.HelloWorldFunction::hello
)
The reason for this is the function-groovy
dependency applies additional code transformations to make it possible to reference the function directly.
With the above build configuration, the function can be deployed to AWS Lambda using the deploy
task.
$ ./gradlew deploy
The deployed function can then be invoked.
$ ./gradlew invoke
Hello, Fred
Consult the Gradle AWS plugin documentation for more details on the use of the plugin.