How do I launch an EC2 instance in VPC from the command line?

Before launching the instance, we need to configure several parameters, and note their values:

Look up the allocation ID of the new Elastic IP address.

Look up the subnet-id of the subnet you want to use (subnet-xxxxxxxx):

root@puppet:~# aws ec2 describe-subnets --output=text  --filters="Name=cidrBlock,Values=172.16.1.0/24"
SUBNETS	us-west-2a	249	172.16.1.0/24	False	False	available	subnet-00000000	vpc-00000000
TAGS	Name	172.16.1.0 Webserver Subnet
root@puppet:~# 

Look up the security group ID (sg-xxxxxxxx):

root@puppet:~# aws ec2 describe-security-groups --output=text --filters="Name=group-name,Values=Webserver Rules"
SECURITYGROUPS	Webserver Rules	sg-00000000	Web server	000000000000	vpc-00000000
IPPERMISSIONS	22	tcp	22
IPRANGES	0.0.0.0/0
IPPERMISSIONS	11	icmp	0
IPRANGES	0.0.0.0/0
IPPERMISSIONS	80	tcp	80
IPRANGES	0.0.0.0/0
IPPERMISSIONS	0	icmp	-1
IPRANGES	0.0.0.0/0
IPPERMISSIONS	8	icmp	-1
IPRANGES	0.0.0.0/0
IPPERMISSIONSEGRESS	-1
IPRANGES	0.0.0.0/0
TAGS	Name	Webserver Rules
root@puppet:~# 
root@puppet:~# aws ec2 run-instances \
>         --image-id ami-6cc2a85c \
>         --key mysshkey \
>         --security-group-ids sg-00000000 \
>         --instance-type t2.small \
>         --subnet-id subnet-00000000 \
>         --private-ip-address 172.16.1.5 \
>         --output=text
000000000000	r-00000000
INSTANCES	0	x86_64	None	False	xen	ami-6cc2a85c	i-00000000	t2.small	vmass	2014-01-01T22:10:41.000Z	ip-172-16-1-5.us-west-2.compute.internal	172.16.1.5	None	/dev/sda1	ebs	True	None	subnet-00000000	hvm	vpc-00000000
MONITORING	disabled
NETWORKINTERFACES	None	eni-e7007290	000000000000	172.16.1.5	True	in-use	subnet-00000000	vpc-00000000
ATTACHMENT	2014-01-01T22:10:41.000Z	eni-attach-00000000	True	0	attaching
GROUPS	sg-00000000	Webserver Rules
PRIVATEIPADDRESSES	True	172.16.1.5
PLACEMENT	us-west-2a	None	default
SECURITYGROUPS	sg-00000000	Webserver Rules
STATE	0	pending
STATEREASON	pending	pending
root@puppet:~# 

Associate the new instance with the Elastic IP you allocated:

root@puppet:~# aws ec2 associate-address --instance-id=i-00000000 --allocation-id eipalloc-00000000
{
    "AssociationId": "eipassoc-00000000", 
    "return": "true"
}
root@puppet:~# 

Tag it:

root@puppet:~# aws ec2 create-tags --resources i-00000000 --tags "Key=Name,Value=web1.mysite.com"
{
    "return": "true"
}
root@puppet:~# 

Add a DNS record to Route53. First, create a JSON file with the update instructions:

{
  "Comment": "adding web1.mysite.com",
  "Changes": [
    {
      "Action": "CREATE",
      "ResourceRecordSet": {
        "Name": "web1.mysite.com",
        "Type": "A",
        "TTL": 300,
        "ResourceRecords": [
          {
            "Value": "123.45.67.89"
          }
        ]
      }
    }
  ]
}

Now send the JSON request:

root@puppet:~# aws route53 change-resource-record-sets --hosted-zone-id Z3XXXXXXXXXXXX         --change-batch file://route53-command.json

Add an entry to your ~/.ssh/config to tell SSH where to find the private key to login to the new instance:

Host    web1.mysite.com
        User ubuntu
        IdentityFile ~/.ssh/mysshkey.pem 
Share: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Twitter
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • Reddit
  • StumbleUpon

Leave a Reply

Your email address will not be published. Required fields are marked *