Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Amazon EC2 Cookbook

You're reading from   Amazon EC2 Cookbook Over 40 hands-on recipes to develop and deploy real-world applications using Amazon EC2

Arrow left icon
Product type Paperback
Published in Nov 2015
Publisher Packt
ISBN-13 9781785280047
Length 194 pages
Edition 1st Edition
Arrow right icon
Toc

Table of Contents (10) Chapters Close

Preface 1. Selecting and Configuring Amazon EC2 Instances FREE CHAPTER 2. Configuring and Securing a Virtual Private Cloud 3. Managing AWS Resources Using AWS CloudFormation 4. Securing Access to Amazon EC2 Instances 5. Monitoring Amazon EC2 Instances 6. Using AWS Data Services 7. Accessing Other AWS Services 8. Deploying AWS Applications Index

Launching EC2 instances using EC2-Classic and EC2-VPC

Your EC2 instance receives a private IP address from the EC2-Classic range each time it's started, whereas your instance receives a static private IP address from the address range in EC2-VPC. You can only have one private IP address in EC2-Classic, but in EC2-VPC, we have multiple private IP addresses. If you attach an EIP (Elastic IP) to EC2-Classic instance, it will get dissociated when you stop the instance. But for VPC EC2 instance, it remains associated even after you stop it. We can create subnets, routing tables, and Internet gateways in VPC. For on-premise connectivity, we need VPC.

Note

There are different VPC options available, depending on whether you created your AWS account before or after 2013-12-04.

If you created your AWS account after 2013-12-04, then only EC2-VPC is supported. In this case, a default VPC is created in each AWS region. Therefore, unless you create your own VPC and specify it when you launch an instance, your instances are launched in your default VPC.

If you created your AWS account before 2013-03-18, then both EC2-Classic and EC2-VPC are supported in the regions you used before, and only EC2-VPC in regions that you didn't use. In this case, a default VPC is created in each region in which you haven't created any AWS resources. Therefore, unless you create your own VPC and specify it when you launch an instance in a region (that you haven't used before), the instance is launched in your default VPC for that region. However, if you launch an instance in a region that you've used before, the instance is launched in EC2-Classic.

In this recipe, we will launch EC2 instances using EC2-Classic and EC2-VPC.

Getting started…

Before we launch the EC2 instances, we need the image ID.

Run the following command to get the list of images. We can apply the filter to identify a specific image. Record the image ID for later use:

$ aws ec2 describe-images
--filter [Filter] 

Note

You can specify one or more filters in this command.

By executing the following command, you obtain the image ID of a 64-bit version of Ubuntu 12.04 image:

$ aws ec2 describe-images
--filter
"Name=virtualization-type,Values=paravirtual"
"Name=root-device-type,Values=ebs" "Name=architecture,Values=x86_64"
"Name=name,Values=ubuntu/images/ebs/ubuntu-precise-12.04-amd64-server-20130204"

How to do it…

We will see the EC2 instances being launched, one by one:

Launching the EC2 instance in EC2-Classic

Using the following command, we can launch instances in EC2-Classic. You can specify the number of instances to launch using the count parameter.

$ aws ec2 run-instances 
--image-id [ImageId] 
--count [InstanceCount] 
--instance-type [InstanceType] 
--key-name [KeyPairName] 
--security-group-ids [SecurityGroupIds]

The parameters used in this command are described as follows:

  • [ImageId]: This is the ID of the image
  • [InstanceCount]: This gives number of instances to be created
  • [InstanceType]: This gives the type of EC2 instance
  • [KeyPairName]: This parameter provides the key/pair name for authentication
  • [SecurityGroupIds]: This one provides security group IDs

The following command will create a micro instance in EC2-Classic (in the Singapore region):

$ aws ec2 run-instances 
--image-id ami-7e2c612c 
--count 1 
--instance-type t1.micro 
--key-name WebServerKeyPair 
--security-group-ids sg-ad70b8c9

Launching the EC2 instance in VPC

Run the following command to launch instances in EC2-VPC. We need to specify the subnet ID while creating an instance in EC2-VPC. Before creating the instance in EC2-VPC, you have to create the VPC and subnets inside it.

$ aws ec2 run-instances 
--image-id [ImageId] 
--count [InstanceCount] 
--instance-type [InstanceType] 
--key-name [KeyPairName] 
--security-group-ids [SecurityGroupIds]
--subnet-id [SubnetId]

Here, SubnetId specifies the subnet where you want to launch your instance.

Next, run the following command to create a micro instance in EC2-VPC (in the Singapore region):

$ aws ec2 run-instances 
--image-id ami-7e2c612c 
--count 1 
--instance-type t1.micro 
--key-name WebServerKeyPair
--security-group-ids sg-ad70b8c8 
--subnet-id subnet-aed11acb

See also

  • The Configuring security groups and Creating an EC2 key pair recipes
You have been reading a chapter from
Amazon EC2 Cookbook
Published in: Nov 2015
Publisher: Packt
ISBN-13: 9781785280047
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime