IaC Cost Patterns

A collection of patterns and antipatterns for managing cost in IaC files.

Pattern: Cost Report

Cost report elements can be used to obtain information on the actual spendings over a period of time.

Context

Given the complexity and scale of cloud computing projects, cost management might be overlooked and, in turn, lead to overspending.

Solution

Major cloud providers offer cost reporting tools such as CUR buckets and lambda rules to collect and later visualize and analyze cost and usage information. These tools enable cost tracking, allow programmers to gain insights into spending trends and might uncover cost-related issues

Example

Add a monthly cost and usage report:

@@ -0,0 +1,24 @@ resource "cost_report" "example" {
+  AWSTemplateFormatVersion: '2010-09-09'
+  Description: 'Monthly Cost and Usage Report'
+  Parameters:
+    Bucket:
+      Type: String
+    Frequency:
+      Type: String
+      Default: "monthly"
+      AllowedValues:
+      - "monthly"
+      - "weekly"
+      - "hourly"
+  Resources:
+    MonthlyCostReport:
+      Type: "AWS::CUR::ReportDefinition"
+      Properties:
+        ReportName: !Sub "${AWS::StackName}"
+        ReportVersioning: False
+        TimeUnit: !Ref Frequency
+        Compression: "parquet"
+        Format: "parquet"
+        S3Region: !Ref primaryRegion
+        S3Prefix: !Ref Frequency
+        S3Bucket: !Ref Bucket
}

References

Occurrences

AWS CloudFormation