0
0
AWScloud~5 mins

Why Infrastructure as Code matters in AWS - Why It Works

Choose your learning style9 modes available
Introduction
Managing cloud resources by hand can be slow and error-prone. Infrastructure as Code lets you write down your setup in files, so you can create and change resources automatically and safely.
When you want to create the same cloud setup multiple times without mistakes.
When you need to update your servers or databases and want to track changes clearly.
When you want to share your cloud setup with teammates so everyone uses the same settings.
When you want to fix problems quickly by rolling back to a previous setup version.
When you want to avoid clicking around in the cloud console and instead use code to manage resources.
Commands
This command creates a new cloud stack using a template file that describes your infrastructure. It automates resource creation.
Terminal
aws cloudformation create-stack --stack-name example-stack --template-body file://example-template.yaml
Expected OutputExpected
An event is created to track the stack creation. You will see a StackId like arn:aws:cloudformation:us-east-1:123456789012:stack/example-stack/abcd1234-5678-90ef-ghij-klmnopqrstuv
--stack-name - Names your stack so you can manage it later
--template-body - Points to the file describing your infrastructure
This command checks the current status of your stack to see if resources were created successfully.
Terminal
aws cloudformation describe-stacks --stack-name example-stack
Expected OutputExpected
{ "Stacks": [ { "StackName": "example-stack", "StackStatus": "CREATE_COMPLETE", "CreationTime": "2024-06-01T12:00:00.000Z" } ] }
--stack-name - Specifies which stack to check
This command updates your existing stack with changes from the template file, allowing safe modifications.
Terminal
aws cloudformation update-stack --stack-name example-stack --template-body file://example-template.yaml
Expected OutputExpected
An event is created to track the stack update. You will see a StackId like arn:aws:cloudformation:us-east-1:123456789012:stack/example-stack/abcd1234-5678-90ef-ghij-klmnopqrstuv
--stack-name - Names the stack to update
--template-body - Points to the updated infrastructure description
This command deletes the stack and all resources it created, cleaning up your cloud environment.
Terminal
aws cloudformation delete-stack --stack-name example-stack
Expected OutputExpected
No output (command runs silently)
--stack-name - Specifies which stack to delete
Key Concept

If you remember nothing else from this pattern, remember: writing your cloud setup as code makes managing and repeating infrastructure safe, fast, and clear.

Common Mistakes
Trying to create a stack with a template that has syntax errors.
The stack creation fails because the cloud service cannot understand the broken template.
Always validate your template file before creating or updating a stack.
Manually changing resources in the cloud console after creating a stack.
Manual changes can cause your code and actual resources to get out of sync, leading to confusion and errors.
Make all changes through your code templates and update the stack to keep everything consistent.
Deleting a stack without checking what resources it will remove.
You might accidentally delete important servers or databases without backup.
Review the stack resources carefully before deleting and back up any important data.
Summary
Use AWS CloudFormation commands to create, update, check, and delete infrastructure stacks.
Write your infrastructure setup in template files to automate and track changes.
Avoid manual changes in the cloud console to keep your code and resources in sync.