AWS Lambda
Overview
AWS Lambda is a compute service that lets you run code without provisioning or managing servers. AWS Lambda executes your code only when needed and scales automatically, from a few requests per day to thousands per second. You pay only for the compute time you consume - there is no charge when your code is not running. With AWS Lambda, you can run code for virtually any type of application or backend service - all with zero administration.
When Should I Use AWS Lambda ?
AWS Lambda is an ideal compute platform for many application scenarios, provided that you can write your application code in languages supported by AWS Lambda (that is, Node.js, Java, Go and C# and Python), and run within the AWS Lambda standard runtime environment and resources provided by Lambda.
Use Case
Schedule adhoc jobs on different RDBMS on AWS RDS without standalone EC2 for cronjobs. E.g. report DBsize utilization once a week, execute partition purge . Oracle RDS provides dbms_scheduler procedure to schedule job but not all RDBMS have the flexibility . Also notify in case of failure of jobs .
How to remove need of EC2 to schedule adhoc jobs using Lambda Function
- Prepare the environment and create deployment package . https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html and https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html#deployment-pkg-for-virtualenv ( For python )
- Create lambda function and associate with a role . Create a role if it doesn't exists
- E.g. Create a role lambda_basic_execution through IAM and attach policy ( e.g. RDS access etc )
- Upload the deployment package zip file ( including your python code and supported libraries )
- Now test to run the function.
- Create Cloudwatch Rule to schedule the execution of the function and create cloud alarms for any failure.
Challenges
- Most of the database jobs has username and password. Ensure the password is encrypted using KMS Key.
- Ensure proper subnet is assigned to the function else the execution of encryption and decryption of the password will time out.
Benefit
- Removed the dependency on EC2 and its maintenance.
- Charged per execution of the function instead of 24X7 EC2 instance