Utilizing Managed Services and Serverless Architectures to Minimize Cost
Amazon API Gateway
Amazon Elastic Map Reduce
Design a Multi-Tier Solution
When To Go Serverless
Which services should I use to build a decoupled architecture?
The course is part of this learning path
This section of the AWS Certified Solutions Architect - Professional learning path introduces common AWS solution architectures relevant to the AWS Certified Solutions Architect - Professional exam and the services that support them. These services form a core component of running resilient and performant architectures.
- Learn how to utilize managed services and serverless architectures to minimize cost
- Understand how to use AWS services to process streaming data
- Discover AWS services that support mobile app development
- Understand when to utilize serverless services within your AWS solutions
- Learn which AWS services to use when building a decoupled architecture
Now before I wrap things up, I want to illustrate by way of example just how much your costs can vary between an EC2 and a serverless architecture depending on your application’s performance requirements. And the idea here is just to show that you can’t necessarily assume a serverless architecture will always be less expensive than one that leverages EC2 instances, or vice versa. Now of course, costs alone shouldn’t dictate whether or not a serverless implementation is more appropriate for your use case. Just know that one option may be more expensive than another, and it’s always worth running the numbers to see just how much these costs may differ. So for these examples I’m going to be using the AWS Pricing Calculator to estimate my costs, which is available at https://calculator.aws. And I would encourage you to do the same for your use cases as well. Now obviously, AWS pricing does change over time, but the prices I’m showing you here were current at the time of this recording.
So let’s start by comparing a workload we want to deploy as a Lambda function with unpredictable access patterns, but we know it gets executed a total of 1 million times per month. And let’s say this function requires 1 gigabyte of memory and 1 gigabyte of ephemeral storage. And let’s say it takes a total of 500 milliseconds to execute each time. So using the AWS pricing calculator, we can see that when operating outside of the free tier, this function will cost us $8.54 per month.
Now let’s say we want to deploy this workload on an EC2 instance instead. And let’s assume we can use a relatively modest t4g.small instance with a single 20 gigabyte EBS volume. With just the one instance, and using an EC2 Instance Savings Plan, this architecture will cost us $9.67 per month, which is pretty close to what we’d be paying to use Lambda.
Of course, if we want to achieve high availability, we would need to add a second EC2 instance, which doubles our total cost to just over $19 per month. And keep in mind this still doesn’t include any kind of load balancing, or any additional costs you might incur during prolonged spikes in demand since we are using a burstable instance type here.
So that was an example where a serverless approach saves a little money. But what if we massively scale up the demand? Let’s say instead of 1 million requests per month, we now have 100 million requests per month. Well since our Lambda function is billed based on usage, and we’ve increased our usage by a factor of 100, you’ll see the cost for this function has also increased by a factor of 100, putting us now at just over $854 per month.
But let’s say we can better predict the demand for these 100 million requests, and maybe we’ve determined that we can meet this demand by always running 4 t4g.xlarge EC2 instances. So by right-sizing our instances, even factoring in the cost of frequent snapshots for our EBS volumes, this implementation will only cost us about $282 per month, and that’s a huge savings.
So again, the idea here isn’t to show that a particular architecture will always be the most cost-effective option, but you do see how the costs of serverless and EC2 architectures can scale very differently. And if you are able to accurately forecast demand and right-size your instances, then an EC2 instance-based architecture may be your best option in terms of both cost and performance. EC2 instances also enable you to take advantage of savings plans and reserved instances that can further reduce your costs. But keep in mind that you will be responsible for managing and administering these servers, as well as configuring things like elastic load balancing and auto scaling. So it’s important to keep those costs in mind, as well as the additional time and labor required for server administration.
So to wrap things up here, there are no hard and fast rules when it comes to choosing a serverless architecture versus a more traditional EC2 instance-based approach. But if you’re looking to avoid the time and energy associated with provisioning and maintaining infrastructure, then a serverless architecture is probably best for you. This is especially true if you’re building a new architecture from scratch. Serverless architectures are also ideal for:
- rapid prototyping,
- event-driven applications,
- applications with low levels of demand that are not frequently accessed, as well as
- cron jobs that only need to be run on a fixed schedule and don’t need to be available at any other time.
On the other hand, you’ll want to go with an EC2 instance-based architecture if you need to have root-level access to the underlying server operating system and complete control over your infrastructure. Keep in mind, however, that you’ll always need to have at least one instance up and running, even if that instance is just sitting idle most of the time. And if you want your application to be highly available, you’ll need at least two instances running across different availability zones.
Serverless services are inherently highly available and don’t require you to pay for idle or unused resources. But there will always be cases where memory and performance requirements may exceed the capabilities of services like Lambda. In these cases, you’ll have no choice but to leverage EC2 and accept the associated costs of updating, patching, and securing your instances.
Danny has over 20 years of IT experience as a software developer, cloud engineer, and technical trainer. After attending a conference on cloud computing in 2009, he knew he wanted to build his career around what was still a very new, emerging technology at the time — and share this transformational knowledge with others. He has spoken to IT professional audiences at local, regional, and national user groups and conferences. He has delivered in-person classroom and virtual training, interactive webinars, and authored video training courses covering many different technologies, including Amazon Web Services. He currently has six active AWS certifications, including certifications at the Professional and Specialty level.