Creating a Basic Lambda Function to Shut Down an EC2 Instance

30 minutes
  • 3 Learning Objectives

About this Hands-on Lab

In this hands-on lab, we will create a basic Lambda function to shut down an EC2 instance. We will start by creating a custom IAM policy for the IAM role. We will then create a basic Lambda function using the newly created IAM policy and role, along with the provided Lambda function code. To verfiy that the new Lambda function shuts down our EC2 instance, we will also run a test to make sure it is all working correctly. The provided IAM policy and Lambda function code can be accessed from the lab guide and in the Additional Information and Resources section of this lab.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create a Custom IAM Policy and Role for a Lambda Function
  1. From the AWS Management Console, navigate to IAM.
  2. Select the option to create a custom IAM policy for the Lambda service.
  3. In the IAM console, click Create policy.
  4. Select the JSON tab and delete the provided JSON.
  5. Copy the provided IAM policy from the GitHub repo or the lab guide and paste it in the JSON tab.
  6. In the IAM console under Roles, search for the newly created policy and add it to our role.

    Note: The IAM policy may take a few minutes to populate.

  7. Once populated, select the new policy and add it to the role.
  8. Add a role name, tags, and a description.
  9. Click Create role.
Create a Basic Lambda Function
  1. In the Lambda console, click Create a function.
  2. Select Author from scratch.
  3. For the runtime, select Python 3.6.
  4. Select the newly created IAM role and delete the provided JSON.
  5. Copy the provided Lambda function code from GitHub or the lab guide and paste it into the role.
  6. Paste in your EC2 instance ID into the Lambda function code.
Create a Test in the Lambda Console
  1. In the Lambda console under Code source, select the new function and click Test.
  2. Add an event name and click Create.
  3. In the EC2 console, verify the instance is running.
  4. Return to the Lambda console and run the test.
  5. Return to the EC2 console and verify that the basic Lambda function stopped the EC2 instance.

Additional Resources

Log in to the AWS Managment Console using the credentials provided. Make sure you are in the us-east-1 region.

Download the IAM policy and Lambda function GitHub files used in this lab.

Note: The Lambda Save button has been replaced with a Deploy button.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?