Purpose of this workshop:
- Lower barrier to use Devops starter-kit
- Teach how to provision an application in the cloud
General Topics covered:
- Automated testing
- Automated builds
- Continuous deployment
- Infrastructure as code
- Cloud
You will learn to use:
- Dockerfile
- AWS console
- Ansible playbook
- Jenkinsfile
Loading the Demo application onto your machine, and verify that it works.
Application
- Clone the repository
- Import the project into IntelliJ
- Set your project JDK to JDK 11
- Verify that you can run the tests, and that they pass
- Run the application, and verify you can access the application in your browser, and see "Hello world Piet"
- Create your own git branch
- Change "Hello world Piet" to "Hello world ", and update the Unit Test.
Ansible & AWS
- Create a virtualenv to install ansible and the aws-cli
# create a virtual environment for python 3 (such that it does not collide with other python projects) python3 -m venv .venv # turn on the venv (this is why this script should be sourced) source ./.venv/bin/activate # install: ansible, awscli and boto (to allow ansible to talk to aws) pip install -r ops/local/requirements.txt
- Setup your machine to access AWS
- https://console.aws.amazon.com/iam/home?region=eu-west-1#/security_credentials
- create access key
- save the credentials to
~/.aws/credentials
with format:[devops-workshop] aws_access_key_id = YOUR_AWS_ACCESS_KEY_ID aws_secret_access_key = YOUR_AWS_SECRET_ACCESS_KEY
- WARNING: For those who already have a project on another AWS account.
- Don't run playbooks on your DEFAULT AWS profile.
- Run the
ansible-playbook test.yml
from theops/ansible
directory.- You also need to specify your AWS profile in the environment variable
AWS_PROFILE
. - Verify that ansible works, and you get no errors in the output.
- You also need to specify your AWS profile in the environment variable
Creating a docker image that will run your application.
Have a look at:
- Create a directory for building a docker image, containing:
- A copy of the application JAR file
- A new Dockerfile
- Dockerfile:
- Based on OpenJDK 11
- Specify that the JAR should be added during build
- Make sure your application port is available from outside of the container
- Let your docker image run Java with the copied JAR file on boot
- Build your dockerfile using the
docker build
command - Run your image with
docker run
(don't forget port mapping) - See that you can access the application from localhost
Pushing the image to the docker registry.
- Build your image with a tag that points to the Docker Repository, add a tag with your name
- De docker repository voor deze workshop is
0000000000000.dkr.ecr.eu-west-1.amazonaws.com/devops-workshop-demo
- You can find this by going to AWS ECR in the Ierland region
- De docker repository voor deze workshop is
- Login into the Docker Repository on amazon (ECR)
$(AWS_PROFILE=devops-workshop aws ecr get-login --no-include-email --region eu-west-1)
- Push your image to the repository (specify the full url, excluding the tag)
https://eu-west-1.console.aws.amazon.com/ecr/repositories/devops-workshop-demo/?region=eu-west-1
- Create an SSH key-pair
- Add your SSH public key to AWS
- aws console > EC2 > Key Pairs > Import Key Pair
- Key pair name: devops-workshop-Piet
- Create an EC2 from the console
- Use our devops workshop image (devops-workshop-application-ami)
- Use our devops workshop security group (devops-workshop-sg)
- Use our devops workshop role (devops-workshop-ec2)
- Preferably with a tag: Name: Piet
- SSH to your instance with ssh
- Find your IP in the AWS console
- The username is
ec2-user
- Launch the application
- login to the docker container registry
- use
docker run
(with your image)
Checklist:
- You can access the application online, and see "Hello world (your name)"
Deploying an EC2
Create an Ansible playbook with a task that deploys an EC2:
- Region
- AMI
- Role
- Security Group
- SSH Key pair name
- instance type
- tags
Hints:
- Have a look at the test.yml playbook for an example.
- Search for "Ansible ec2" on google for documentation.
- Use AWS Console to find the ID's of the resources above (use your own ec2 as an example)
Checklist:
- You can SSH to the EC2
- Terminate your instance after you're done
Starting the Application on boot
Launch your ec2 instance with a user-data script that runs your docker image.
Hints:
- Check the Ansible EC2 documentation
- Pass User-Data to the ec2, and indicate that it's a bash script
- You will need yml multiline strings
- Remember the commands you ran to start the application manually on EC2
- Check the logs to see if your script executed correctly
/var/log/cloud-init-output.log
Checklist:
- You can view your application, online, in the browser
- Manually terminate your instance
Visit Jenkins:
- url: the ip-address of ec2: devops-workshop-jenkins
- user: admin
- password: ask the facilitator to share it in the slack channel
Task:
- Modify the Jenkinsfile (
ops/jenkins/Jenkinsfile
) - Stages:
- Building & Testing the application with gradle
- Building & Pushing the docker image
- Deploying the application on EC2 with ansible
Hints:
- Jenkins is preinstalled with docker, java, ansible and aws
- You do not need to activate the virtual environment
- Speed up your Jenkinsfile experiments!
- Edit and rerun your pipeline from within Jenkins
- Click on a build number, click "Replay", click Run
- Run gradle without the daemon
Checklist:
- Change your hello world message
- Wait for the pipeline to finish
- See that you see your newly deployed application
- Cleanup your old instances