How do I automate tasks with Lambda?

Last updated: 2019-10-14

I want to use AWS Lambda to automate tasks in my AWS account. How do I set that up?

Short Description

There are multiple AWS services that you can integrate with Lambda to invoke a function on a schedule or in response to certain events. Amazon CloudWatch Events and Amazon Simple Storage Service (Amazon S3) event notifications are some of the most commonly used for automating tasks. This article provides examples for both methods that you can use as a guideline for your use case.

For more examples, see Tutorial: Using AWS Lambda with Scheduled Events and Tutorial: Using AWS Lambda with Amazon S3.

Note: CloudWatch and Amazon S3 can automatically update your Lambda function's execution role, adding required access using resource-based policies. However, not all AWS services do this automatically, and other processes can also change your function's execution role and stop it from invoking correctly. In those cases, add permissions manually for the service that you're integrating with Lambda.


Create a Lambda function

Follow these example steps to create a simple function that logs input to Amazon CloudWatch Logs. This function will be the target of the events that you configure. You can replace the example function code with your own code for the task that you want to automate in your use case.

Note: You can also create a Lambda function by building and uploading your own deployment package.

1.    Open the Lambda console.

2.    If you're new to Lambda, choose Get Started Now. If not, choose Create function. The Create function page opens to the Author from scratch option.

3.    With Author from scratch selected, under Basic information, do the following:
For Function name, enter a name.
For Runtime, choose Node.js 10.x.

4.    Under Permissions, expand Choose or create an execution role, and then do one of the following:
If you're new to Lambda, choose Create a new role with basic Lambda permissions.
If you've already created a Lambda execution role that you want to use, choose Use an existing role.
If you want to create a new execution role using an AWS managed policy template, choose Create a new role from AWS policy templates, enter a name, and then choose a policy template.

5.    Choose Create function.

6.    On the Configuration pane, under Function code, paste this example function code into the editor pane:

'use strict';
exports.handler = (event, context, callback) => {
    console.log('Received event:', JSON.stringify(event, null, 2));
    callback(null, 'Finished');

7.    Choose Save.

For more information, see Create a Lambda Function with the Console.

Create CloudWatch Events rules

Use CloudWatch Events to create rules that trigger on a schedule or that trigger in response to an event. For more information, see Getting Started with Amazon CloudWatch Events.

Scheduled events

To do things with specific timing and without any input, follow the instructions in Creating a CloudWatch Events Rule That Triggers on a Schedule. Specify a schedule for how often or when you want your automated task to run. Add the Lambda function that you created as a target to trigger in response to the event.

After you create the rule, your Lambda function is invoked automatically with the timing that you defined. If you used the example function code, a stream of logs from Lambda populates in CloudWatch on schedule.

Note: For another example, see Tutorial: Schedule AWS Lambda Functions Using CloudWatch Events.

Service events

To do things in response to an event generated by an AWS service, follow the instructions in Creating a CloudWatch Events Rule That Triggers on an Event. For this example setup, for Service Name, choose EC2. For Event Type, choose EC2 Instance State-change Notification. Add the Lambda function that you created as a target.

Note: For more information, see Event Patterns in CloudWatch Events and CloudWatch Events Event Examples From Supported Services.

After you create the rule, your Lambda function is invoked for each occurrence of the event pattern that you defined.

For this example, you can test the rule by causing a state change in an Amazon Elastic Compute Cloud (Amazon EC2) instance. If you haven't already done so, launch an EC2 instance. When you create the instance, and when you stop or start the instance, Lambda sends a stream of logs to CloudWatch.

Note: An EC2 instance can incur charges on your AWS account. If you create an instance only for this example, make sure to terminate the instance when you're done to prevent recurring charges.

Configure an Amazon S3 event notification

Use Amazon S3 event notifications to trigger your Lambda function whenever the supported Amazon S3 event type that you specify occurs in your S3 bucket. For more information and an example event, see Using AWS Lambda with Amazon S3.

If you haven't already done so, create an S3 bucket. Then, follow the instructions in Enabling and Configuring Event Notifications. For this example, for Events, select the All object create events check box. For Send to, choose Lambda Function. For Lambda, choose the Lambda function that you created.

To test the setup, upload an object to the S3 bucket. If you configured a Prefix or Suffix filter, make sure that the object has the correct prefix or suffix. When uploading is complete, your Lambda function invokes. If you used the example function code, a stream of logs from Lambda populates in CloudWatch. These logs contain metadata from the event object, such as the S3 bucket name and the object name.

Did this article help you?

Anything we could improve?

Need more help?