Create a Basic AWS Lambda Function

Hands-On Lab

 

Photo of Tia  Williams

Tia Williams

AWS Training Architect II in Content

Length

00:30:00

Difficulty

Beginner

Let's go Serverless! In this learning activity we will create a basic Lambda function that will shut down an EC2 instance.

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.

Create a Basic AWS Lambda Function

Introduction

In this lab, we'll create a basic Lambda function that will shut down an EC2 instance.

Log in to the AWS console with the cloud_user credentials provided on the lab page, and make sure you are in the us-east-1 region.

Create a Lambda Function

  1. Navigate to EC2.

  2. Click the running instances, and then copy the ID of the listed running instance. Paste it into a note or text file on your desktop for easy access — we'll need it soon.

  3. Navigate to Lambda via the Services menu at the top.

  4. Click Create a function.

  5. Make sure the Author from scratch option at the top is selected, and then use the following settings:

    • Name: EC2shutdown
    • Runtime: Python 3.6
    • Role: Create a custom role
  6. In the role creation page that opens, expand View Policy Document, click Edit, and click OK.

  7. Delete the entire existing policy document, and then paste in the following:

    {
     "Version": "2012-10-17",
     "Statement": [
       {
         "Effect": "Allow",
         "Action": [
           "logs:CreateLogGroup",
           "logs:CreateLogStream",
           "logs:PutLogEvents"
         ],
         "Resource": "arn:aws:logs:*:*:*"
       },
       {
         "Effect": "Allow",
         "Action": [
           "ec2:Stop*"
         ],
         "Resource": "*"
       }
     ]
    }
  8. Click Allow.

  9. Back on the Create function page, click Create function.

  10. On the EC2Shutdown page, scroll to the Function code section.

  11. Delete the existing code there, and enter the following:

    import boto3
    #This simple lambda function is available from AWS with instructions on starting and stopping an instance at regular intervals using Lambda and CloudWatch: https://aws.amazon.com/premiumsupport/knowledge-center/start-stop-lambda-cloudwatch/
    # Enter the region your instances are in. Include only the region without specifying Availability Zone; e.g., 'us-east-1'
    region = 'us-east-1'
    # Enter your instances here: ex. ['X-XXXXXXXX'] you can comma separate the instance IDs for more than one instance: i.e. ['X-XXXXXXXXX', 'X-XXXXXXXXX"]
    instances = ['X-XXXXXXXX']
    
    def lambda_handler(event, context):
       ec2 = boto3.client('ec2', region_name=region)
       ec2.stop_instances(InstanceIds=instances)
  12. Replace the X's in the instances = ['X-XXXXXXXX'] line with the instance ID you copied a few minutes ago — but make sure you leave the quote marks on either side.

  13. Click Save.

Test the Lambda Function and Verify It Stops the EC2 Instance

  1. Open the EC2 dashboard in a new browser tab, and check that the instance is still running, which it should be.
  2. Back in the EC2Shutdown page, click Test.
  3. On the Configure test event page, make sure Create new test event is selected.
  4. Give it an Event name of "TestEvent", and click Create.
  5. Click the Test button again.
  6. Head to the instances dashboard tab, and refresh the table. Now, the instance should be stopped.

Conclusion

Congratulations on completing this lab!