IaC Cost Patterns

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

Antipattern: Expensive network resource

Network resources like NAT gateways, elastic IP addresses and subnets tend to be expensive while not being strictly needed.

Context

Due to their interdependence, the cost of certain types of networking resources often adds up. For example, a developer may create multiple subnets, each having its own NAT gateway, each of which in turn is assigned an IPv4 address. In other cases, network resources are used which are not strictly required, e.g. load balancers.

Solution

It is often possible to forego the use of the expensive resources entirely. Solutions include subnets sharing a single NAT gateway, reducing the number of subnets or removing the use of load balancers.

Example

Remove resources that are not strictly required, or reduce the number of networking resources. For example, the commonly used module terraform-aws-modules/vpc has an option to use a single NAT gateway instead of creating one per subnet:

module "vpc" {
  source = "terraform-aws-modules/vpc"

  # ...

  enable_nat_gateway = true
  single_nat_gateway = true

  # ...
}

References

Occurrences

Terraform

AWS CloudFormation