Linux | Cloud | DevOps | Scripting

Breaking

Wednesday, 12 June 2019

Create an EC2 instance in AWS using Vagrantfile


Resources:

Operating System                       : Windows (you can choose Linux or MAC as well)
Task Automation Framework   : PowerShell

Steps we need to follow:

  1. Install 'vagrant-aws' plugin
  2. Install the dummy box provided by Hashicorp
  3. Access and Secret Access keys
  4. Add a new security group in the EC2 'us-east-1' region called 'vagrant'
  5. Prepare the Vagrantfile
  6. Start and provision the vagrant environment
  7. Connect to the EC2 instance using Powershell

Step 1: Install 'vagrant-aws' plugin:
C:\> vagrant plugin install vagrant-aws

Sometimes it didn't work and provide an error. We need to check logs for this find logs at: 
C:\Users\<username>\.vagrant.d\gems\2.4.4\extensions\x64-mingw32\2.4.0\ovirt-engine-sdk-4.3.0\ mkmf.log


If we found this error, we need to run below-mentioned command first:
C:\> vagrant plugin install --plugin-version 1.0.1 fog-ovirt

Now run:
C:\> vagrant plugin install vagrant-aws

Step 2: Install the dummy box provided by Hashicorp:

C:\> vagrant box add dummy https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box

RESULT:- box: Successfully added box 'dummy' (v0) for 'aws'!

Step 3: Access and Secret Access keys:

If you don't remember this, you might generate new keys:

Login to AWS console ➔ Dropdown top right side under your account name ➔ My Security Credentials ➔ Create new access key ➔ Download key file ➔ Close.

This will download the key file in your PC. Copy them to a safe location OR copy to a piece of paper and delete the key from PC.

Step 4: Add a new security group in the EC2 'us-east-1' region called 'vagrant':

AWS ➔ EC2 ➔ Security Groups ➔ Create Security Group ➔ Name: vagrant ➔ Group description: Created for vagrant web server ➔ VPC: Select VPC in which you want to create this SG ➔ Inbound Rule ➔ Add Rule ➔ Type: SSH ➔ Source: Anywhere ➔ Add Rule ➔ Type: RDP ➔ Source: Anywhere ➔ Create.

Step 5: Prepare the Vagrantfile:

##### Start Script #####
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
 config.vm.box = "dummy"
 config.vm.provider :aws do |aws, override|
   aws.access_key_id = "AKIAJVBRFJIKE4LV5WFQ"
   aws.secret_access_key = "Cn9VPX1Af9LCbo55F3GS/+lgI1jBRtKVhOotd60y"
   aws.keypair_name = "vagrant-keypair"
   aws.ami = "ami-0756fbca465a59a30"
   aws.region = "us-east-1"
   aws.instance_type = "t2.micro"
   aws.security_groups = ['vagrant']
   config.vm.synced_folder '.', '/vagrant', disabled: true
   override.ssh.username = "ec2-user"
   override.ssh.private_key_path = "D:\\temp\\vagrant-keypair.pem"
 end
    config.vm.provision "shell", inline: <<-SHELL
        sudo yum -y install http
        sudo systemctl start httpd.service
sudo systemctl enable httpd.service
         wget https://raw.githubusercontent.com/cermegno/Ansible-test/master/web.py
  SHELL
end      
##### End Script #####


Step 6: Start and provision of the vagrant environment:

C:\> vagrant up
This command will create our instance in AWS. 


Step 7: Connect to ec2 instance using Powershell:

C:\> ssh -i "D:\temp\vagrant-keypair.pem" ec2-user@<public_IP OR DNS name>



Yaayyyy…Got Access!

But in some cases, there is git environment issue. So, first install git and then follow below mentioned commands:

$new_path = "$env:PATH;C:/Program Files/Git/usr/bin"
$env:PATH=$new_path
[Environment]::SetEnvironmentVariable("path", $new_path, "Machine")


NOTE:
Use 'vagrant halt' to stop and 'vagrant destroy' to terminate the instance.



1 comment:

  1. One of the most sustainable, high performance building materials prevalent throughout the world is zinc. As the need for environmentally sensitive, sustainable buildings, including homes, continues to grow, architectural zinc will become more prevalent as a durable AWS Certified Solutions Architect - Associate dumps, eco-friendly roofing material for new construction, renovation, and historic preservation.

    ReplyDelete

Pages