Skip to main content

Creating an EC2 Instance with Lambda

Hands-On Lab

 

Photo of Mark Richman

Mark Richman

AWS Training Architect II in Content

Length

00:30:00

Difficulty

Intermediate

In this AWS hands-on lab, we will write a Lambda function that will create an EC2 instance. This Lambda function will be written in Python using the Boto3 library. We will also create a custom Lambda execution policy. When we're done, we will be able to log in to the new EC2 instance via SSH.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Creating an EC2 Instance with Lambda

In this lab, we will write a Lambda function that will create an EC2 instance. This Lambda function will be written in Python using the Boto3 library. We will also create a custom Lambda execution policy. When we're done, you will be able to SSH into the new EC2 instance.

Log in to the AWS environment using the credentials provided on the lab page.

Create EC2 Key Pair

  1. Navigate to EC2.
  2. In the navigation pane, under NETWORK & SECURITY, choose Key Pairs.
  3. Choose Create Key Pair.
  4. Enter a name for the new key pair (e.g., "ec2") in the Key pair name field of the Create Key Pair dialog box, and then choose Create.
  5. Save the private key file in a safe place. Note: Make sure you remember the name of your private key file and save it somewhere easily accessible.

Create Lambda Function

  1. Navigate to AWS Lambda.
  2. Click Create a function.
  3. Choose Author from scratch and use the following settings:
    • Name: CreateEC2Instance
    • Runtime: Python 3.7
    • Role: Create a custom role
      • Role name: Leave as-is
      • Click View policy document.
        • Click Edit.
        • Paste in the policy from this file on GitHub.
        • Click Allow.
  4. Click Create function.
  5. On the CreateEC2Instance function page, scroll down to the Function code section.
  6. Paste in the Python source code from this file on GitHub.
  7. Scroll down to the Environment variables section.
  8. Set the following environment variables:
    • AMI
      • Key: AMI
      • Value: Open EC2 in a new browser tab, click Launch Instance, and copy and paste the ami value listed after Amazon Linux 2.
    • INSTANCE_TYPE
      • Key: INSTANCE_TYPE
      • Value: t2.micro
    • KEY_NAME
      • Key: KEY_NAME
      • Value: The name of the EC2 key pair you created earlier.
    • SUBNET_ID
      • Key: SUBNET_ID
      • Value: Navigate to VPC > Subnets, and copy and paste the ID of one of the public subnets in your VPC.
  9. Save the Lambda function.

Test Lambda Function

  1. Click Test.
  2. Define an empty test event. Its contents can simply be {}.
  3. Give it any name you like.
  4. Click Create.
  5. Click Test.
  6. Observe that an EC2 instance is initializing.

Connect to the Instance via SSH

  1. Open a terminal session, and use the following command to set the permissions of your private key file so that only you can read it.

    chmod 400 <KEY-PAIR>.pem

    Replace <KEY-PAIR> with your key pair name.

  2. Connect via the public IP of the EC2 instance.

    ssh -i <KEY-PAIR>.pem ec2-user@<IP ADDRESS>

    Remember to replace <IP ADDRESS> with the public IP of the EC2 instance you created.

Conclusion

Congratulations on completing this lab!