Amazon AWS is no doubt the best public cloud
out there. As we discussed in previous tutorials, Ansible is a very handy tool for sysops to maintain their company infrastructure.
In this tutorial we will go over steps on how to create, start and setup Amazon EC2 instance using simple Ansible scripts.
Details:
- specify instance_type:
t2.micro
- specify security_group:
crunchify_security_grp
- Change the security group as per your need.
- specify image:
ami-crunchify231di
- You need to create Amazon Image before executing this.
- specify keypair:
crunchify
- This is your security key for password less login.
- choose default region:
us-east-2
- Default region that I would recommend.
- number of VMs you want to start: 1
- start with VM 1.
- create basic firewall group
- create Amazon EC2 instance
- Wait for instance to come up
- Get IP address and save in file crunchify.txt file
- you need to create crunchify.txt before executing this ansible script.
- Tag newly created instance as crunchify
Step-1)
Install ansible on macOS. Make sure you have setup Ansible right way 🙂
Step-2)
You need to export your AWS Access Key
and Secret Access Key
. Please follow tutorial on how to Setup Amazon AWS CLI to get your keys.
export AWS_ACCESS_KEY_ID=JHKHLJLHJHJK2SHIY27AIF export AWS_SECRET_ACCESS_KEY=QLKJDKIAYXNIWN2ZHIY27AI54345HKLHJ
Step-3) Create crunchify-host file
[local] localhost ansible_connection=local ansible_python_interpreter=python
Step-4) Create crunchify-ec2.yml file
--- - name: Provision an EC2 Instance. Detailed steps by Crunchify. hosts: local connection: local gather_facts: False tags: provisioning # required parameters vars: instance_type: t2.micro security_group: crunchify_security_grp image: ami-crunchify231di keypair: crunchify region: us-east-2 # Change the Region count: 1 # Task that will be used to Launch/Create an EC2 Instance tasks: - name: Create a security group local_action: module: ec2_group name: "{{ security_group }}" description: Security Group for Crunchify's EC2 Servers region: "{{ region }}" rules: - proto: tcp from_port: 22 to_port: 22 cidr_ip: 0.0.0.0/0 - proto: tcp from_port: 8080 to_port: 8080 cidr_ip: 0.0.0.0/0 - proto: tcp from_port: 443 to_port: 443 cidr_ip: 0.0.0.0/0 rules_egress: - proto: all cidr_ip: 0.0.0.0/0 register: basic_firewall - name: Launching Crunchify's the new EC2 Instance local_action: ec2 group={{ security_group }} instance_type={{ instance_type}} image={{ image }} wait=true wait_timeout=500 region={{ region }} keypair={{ keypair }} count={{count}} register: ec2_crunchify - name: Add the newly created EC2 instance(s) to the local host group local_action: lineinfile path=crunchify.txt regexp={{ item.public_ip }} insertafter='\[crunchify\]' line={{ item.public_ip }} with_items: '{{ec2_crunchify.instances}}' - name: Add new instance to Crunchify's host group add_host: hostname: "{{ item.public_ip }}" groupname: launched with_items: "{{ ec2_crunchify.instances }}" - name: Let's wait for SSH to come up. Usually that takes ~10 seconds local_action: wait_for host={{ item.public_ip }} port=22 state=started with_items: '{{ ec2_crunchify.instances }}' - name: Add tag to Instance(s) local_action: ec2_tag resource={{ item.id }} region={{ region }} state=present with_items: '{{ ec2_crunchify.instances }}' args: tags: Name: crunchify
Step-5) Execute ansible playbook
ansible-playbook -i ./hosts crunchify-ec2.yml
Ansible Result:
bash3.2 $ ansible-playbook -i ./hosts crunchify-ec2.yml PLAY [Provision an EC2 Instance. Detailed steps by Crunchify.] **************************************************************************************************************** TASK [Create a security group] ************************************************************************************************************************************************ ok: [localhost -> localhost] TASK [Master - Launch the new EC2 Instance] *********************************************************************************************************************************** changed: [localhost -> localhost] TASK [Add the newly created EC2 instance(s) to the local host group] ********************************************************************************************************** changed: [localhost -> localhost] => (item={u'ramdisk': None, u'kernel': None, u'root_device_type': u'ebs', u'private_dns_name': u'ip-172-31-41-108.us-east-2.compute.internal', u'block_device_mapping': {u'/dev/sda1': {u'status': u'attached', u'delete_on_termination': True, u'volume_id': u'vol-06d37e8354c769d93'}}, u'key_name': u'crunchify', u'public_ip': u'3.19.60.48', u'image_id': u'ami-crunchify231di', u'tenancy': u'default', u'private_ip': u'172.31.41.108', u'groups': {u'sg-0eb80f388be5a7c35': u'crunchify_security_grp'}, u'public_dns_name': u'ec2-3-19-60-48.us-east-2.compute.amazonaws.com', u'state_code': 16, u'id': u'i-0e447dd1223a40f8e', u'tags': {}, u'placement': u'us-east-2c', u'ami_launch_index': u'0', u'dns_name': u'ec2-3-19-60-48.us-east-2.compute.amazonaws.com', u'region': u'us-east-2', u'ebs_optimized': False, u'launch_time': u'2019-05-10T18:48:18.000Z', u'instance_type': u't2.micro', u'state': u'running', u'architecture': u'x86_64', u'hypervisor': u'xen', u'virtualization_type': u'hvm', u'root_device_name': u'/dev/sda1'}) TASK [Add new instance to host group] ***************************************************************************************************************************************** changed: [localhost] => (item={u'ramdisk': None, u'kernel': None, u'root_device_type': u'ebs', u'private_dns_name': u'ip-172-31-41-108.us-east-2.compute.internal', u'block_device_mapping': {u'/dev/sda1': {u'status': u'attached', u'delete_on_termination': True, u'volume_id': u'vol-06d37e8354c769d93'}}, u'key_name': u'crunchify', u'public_ip': u'3.19.60.48', u'image_id': u'ami-crunchify231di', u'tenancy': u'default', u'private_ip': u'172.31.41.108', u'groups': {u'sg-0eb80f388be5a7c35': u'crunchify_security_grp'}, u'public_dns_name': u'ec2-3-19-60-48.us-east-2.compute.amazonaws.com', u'state_code': 16, u'id': u'i-0e447dd1223a40f8e', u'tags': {}, u'placement': u'us-east-2c', u'ami_launch_index': u'0', u'dns_name': u'ec2-3-19-60-48.us-east-2.compute.amazonaws.com', u'region': u'us-east-2', u'ebs_optimized': False, u'launch_time': u'2019-05-10T18:48:18.000Z', u'instance_type': u't2.micro', u'state': u'running', u'architecture': u'x86_64', u'hypervisor': u'xen', u'virtualization_type': u'hvm', u'root_device_name': u'/dev/sda1'}) TASK [Wait for SSH to come up] ************************************************************************************************************************************************ ok: [localhost -> localhost] => (item={u'ramdisk': None, u'kernel': None, u'root_device_type': u'ebs', u'private_dns_name': u'ip-172-31-41-108.us-east-2.compute.internal', u'block_device_mapping': {u'/dev/sda1': {u'status': u'attached', u'delete_on_termination': True, u'volume_id': u'vol-06d37e8354c769d93'}}, u'key_name': u'crunchify', u'public_ip': u'3.19.60.48', u'image_id': u'ami-crunchify231di', u'tenancy': u'default', u'private_ip': u'172.31.41.108', u'groups': {u'sg-0eb80f388be5a7c35': u'crunchify_security_grp'}, u'public_dns_name': u'ec2-3-19-60-48.us-east-2.compute.amazonaws.com', u'state_code': 16, u'id': u'i-0e447dd1223a40f8e', u'tags': {}, u'placement': u'us-east-2c', u'ami_launch_index': u'0', u'dns_name': u'ec2-3-19-60-48.us-east-2.compute.amazonaws.com', u'region': u'us-east-2', u'ebs_optimized': False, u'launch_time': u'2019-05-10T18:48:18.000Z', u'instance_type': u't2.micro', u'state': u'running', u'architecture': u'x86_64', u'hypervisor': u'xen', u'virtualization_type': u'hvm', u'root_device_name': u'/dev/sda1'}) TASK [Add tag to Instance(s)] ************************************************************************************************************************************************* changed: [localhost -> localhost] => (item={u'ramdisk': None, u'kernel': None, u'root_device_type': u'ebs', u'private_dns_name': u'ip-172-31-41-108.us-east-2.compute.internal', u'block_device_mapping': {u'/dev/sda1': {u'status': u'attached', u'delete_on_termination': True, u'volume_id': u'vol-06d37e8354c769d93'}}, u'key_name': u'crunchify', u'public_ip': u'3.19.60.48', u'image_id': u'ami-crunchify231di', u'tenancy': u'default', u'private_ip': u'172.31.41.108', u'groups': {u'sg-0eb80f388be5a7c35': u'crunchify_security_grp'}, u'public_dns_name': u'ec2-3-19-60-48.us-east-2.compute.amazonaws.com', u'state_code': 16, u'id': u'i-0e447dd1223a40f8e', u'tags': {}, u'placement': u'us-east-2c', u'ami_launch_index': u'0', u'dns_name': u'ec2-3-19-60-48.us-east-2.compute.amazonaws.com', u'region': u'us-east-2', u'ebs_optimized': False, u'launch_time': u'2019-05-10T18:48:18.000Z', u'instance_type': u't2.micro', u'state': u'running', u'architecture': u'x86_64', u'hypervisor': u'xen', u'virtualization_type': u'hvm', u'root_device_name': u'/dev/sda1'}) PLAY RECAP ******************************************************************************************************************************************************************** localhost : ok=6 changed=4 unreachable=0 failed=0
Let’s verify that new instance got created successfully with all our specifications
Go to Amazon AWS console
to check instance.
Link:
https://us-east-2.console.aws.amazon.com/ec2/v2/home?region=us-east-2#Instances:sort=instanceId
Make sure you verify all your settings.
Check your Tags. This is very helpful if you are dealing with hundreds of instances
.
Check crunchify.txt file which has newly created hosts’s IP:
bash3.2 $ cat crunchify.txt 18.217.28.189
That’s it. Congratulation. You have just created and started new EC2 instance on Amazon AWS cloud remotely using Ansible.
Let me know if you face any issue creating instance on Amazon EC2 cloud.