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

  1. import com.amazonaws.services.lambda.model.InvocationType
  2. import jp.classmethod.aws.gradle.lambda.AWSLambdaInvokeTask
  3. import jp.classmethod.aws.gradle.lambda.AWSLambdaMigrateFunctionTask
  4. import com.amazonaws.services.lambda.model.Runtime
  5. buildscript {
  6. repositories {
  7. ...
  8. maven { url "https://plugins.gradle.org/m2/" } (1)
  9. }
  10. dependencies {
  11. classpath "jp.classmethod.aws:gradle-aws-plugin:0.22"
  12. }
  13. }
  14. apply plugin: 'jp.classmethod.aws.lambda' (2)
  15. ...
  16. task deploy(type: AWSLambdaMigrateFunctionTask, dependsOn: shadowJar) {
  17. functionName = "hello-world"
  18. handler = "example.HelloWorldFunction::hello"
  19. role = "arn:aws:iam::${aws.accountId}:role/lambda_basic_execution" (3)
  20. runtime = Runtime.Java8
  21. zipFile = shadowJar.archivePath
  22. memorySize = 256
  23. timeout = 60
  24. }
  25. task invoke(type: AWSLambdaInvokeTask) {
  26. functionName = "hello-world"
  27. invocationType = InvocationType.RequestResponse
  28. payload = '{"name":"Fred"}'
  29. doLast {
  30. println "Lambda function result: " + new String(invokeResult.payload.array(), "UTF-8")
  31. }
  32. }
1The AWS Gradle plugin is hosted from the https://plugins.gradle.org repository
2Apply the Gradle AWS plugin
3The 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.

  1. $ ./gradlew deploy

The deployed function can then be invoked.

  1. $ ./gradlew invoke
  2. Hello, Fred

Consult the Gradle AWS plugin documentation for more details on the use of the plugin.