Jenkins pipeline to build and deploy a web application on AWS EC2 resources. One instance builds an application, other one starts it.
- 0 - Jenkins pulls this repository and processes Jenkinsfile
- 1 - Terraform deploys an infrastructure on AWS EC2: two instances (Builder and Webserver)
- 2 - Ansible configures instances
- 3 - Docker builds an application on the Builder instance
- 3a - Docker pushes an artifact container to AWS ECR repository
- 4 - Docker cleans containers on the Webserver instance
- 4a - Docker pulls an artifact from AWS ECR repository and start it
Docker on the Jenkins host uses ssh endpoint to work with remote docker-socket
Terraform just destroy AWS EC2 instances (Builder and Webserver). AWS ECR repository is not touched.
- appVersion is a version of the application (default is 1.0)
- ecrHost is a hostname of ECR (default is empty)
- autoApprove is true or false (default), automatically run Terraform apply after generating plan or user approvement is required
- destroy is true or false (default), destroy Terraform build or not
- Jenkinsfile (pipeline)
- *.tf (Terraform files)
- prepare-instances.yml (Ansible playbook)
- Dockerfile, app.py, requirements.txt (Python application)
Just a simple Hello world web server based on the python flask. It shows version that set by Jenkins parameter appVersion.
-
Install aws cli, terraform, ansible, Jenkins (plugins: SSH Agent, Ansible, Terrafotm)
-
Generate ssh key and import it to the AWS EC2
ssh-keygen -t rsa -C "aws-ec2-key" -f ~/.ssh/aws-ec2-key aws ec2 import-key-pair --key-name devops-cert_task-key \ --public-key-material fileb://~/.ssh/aws-ec2-key.pub
-
Create ECR repository
aws ecr create-repository --repository-name cert_task
-
Create Jenkins job
Dashboard -> New job -> type Pipeline, name devops-cert_task
Pipeline Definition: Pipeline script from SCM, Git
Repository URL: https://github.com/LovingFox/devops-cert_task.git -
Create Jenkins credentials for ssh
Kind: SSH Username with private key
ID: AWS_UBUNTU_INSTANCE_SSH_KEY
Username: ubuntu
Key:cat ~/.ssh/aws-ec2-key -
Create Jenkins credentials for AWS ECR repository
Kind: SSH Username with private key
ID: AWS_ECR_CREDENTIALS Username: AWS
Password:aws ecr get-login-password
-
Create Jenkins credentials for AWS API
Kind: Secret text
ID: AWS_ACCESS_KEY_ID
Secret: <Your AWS Access Key ID>Kind: Secret text
ID: AWS_SECRET_ACCESS_KEY
Secret: <AWS Secret Access Key> -
Start Jenkins job by GUI or by cli:
java -jar jenkins-cli.jar build -v -f devops-cert_task \ -p autoApprove=true -p appVersion=1.0 \ -p ecrHost=657846606580.dkr.ecr.eu-central-1.amazonaws.com657846606580.dkr.ecr.eu-central-1.amazonaws.com is just an example hostname of ECR, change it by yours
-
Check the application is working:
cutl http://curl http://<host name>.<region>.compute.amazonaws.com
URL of the webserver is prined at the end of the Jenkins job
-
Destroy the infrastructure by GUI or by cli:
java -jar jenkins-cli.jar build -v -f devops-cert_task -p destroy=true
