Creation of AWS Instance and Installing Java using Ansible

Reading Time: 4 minutes



Amazon Web Services (AWS) provides cloud services throughout the world also AWS provides servers, storage, networking, remote computing, email, mobile development, and security.

AWS EC2: Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides secure, resizable compute capacity in the cloud. It is designed to make web-scale cloud computing easier for developers.
Indeed EC2’s is a simple web service interface that allows you to obtain and configure capacity with minimal friction.

Instance: An instance is a virtual server that exists in the cloud and we can access it anywhere with the help of the internet.

AWS Instance: An instance is a virtual server in the AWS Cloud. With Amazon EC2, you can set up and configure the operating system and applications that run on your instance.

Create your EC2 resources and launch your EC2 instance:-

Open the Amazon EC2 console at

Choose Launch Instance.

1: Choose an Amazon Machine Image (AMI), find an Amazon Linux 2 AMI at the top of the list, and choose Select.

2: Select an Instance Type, then Next: Configure Instance Details.

3: After that configure Instance Details, provide the following information:
Leave the Number of instances at once.
Leave the Purchasing option at the default setting.
For Subnet, choose a default subnet in any Availability Zone.
The User data automatically includes the commands for mounting your Amazon EFS file system.

4: Next: Add Storage.

5: Next: Add Tags.

Name your instance and choose Next: Configure Security Group.

6: Configure Security Group, set Assign a security group to Select an existing security group. Choose the default security group to make sure that it can access your EFS file system.

You can use the following settings to add SSH access:
-Type: SSH
-Protocol: TCP
-Port Range: 8080
-Source: Anywhere

7: Review and Launch.

8: Launch.

Select the check box for the key pair that you created, and then choose
Launch Instances.How EC2 Instance C.

How EC2 Instance Connect works:-

When the EC2 Instance Connect feature is enabled on an instance, the SSH daemon (sshd) on that instance is configured with a custom Authorized Keys Command script. This script updates the Authorized Keys Command to read SSH public keys from instance metadata during the SSH authentication process and connects you to the instance.
The SSH public keys are only available for one-time use for 60 seconds in the instance metadata. To connect to the instance successfully, you must connect using SSH within this time window. Because the keys expire, there is no need
to track or manage these keys directly, as you did previously.

Connecting to an instance using SSH:-

Use the SSH key pair that was assigned to your instance when you launched it and use the default user name of the AMI that you used to launch your instance. For an Ubuntu AMI, the user name is Ubuntu.

ssh -i my_ec2_private_key.pem
After that, we can access our instance from our system and make use of it.

Using Ansible to install Java on an ec2 box running in the cloud

Step-1: Install ansible

Step-2: Install Amazon EC2 Ansible integration tool

We will run a Python script and setup a few environment variables. We’ll need to set environment variables for our Secret and Access key:

Now you’ll need to set a few more environment variables for the inventory management script
$ export ANSIBLE_HOSTS=/etc/ansible/

This tells Ansible to use the dynamic EC2 script instead of a static /etc/ansible/hosts file.
Open up in a text editor and make sure that the path to the ec2.ini config file is defined correctly at the top of the script:

$ export EC2_INI_PATH=/etc/ansible/ec2.ini
This tells where the ec2.ini config file is located.

Step-3: Start ssh-agent with your key

$ ssh-agent bash
$ ssh-add ~/.ssh/YOUR_KEY.pem

Step-4: Install ansible Oracle Java install plugin

$ ansible-galaxy install

Step-5: Start-up an EC2 box tag it as elk=elk

Start up an EC2 instance and tag it with the tag elk=elk. This is just the type of box. In this case, we will do the process of writing an ansible setup script for an ELK stack.

Step-6: Test your ansible connection with the ping module

$ ansible tag_elk_elk -m ping -u ec2-user | SUCCESS => {
    "changed": false, 
    "ping": "pong"

Step-7: Create an ansible-playbook

- hosts: tag_elk_elk
  user: ec2-user
  sudo: yes
     - { role:,
         oracle_java_set_as_default: yes,
         oracle_java_version: 8,
         oracle_java_version_update: 102,
         oracle_java_version_build: 14}

  - name: ensure apache is at the latest version
    yum: name=httpd state=latest

Step-8: Run the ansible-playbook

$ ansible-playbook elk_install_playbook.yml 

Now the ansible will install the Java using an EC2 box running in the cloud.


In short, AWS gives us the opportunity to create an instance and work on it free of cost and EC2 is a virtual machine that represents a physical server for you to deploy your applications. There are many benefits of using AWS instance it provides us with Elastic Web-Scale Computing, Amazon EC2 enables you to increase or decrease capacity within minutes, not hours or days, We have complete control of your instances, Flexible Cloud Hosting Services, reliable,
secure, inexpensive and easy to start.

Written by 

Deeksha Tripathi is a Software Consultant at Knoldus Inc Software. She has a keen interest toward learning new technologies. Her practice area is DevOps. When not working, she will be busy in listening music , and spending time with her family .