How do I automate tasks in my AWS account with Lambda?

5 minutos de lectura
0

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. For information on what AWS services you can integrate with Lambda, see Using Lambda with other services.

This article shows examples for two of the most common AWS services that use Lambda for automating tasks:

For more examples, see Using Lambda with scheduled events and Using Lambda with Amazon S3.

Resolution

Important: EventBridge and Amazon S3 automatically update your Lambda function's execution role, adding required access using resource-based policies. Not all AWS services do this. If you're integrating Lambda with another AWS service, make sure that you add the required permissions manually.

Create a Lambda function that logs input into your Amazon CloudWatch Logs

Note: The function you create will be the target of the events that you configure. Replace the example function code with your own code for the task that you want to automate in your use case.

1.    In the Lambda console, choose Create function. The Create function page opens to the default Author from scratch option.

2.    Under Basic information, enter the following:
For Function name, enter a name for your function.
For Runtime, choose Node.js 14.x.

3.    Under Permissions, choose Change default execution role. 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. Then, enter a name and choose a policy template.

4.    Choose Create function.

5.    On the Configuration pane, under Function code, open up the index.js file. Then, copy and paste the following example function code into the editor pane:

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

6.    Choose Deploy.

For more information, see Create a Lambda function with the console. You can also create a Lambda function by building and uploading your own deployment package or by creating and uploading a container image.

(For EventBridge) Create EventBridge rules that trigger on a schedule or in response to an event

For scheduled events

To automate tasks with specific timing and without any input, follow the instructions in Creating an EventBridge rule that triggers on a schedule. Make sure that you specify a schedule for 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.

Note: 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.

For an example, see Schedule Lambda functions using EventBridge.

For service events

To automate tasks in response to an event generated by an AWS service, follow the instructions in Creating a rule for an AWS service.

For this example setup, use the following configurations when you create the rule:
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: After you create the rule, your Lambda function is invoked for each occurrence of the event pattern that you defined.

For more information, see Event patterns in EventBridge and EventBridge event examples from supported AWS services.

To test the EventBridge rule

To test the EventBridge rule, cause a state change in an Amazon Elastic Compute Cloud (Amazon EC2) instance by stopping or starting the instance. Lambda will send a stream of logs to CloudWatch.

For information on how to launch an EC2 instance, see Launch an instance.

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

For more information, see Getting Started with Amazon EventBridge.

(For Amazon S3) Configure an S3 Event Notification to trigger your Lambda function

To use Amazon S3 Event Notifications to trigger your Lambda function, follow the instructions in Enabling and configuring event notifications.

For this example setup, use the following configurations when you create the S3 Event Notification:
For Event types, choose the All object create events check box.
For Destination, choose Lambda function.
In the Lambda function dropdown list, choose the Lambda function that you created earlier.

For information on how to create an S3 bucket, see Create your first S3 bucket.

To test the Amazon S3 Event Notification

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 CloudWatch logs contain metadata from the event object, such as the S3 bucket name and the object name.

For an example, see Using Lambda with Amazon S3.


Related information

How can I get customized email notifications when my EC2 instance changes states?

OFICIAL DE AWS
OFICIAL DE AWSActualizada hace 3 años