
AWS Edge Services - Cost optimization
In addition to its benefits for performance, security and availability of your web application, CloudFront can be used to reduce the costs of web application.
- The Regional Data Transfer Out (DTO) to Internet, which accounts for the amount of GBs served by CloudFront to users on the internet. Note that this pricing depends on the region of users, it's metered using binary gigabytes (i.e. 1 GB = 1024 MB), and it only includes the HTTP payload (e.g. excluding TLS handshake data transfer).
- The Request fees, which accounts for the number of HTTP(S) requests served by CloudFront. It also depends on the region of the users.
- Rely on HTTP cache control mechanisms (e.g. Cache-Control header, URL versioning, conditional requests) by default, and prefer CDN based invalidations for break glass scenarios. For more details, read this blog.
- Use a wildcard in your invalidations when applicable to invalidate multiple files with a single invalidation request. For example, use /stories/sports/worldcup* to invalidate all articles related to Worldcup event, instead of invalidating them one by one.
- Enable AWS WAF exclusively on the CloudFront distribution that requires protection according to your threat modeling. For example, if your static content hosted on S3 do not require WAF protection, move them to a separate domain (e.g. static.example.com) on a separate CloudFront distribution, and only enable AWS WAF on the main distribution (e.g. www.example.com) which is serving your HTML files.
- When you subscribe to Shield Advanced, the AWS WAF request fees are included in Shield Advanced pricing. Shield Advanced charges a monthly subscription, and a premium fee on your application Data Transfer Out. If your application is request intensive (High request per second with low average payload, e.g. with APIs), Enabling Shield Advanced might help you reduce your overall WAF costs.
- Finally, some AWS WAF rules have additional costs, such as CATPCHA, AWS Bot Control or managed rules by vendors in the AWS Marketplace. Optimize these costs by scoping down such rules to specific conditions (e.g. only CAPTCHA suspicious IPs or only apply Bot Control on HTML requests), and by using at the end of the WebACL, giving the chance for prior free rules to block malicious requests at no additional cost. Read this blog for more in-depth explanation of WAF rule cost optimizations.
- Leverage browser caching to reduce the amount of requests delivered by CloudFront
- Implement ETags on your objects to enable conditional requests. Object refreshes with 304 Not Modified responses are cheaper to deliver compared to full fetches.
- Implement image optimization to compress images using modern formats such as WebP and Avif. In addition, lazy load images on your website to avoid downloading ones that are not rendered by browsers.
- Implement Adaptive Bit Rate streaming for video delivery. Consider QVBR video encoding. Consider delivering optimized rendition ladder according to device types. This blog explains this concept, however it was written using Lambda@Edge prior to launching CloudFront Functions.
- Use a Same Origin Policy if possible for API calls, which removes the cost of preflight CORS OPTION requests. Read this blog for further details.
- Consider all logging options and select most cost efficient one for you. CloudFront's standard access logs is provided at no additional charges, however it applies to all your traffic. On the contrary, CloudFront's real time logs are granularly configured by CloudFront Cache Behavior, and by required field for an incremental charge. WAF offers multiple logging options, the most cost efficient one being the one based on Kinesis Firehose. Note that you can filter logged requests based on dimensions like rule action or emitted label.
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.