Template structure (JSON/YAML) in AWS - Time & Space Complexity
When working with AWS templates in JSON or YAML, it's important to understand how the time to process these templates grows as they get bigger.
We want to know how the number of resources or sections affects the time AWS takes to create or update the infrastructure.
Analyze the time complexity of deploying a CloudFormation template with multiple resources.
{
"Resources": {
"MyInstance1": { "Type": "AWS::EC2::Instance" },
"MyInstance2": { "Type": "AWS::EC2::Instance" },
"MyInstance3": { "Type": "AWS::EC2::Instance" }
}
}
This template defines several EC2 instances to be created as resources.
Look at what happens repeatedly when deploying this template.
- Primary operation: Creating each EC2 instance resource.
- How many times: Once per resource defined in the template.
As you add more resources, AWS must create each one separately.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 resource creations |
| 100 | 100 resource creations |
| 1000 | 1000 resource creations |
Pattern observation: The number of operations grows directly with the number of resources.
Time Complexity: O(n)
This means the time to deploy grows in a straight line as you add more resources.
[X] Wrong: "Adding more resources won't affect deployment time much because AWS handles everything fast."
[OK] Correct: Each resource requires separate work, so more resources mean more time overall.
Understanding how deployment time grows with template size shows you can plan and manage cloud resources efficiently.
"What if the template uses nested stacks instead of one big list of resources? How would the time complexity change?"