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 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. We have provided an IAM policy and Lambda function code, which is also available in the downloads section of the course.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create a Custom IAM Policy for an IAM Role
  1. Select the option to create a custom IAM policy from the Lambda console. It will open a new window to the IAM console.
  2. In the IAM console, click Create policy, select the JSON tab, and delete what is there.
  3. Copy the provided IAM policy from the Linux Academy downloads link, and paste it in the JSON tab.
  4. Back in the IAM console under Roles, search for the new policy we just created and add it to our role.
  5. The new custom IAM policy may take a few minutes to populate, but once it does, select the new policy and add it to the role.
  6. Add a role name, tags, and description.
  7. Click Create role.
Create a Basic Lambda Function
  1. In the Lambda console, click Create a function.
  2. Choose Author from scratch, choose Python 3.6 for the runtime, and select the new IAM role we just created in the previous task.
  3. Delete the provided JSON, and copy and paste the provided Lambda function code. (Remember to paste in your EC2 instance ID into the Lambda function code.)
Create a Test in the Lambda Console
  1. In the Lambda console, in the upper right-hand corner, click Test.
  2. Leave HelloWorld, add an event name, and click Create.
  3. Before running the test, go back to the EC2 console to verify that the EC2 instance is running.
  4. Back in the Lambda console, run the test. You will receive verification that the test was successful.
  5. Back in the EC2 console, verify that the basic Lambda function stopped the EC2 instance.

Additional Resources

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

The Lambda script and IAM policy are in the downloads section of the course. Please copy and paste the contents of each file into AWS as directed during the lab.

Alternatively, you may use the following links to download the files:

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.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


Sign In
Welcome Back!

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