In this blog, I will be demonstrating how to create an AWS instance and how to deploy your play application on that AWS instance using Ansible.
First let’s see how to make an AWS instance:-
- Login to AWS console
- Click on launch instance on top and select the free instance
- Select the machine which you want to set up.
- Click on Configure Instance Details -> Select the number of instances -> Click Add Storage ->Add tags ->Next configure Security group
- Here we have to add a rule as our play application works on 9000 port
- Click on Add Rule ->Select Type =Custom TCP Port, Port Range=9000, Source= Anywhere and click Review and launch.
- Here you will get a key pair to log in to your machine. Download it so that in future you can use it.
- Now make an ssh connection with your machine
Command ssh -i abc.pem ec2-user@IP
Here abc.pem is the key which you have Downloaded and IP is the IP of your machine.
Now open your terminal and write command to create an executable file.
Command sbt dist
This command will create a zip file in ./target/universal/ folder
Now let’s see the script that we will write to deploy the application on AWS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
— | |
– hosts: test-servers | |
become: yes | |
become_user: root | |
tasks: | |
– name: Check whether the executable file exists or not | |
stat: | |
path: /home/ec2-user/assignment-1.0-SNAPSHOT | |
register: stat_result | |
– name: "Unzip module" | |
unarchive: src=/home/knoldus/play/target/universal/assignment-1.0-SNAPSHOT.zip dest=/home/ec2-user | |
when: stat_result.stat.exists == False | |
– name: "Deploy website" | |
action: "shell sh /home/ec2-user/assignment-1.0-SNAPSHOT/bin/assignment >> /dev/null &" | |
async: 60 | |
poll: 0 | |
when: stat_result.stat.exists == False |
Here I have used the
stat module:- to see whether the file exists or not and store the result in variable stat_result which I have used in the next task.
If the file is found then I skip the task else I have used
unarchive module:- to copy and extract the zip file in target destination.
Now to deploy play website I have used
action plugin: –which is used to run my desired task in the background.
Async:– is used to asynchronous complete the task.
polling:- is in how much time should I poll to see whether the task is completed or not.
Now we can run our application on IP:9000 port.
Good and nice article. Clean content. Kindly know Farm house for rent in Mysore
It would be nice to see the setup of the dependencies in the Playbook as well. Like installing/setting up a JDK. Also any thoughts on doing a Play app inside a Docker container?
you can create a image of your play app and Deploy it on DockerHub and then pull that image in you aws