I was going to just have one blog post about setting up a Backup and Cleanup Python scripts for snapshotting EBS volumes, but as I started thinking about this everything is always about cost, so I wanted to explain how the pricing works in Lambda and compare it to EC2. Below I will explain the cost of running three example administrative tasks in EC2 vs Lambda. My next post will go into the details of setting up a backup and cleanup job for EBS volume snapshots in Lambda.
Traditionally in AWS if you had administrative tasks that needed to be run, such as cleaning up unmounted EBS volumes, tag auditing for resources, and backing up EBS volumes, you would spin up a micro Linux instance and run your scripts in cron to perform these tasks. Running this micro Linux instance would cost you between $6.28 - $9.49 per month depending on if instance is reserved (All Upfront & 1-Year Term) or on-demand.
To take a look at how much these jobs cost to run per month on this micro instance, in our example we will be running the three administrative task jobs from above once a day. So lets be conservative and say each job is taking up to a minute to run daily in a larger environment with many resources to perform actions on. In a month for all three jobs that is 5,400 seconds/month which is 90 minutes but in AWS EC2 pricing we will round it up to two hours. So if your instance was reserved at fully upfront and a 1-Year term you would be wasting 728 hours a month of compute time or $6.26 (effective hourly ($0.0086) x 2 hours equals $0.02 (rounded up to make easier) subtracted from monthly cost of $6.28). So this equates to an inefficiency of 99.73% for this EC2 instance’s utilized compute time.
Now I know the cost of running this EC2 instance for some environments is small potatoes compared to other computing resources running in some environments, but the concepts discussed here possibly can be applied to your larger, more expensive computing resources. Let’s now talk about AWS’s Lambda service, and how it can increase the efficiency of these administrative tasks.
AWS Lambda is a service that allows you to run code without provisioning or managing any servers. You just upload your code and AWS takes care of the rest, currently as of this writing Java 8, Node.js 0.10 & 4.3, and Python 2.7 are the only supported languages. The code is run when its triggered from another AWS service or is called via HTTP. In our example we will using a CloudWatch scheduled event to trigger the running of our Python code once a day again.
Lambda has a indefinitely free tier even after your initial free 12 months tier from Amazon which includes 1 Million free requests and 400,000 GB-seconds of compute time per month.
You might be asking your self, “What is a GB-second?” Well to compute a GB-second you first you calculate how many times a month the jobs will run, so if we are sticking with our example above, we are running three jobs once a day so in a month so 90 times a month. Next we know each job is taking 60 seconds to complete, so we multiply 60 90 and we are back to our 5,400 seconds of compute time a month for these jobs. So we take our 5,400 seconds of compute time a month and multiply it by the allocated memory for our code to run in Lambda. Since our code is not doing anything memory intensive, we will choose the lowest memory that can be allocated in Lambda of 128MB. So 5400 128MB/1024 (Divide by 1024 to convert to a GB) equals 675 GB-seconds. Now for our calculation we will omit the Free Lambda tier compute because we want to compare an actual monetary value of Lambda against our EC2 way of running jobs. We will take our 675 and multiply it by the 1024MB Lambda rate converted to per second instead of per 100ms ($0.000001667 * 10) of $0.00001667 which comes out to $0.01125225 or we will round up to $0.02 to keep our decimal places easy and comparable to above. We are omitting the cost per request because its so small but it is $0.0000002 per request, so our 90 requests per month would be $0.000018.
So you see our cost for computing these jobs per month in Lambda is in line with the cost of $0.02 of utilized compute time in EC2 but without paying for all those other unused compute hours in EC2 or purchasing a reserved instance. If we were comparing to an on-demand instance we would be saving $9.45 per month.
In my next Blog post I will discuss setting up two Python scripts in Lambda to create and cleanup snapshots of EBS volumes.