A simple build and deploy example for deploying Serverless service with Ansible.
Docker is required for deploying the playbook or alternatively Ansible and all the dependencies defined in Dockerfile installed in you environment.
For bigger scale Serverless projects there is also a more advanced version serverless-deployment-ansible that deploys Serverless service from built artifacts.
group_vars
default variablesinventories
inventory files and variables for environments (development, production, etc.)roles
infra
role for infrastructure, vpc, database etc.service
role for Serverless service
scripts
scripts that helps deployment
When using jenkins for deployment, easiest way is to setup Jenkins into EC2 instance running in your AWS account. Here is quick and dirty instructions how to do that Jenkins setup instructions
In addition to suggested plugins, install following plugins also:
- Pipeline: AWS Steps
- Version Number Plug-In
- Get services from git, S3 bucket or other places with Ansible
- Deploy Serverless service with Ansible Serverless module
- Deploy other services, e.g. copy frontend files to buckets, deploy CloudFormation stacks
When deploying from local environment AWS secrets needs to be passed to deployment container, for that e.g. .deploy.sh
script in the project root with contents of
#!/usr/bin/env bash
export AWS_ACCESS_KEY=my-access-key
export AWS_SECRET_KEY=my-secret-key
export SECRET=example-lambda-env-variable
./scripts/deploy-local.sh
might ease up the deployment flow.
- Build Dockerfile with
./scripts/build-docker.sh
- Run
./.deploy.sh
When using Jenkins on AWS EC2, the role of the instance needs to have permissions to deploy CloudFormation stacks, create S3 buckets, IAM Roles, Lambda and other services that are used in Serverless service.
node {
stage('Checkout repository') {
git 'https://github.com/SC5/serverless-deployment-ansible-lite.git'
}
stage('Build Docker image') {
sh "./scripts/build-docker.sh"
}
stage('Deploy') {
withEnv(['SECRET=my-example-secret']) {
sh './scripts/deploy-development.sh'
}
}
}