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

Creating an EC2 key pair

AWS can authenticate using the public-private key mechanism. The recommended authentication mechanism is public-private key authentication instead of passwords to remotely log in to your instances with SSH. We upload the public key to AWS, and store the private key on our local machine. If anyone has your private key, then they can easily log in to your EC2 instances. It's a best practice to store these private keys in a secure place. We can create the public and private key from our machine using tools like PuTTY Key Generator.

You should include a passphrase with the private key to prevent unauthorized persons from logging in to your EC2 instance. When you include a passphrase, you have to enter the passphrase whenever you log in to the EC2 instance. A passphrase on a private key is an extra layer of protection. If you lost your private key for an EBS-backed instance, you can regain access to your instance by executing the following steps:

  1. Stop the EBS-backed EC2 instance.
  2. Detach the root volume from EC2 instance.
  3. Launch the new EC2 instance for recovery.
  4. Attach the EC2 root volume as data volume to the previously created instance.
  5. Modify the authorized_keys file.
  6. Detach the root volume from recovery instance.
  7. Attach the root volume back to the EC2 instance.
  8. Start the instance.

How to do it…

Here, we list the commands to create a key pair and then launching the EC2 instance (using the key pair).

Creating a key pair

Use the following steps to create a key pair:

  1. Run the following command to create the key pair.

    You have to provide the key pair name. You can explicitly specify the text output for this command using the –output argument for easy cut and paste.

    $ aws ec2 create-key-pair 
    --key-name [KeyPairName]
    

    Note

    The [KeyPairName] parameter in this command is used to specify a name for the key pair.

  2. After executing the create-key-pair command, copy the entire output key into file including the following lines:
    ----BEGIN RSA PRIVATE KEY----
    -----END RSA PRIVATE KEY-----
  3. Save the file with ASCII encoding.
  4. Run the following command to create the key pair with name WebServerKeyPair.
    $ aws ec2 create-key-pair 
    --key-name WebServerKeyPair
    
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