AWS CloudFormation Essential2011年〜
An IaC service that defines and provisions AWS resources as code using templates
What It Does
AWS CloudFormation is an Infrastructure as Code (IaC) service that lets you describe your AWS resource configurations in JSON or YAML template files and automatically creates, updates, and deletes resources based on those definitions. Instead of manually operating the console, you can manage your infrastructure through code. By version-controlling your templates, you can track infrastructure change history and achieve reproducible environment builds.
Use Cases
CloudFormation is widely used to create development, staging, and production environments from the same template, eliminating differences between environments. It is also used to quickly build best-practice infrastructure when starting new projects, and to rapidly rebuild disaster recovery (DR) environments in different regions.
Everyday Analogy
Think of it like a house blueprint. If you tell a builder (AWS) verbally, "Make the living room about this big, and put the kitchen over there..." you'll get a slightly different house every time. CloudFormation is the approach of handing over detailed blueprints (templates) and saying, "Build it exactly like this." With the same blueprints, you can build the exact same house every time.
What Is CloudFormation?
AWS CloudFormation is a service for managing AWS infrastructure as code. You describe the AWS resources you need (EC2 instances, S3 buckets, Lambda functions, etc.) and their configurations in text files called templates. CloudFormation reads the template and automatically creates the described resources in the correct order. This approach is called Infrastructure as Code (IaC).
Resource Management with Stacks
In CloudFormation, the collection of resources created from a template is called a "stack." You can manage the lifecycle of resources at the stack level - deleting a stack cleanly removes all associated resources. When you update a template and update the stack, CloudFormation identifies only the resources that need changes and safely applies updates. The change set feature lets you preview the impact before applying updates.
SAM and CDK
AWS SAM (Serverless Application Model) and AWS CDK (Cloud Development Kit) are tools that make CloudFormation easier to use. SAM provides a simplified template syntax for serverless applications, letting you define Lambda functions and API Gateway with shorter code. CDK lets you define infrastructure using programming languages like TypeScript and Python, which is ultimately converted into CloudFormation templates. To get a comprehensive understanding of SAM and CDK, check out specialized books (Amazon).
Getting Started
To get started with CloudFormation, create a template file in YAML or JSON. A minimal template only needs the AWSTemplateFormatVersion and Resources sections. Upload the template to the CloudFormation console, or use the AWS CLI to run sam deploy or aws cloudformation deploy to create a stack.
Things to Watch Out For
- When updating a stack, some resources may be replaced (deleted and recreated). Always check with change sets beforehand.
- The template size limit is 1 MB when uploaded via S3. For large templates, consider splitting them into nested stacks.
- Stacks in ROLLBACK_COMPLETE state cannot be updated and must be deleted and recreated.