Gradle plugin for deploying Serverless Java applications using AWS Serverless Application Models (SAM) via CloudFormation templates.
- Add this plugin to your build script, see https://plugins.gradle.org/plugin/com.github.kaklakariada.aws-sam-deploy
plugins {
id 'java'
id 'com.github.kaklakariada.aws-sam-deploy' version '0.6.1'
}
- Configure your AWS credentials in
~/.aws/
, see https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html#cli-config-files - Create a SAM cloud formation template, see https://github.com/awslabs/serverless-application-model
- Optionally: create a swagger interface definition, see http://swagger.io/specification/
- Create file
gradle.properties
in your project dir with the following entries:awsDeploymentBucket
: Bucket used for uploading lambda code and swagger definition, e.g.my-deployment-bucket
awsRegion
: AWS region in which to deploy your application, e.g.eu-west-1
- Add a
serverless
section to your build script and configure your stages:
ext {
deployStage = project.hasProperty('stage') ? project.properties['stage'] : 'test'
}
serverless {
activeStage = deployStage
defaultAwsProfile = 'default'
defaultAwsRegion = awsRegion
defaultDeployBucket = awsDeploymentBucket
stages {
test {
// use default values
}
prelive {
awsRegion = 'eu-west-1'
awsProfile = 'prelive-profile'
deploymentBucket = 'prelive-bucket'
}
}
api {
stackName = "${project.name}-${deployStage}"
samTemplate = file('template.yml')
}
}
aws --profile $profile iam create-policy-version --policy-arn $policy_arn --policy-document file://iam-deploy-policy.json --set-as-default
Deploy your app with ./gradlew -Pstage=<myStage> deploy -i
.
This will
- Build a zip of your application including dependencies
- Upload the zip to the configured S3 bucket
- Replace some placeholders in your swagger definition and upload it to S3
- Replace some placeholders in your CloudFormation template and create a change set
- Execute the change set to create/update the CloudFormation stack
Delete your complete stack with ./gradlew -Pstage=<myStage> deleteStack -i
.
Be careful, this will may also delete DynamoDB tables.
- example-project-minimal: minimal project
- example-project-swagger: minimal project with swagger definition in separate file
- example-project-inline-swagger: minimal project with inline swagger definition in CloudFormation template
$ git clone https://github.com/kaklakariada/aws-sam-gradle.git
Import into eclipse using buildship.
$ ./gradlew licenseFormat
Publish to plugins.gradle.org
See https://plugins.gradle.org/docs/submit for details.
-
Add API Key from https://plugins.gradle.org to
~/.gradle/gradle.properties
:gradle.publish.key = ... gradle.publish.secret = ...
-
Run
./gradlew publishPlugins