Difference between revisions of "Using AWS command-line tools for EC2 VM creation"

From Notes_Wiki
(Created page with "<yambe:breadcrumb>Amazon_web_services|Amazon web services</yambe:breadcrumb> =Using AWS command-line tools for EC2 VM creation= ==Creating new VM with desired AMI== To creat...")
 
m
Line 56: Line 56:
ssh -o StrictHostKeyChecking=no ubuntu@$PUBLIC_DNS_NAME 'ls'
ssh -o StrictHostKeyChecking=no ubuntu@$PUBLIC_DNS_NAME 'ls'
</pre>
</pre>
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.  After this files can be copied to instance using something similar to:
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:
<pre>
<pre>
rsync -vaHz ~/ec2_files/ ubuntu@$PUBLIC_DNS_NAME:files/
rsync -vaHz ~/ec2_files/ ubuntu@$PUBLIC_DNS_NAME:files/

Revision as of 07:33, 5 December 2013

<yambe:breadcrumb>Amazon_web_services|Amazon web services</yambe:breadcrumb>

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/",//')


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 $INSTANCEID --tags Key=Name,Value=$HOST
aws ec2 create-tags --resources $INSTANCEID --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 | 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


<yambe:breadcrumb>Amazon_web_services|Amazon web services</yambe:breadcrumb>