Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use cURL and mail in AWS Lambda

I run a Linux machine with 50+ hourly cron jobs of the format:

00 00 * * * /usr/bin/curl http://domain.com/page.php/reports | mail -s "domain.com report cron successful execution" [email protected]

The above cron:

  1. sends the output of curl command to pipe
  2. mail the output to [email protected] with subject line "domain.com report cron successful execution"

How can I set these cron to use with AWS Lambda, as I do not want a separate server just for these cron jobs.

If this is possible, I can setup AWS SES to work with this.

like image 502
Varun Chandak Avatar asked Feb 03 '17 11:02

Varun Chandak


People also ask

Can we send email from AWS Lambda?

To send email from a Lambda function using Amazon SES, do the following: 1. Create an AWS Identity and Access Management (IAM) policy and execution role for Lambda to run the API call.

Can we do multithreading in Lambda?

Using multithreading in AWS Lambda can speed up your Lambda execution and reduce cost as Lambda charges in 100 ms unit.


1 Answers

For scheduling the CRON, you should use CloudWatch Events. You can set a CRON expression that will trigger the CloudWatch Event. This event can in turn trigger a Lambda function.

Your Lambda function can make HTTP calls using the oh so great requests package. Instructions for deployment on Lambda here. You can also use the not so easy to use urllib2 module as an alternative to requests if you don't want to package requests with your Lambda function.

As for sending emails, AWS SES would be the easiest avenue. You can alternatively use the email module, though I've never used it in the context of a Lambda function.

like image 72
spg Avatar answered Nov 10 '22 18:11

spg