This project demonstrates a simple CI/CD pipeline for a java application.
Behind the scenes we're creating a VM which we configure and deploy the app, and then we take a snapshot which is then deployed without downtime to a live cluster behind an AWS Load Balancer.
You can targert multiple environments using different branches.
- master: production
- sta: staging
- test: testing
- dev: development
You can skip CI builds by adding [skip ci]
to your commit message.
Create two IAM users with the permissions linked below and export as env variables for local usage.
Packer (permissions):
export AWS_ACCESS_KEY="abc"
export AWS_SECRET_KEY="xyz"
Terraform(Needs full rights to EC2, Auto Scaling, Launch Configuration, ELB, Security Groups, and the S3 bucket below):
export AWS_ACCESS_KEY_ID="abc"
export AWS_SECRET_ACCESS_KEY="xyz"
Create an S3 bucket named (Terraform state is kept here between CI executions)
opstest-terra-state
Pull repo
git clone [email protected]:Lusitaniae/java-aws-cicd.git; cd java-aws-cicd
Test App
make test
Bake AMI
make bake
Deploy Infrastructure (or changes only, say deploy new AMI)
make deploy AMIID=ami-xxxxxxxxxxxx ENV=<prod|sta|test|dev>
TravisCI is running the show.
On commit and or PR, a pipeline with 2 stages is started.
- Test
- Test the Java application
- Deploy
- Build AMI using Packer (Ansible does the configuration)
- Deploy new AMI to ASG using Terraform.
Deployments are fully online using Blue Green strategy.
New ASG and LC are configured with new AMI and once healthy they replace the original ones in the ELB.
The stack consists of
- Classic Elastic Load Balancer,
- Launch Configuration, and
- Auto Scaling Group.
They're configured to span multiple AZs and by configuring min, max, desired accordingly we can achieve HA in case of AZ loss (e.g. 3 min, 9max, 3 desired).
Could be improved by moving instances to a private subnet in the respective AZ to restrict access.
S3 is used to store Terraform state between executions (from CI or locally). See required permissions here https://www.terraform.io/docs/backends/types/s3.html#protecting-access-to-workspace-state
Each commit is generating new AMIs, needs lifecycle management tool.