25 November 2022 - 7 min. read
We are in the middle of the serverless age, in a moment where the advantages of this approach become more and more evident.One of the advantages that contributed the most to the rise of the serverless paradigm is certainly the promise of significant economic advantages. But are you really saving money? And: are you taking full advantage of the serverless paradigm?
What is serverless?Serverless is a native cloud architecture that enables you to shift more and more of your operational responsibilities to the cloud provider, in this case, AWS, increasing your agility and innovation. Serverless allows you to build and run applications and services without caring about servers and thus eliminating infrastructures management tasks such as servers or cluster provisioning, patching, operating system maintenance, and capacity provisioning. Serverless helps you run and scale your application while high availability is provided transparently by the services you use as building blocks.While Designing the architecture of a serverless application, you need to think about different topics, including storage, database, computing, messaging, and many more.AWS offers plenty of services you can use to make a valid serverless application. As you need to pick the right tool for each scope, the knowledge of serverless services is vital to make wise choices.
Not only LambdaWhen thinking about serverless the first AWS service that comes to mind is for sure AWS Lambda, indeed the computing part of a serverless application is probably the most iconic and important one. However, a serverless architecture is not composed only by lambdas, but also by different AWS managed services which in turn must be serverless.Here is a brief and not exhaustive list of serverless and fully managed services for each tier of an application.
ComputeOf course, we have, AWS Lambda and Lambda@Edge, which are usually associated with serverless and attract the majority of frameworks and thirty parties solutions, but alsoAWS Fargate, that is a purpose-built serverless compute engine for containers.
StorageAmazon Simple Storage Service (Amazon S3) provides a secure, durable, highly-scalable object storage.
DatabaseAmazon DynamoDB is a fast and flexible NoSQL database service for all the applications that need consistent, single-digit millisecond latency at any scale.Amazon Aurora Serverless is an on-demand, auto-scaling configuration for Amazon Aurora. The database will automatically start-up, shut-down, and scale capacity up or down based on your application's needs.
API ProxyAmazon API Gateway is a fully managed service and offers a comprehensive platform for API management with support for authorization, access control, monitoring, and API version management.
Application integrationAmazon SNS is a fully managed pub/sub messaging service that makes it easy to decouple and scale microservices, distributed systems, and serverless applications.Amazon SQS is a fully managed message queuing service that makes it easy to decouple and scale microservices, distributed systems, and serverless applications.AWS AppSync simplifies application development by letting you create a flexible GraphQL API to securely access, manipulate, and combine data from one or more sources.
OrchestrationAWS Step Functions makes it easy to coordinate the components of distributed applications and microservices using visual workflows. Step Functions is a reliable way to coordinate components and step through the functions of your application.
AnalyticsAmazon Kinesis is a platform for streaming data on AWS, offering powerful services to make it easy to load and analyze streaming data, and also providing the ability for you to build custom streaming data applications for specialized needs.Amazon Athena is an interactive query service that makes it easy to analyze data in Amazon S3 using standard SQL. Athena is serverless, so there is no infrastructure to manage, and you pay only for the queries that you run.
AWS Lambda: pricing model and pitfallsWhen creating a serverless application, it is important to keep in mind its pricing model in order to structure the services and triggers in a way that allows you to achieve the best performances at the lowest price.AWS Lambda has a really interesting pricing model if used in the right way; also, you can leverage a generous free tier of 1M free requests per month and 400,000 GB-seconds of computing time per month. This free tier does not expire after the first year.The Lambda pricing model is all about invocations and execution duration. Lambda counts a request each time it starts executing in response to an event notification or invoke call. The duration is calculated from the time your code execution begins to the moment it ends, rounded up to the nearest 100ms by excess. This means that the execution time is paid "in steps" of 100ms. Having a function whose average execution time is very close to the threshold can thus considerably increase the overall execution cost.The price of each 100ms “time step” depends on the amount of memory you allocate to your function. An increase in memory size triggers an equivalent increase in the computational power available to your function. Requests: $0.20 per 1M requestsThe following facts can thus be deduced from the pricing model:
- Optimizing the execution time is crucial, the code must make the best use of resources and terminate as soon as possible.
- The Lambda size affects response time and overall costs. We need to balance the cost with the response time, keeping in mind that the price is for a step of 100ms.
- A lambda function should never be waiting for something: triggers, queues, step functions should be used to decouple the services and invoke the Lambda only when necessary.