Using AWS command-line tools for EC2 VM creation

From Notes_Wiki

Home > Amazon web services > Using AWS command-line tools for EC2 VM creation

Creating new VM with desired AMI

To create new VM with desired AMI use:

aws ec2 run-instances --count <no-of-instance> --image-id <ami-id> --key-name <key-pair> --security-groups <security-group> --instance-type <type> --ebs-optimized  > info1.txt

where --ebs-optimized can be ommitted if ebs optimization is not desired. The output will go to file info1.txt where it can be parsed for various values.

For example:

aws ec2 run-instances --count 1 --image-id ami-6aad335a --key-name saurabh-amazon --security-groups saurabh-script-test --instance-type m1.large --ebs-optimized  > info1.txt

Then instance ID can be obtained using:

INSTANCE_ID=$(cat info1.txt | grep -i instanceid | sed 's/ *"InstanceId": "//' | sed 's/",//')


Setting VM root disk image size

To set VM root device size, use following steps:

  1. Use 'aws ec2 describe-images --image-ids <AMI-ID>' to get block device mapping for given AMI
  2. Define variables to specify disk size and perhaps IOPS:
    VM_DISK_SIZE=13
    IOPS=200
  3. Finally define mappings in variable using:
    BLOCK_DEVICE_MAPPINGS="[{\"DeviceName\":\"/dev/sda1\",\"Ebs\":{\"VolumeSize\":$VM_DISK_SIZE,\"VolumeType\":\"io1\",\"DeleteOnTermination\":true,\"SnapshotId\":\"snap-214b83d6\",\"Iops\":$IOPS}}]"
  4. Then add to command option
    --block-device-mappings "$BLOCK_DEVICE_MAPPINGS"


Assigning name and other tags to instances

After instance is created we should at least assign name to it using:

aws ec2 create-tags --resources $INSTANCE_ID --tags Key=Name,Value=$HOST
aws ec2 create-tags --resources $INSTANCE_ID --tags Key=Env,Value=saurabh-script-test


Getting details of VM with instance ID

After waiting for a minute or two details of instance can be obtained using:

aws ec2 describe-instances --instance-id $INSTANCE_ID > info2.txt


Obtaining public DNS name of instance

To obtain public DNS name of instance use:

aws ec2 describe-instances --instance-id $INSTANCE_ID > info2.txt
PUBLIC_DNS_NAME=$(cat info2.txt | grep -i PublicDNSName | head -1 | sed 's/ *"PublicDnsName": "\([^"]*\)",.*/\1/')


Performing SSH to instance with key-pair and accepting SSH fingerprint

Before rsync can be used to copy files or before running commands on instance using SSH we should add key-pair and accept SSH fingerprint using:

ssh-add ~/saurabh-amazon.pem
ssh -o StrictHostKeyChecking=no ubuntu@$PUBLIC_DNS_NAME 'ls'

Note that accepting SSH fingerprint in this manner is susceptible to MITM attacks. Also in case of Cent-OS VMs the username should be changed from ubuntu to ec2-user.


Copying files to instance using rsync and executing scripts on VM using ssh

After adding ssh-key and accepting SSH fingerprint files can be copied to instance using something similar to:

rsync -vaHz ~/ec2_files/ ubuntu@$PUBLIC_DNS_NAME:files/

and scripts can be executed using something similar to:

ssh -o StrictHostKeyChecking=no ubuntu@$PUBLIC_DNS_NAME "cd ~/files;./setup.sh $INSTANCE_ID $PUBLIC_DNS_NAME > output.txt 2>&1 &"


You may also want to add / replace DNS entries to point to new VM using Managing Route53 zones using command-line


Home > Amazon web services > Using AWS command-line tools for EC2 VM creation