Creating an AWS CloudFormation stack allows you to deploy a collection of related AWS resources as a single unit by using a template file. To achieve this, you upload a JSON or YAML template to the AWS Management Console or via the CLI, which AWS then parses to provision the exact infrastructure described in the code. This process ensures that your environment is repeatable, version-controlled, and free from the manual errors associated with clicking through the AWS Console.
What You'll Need
- An active AWS Account with appropriate administrative permissions.
- An IAM user or role with permissions to create CloudFormation stacks and the specific permissions required to create the resources defined in your template (e.g., EC2, S3, RDS permissions).
- A CloudFormation template file written in YAML or JSON format.
- An internet connection and a modern web browser to access the AWS Management Console.
Step-by-Step
- Log in to the AWS Management Console and use the search bar at the top to find and select CloudFormation. Note that menu labels may differ slightly depending on your region or account version.
- On the CloudFormation dashboard, click the Create stack button. You will typically be presented with a choice between With new resources (standard) or With new resources (advanced). For most standard deployments, select With new resources (standard).
- In the Prepare template section, choose how you want to provide your template. If the file is stored on your local computer, select Upload a template file, click Choose file, and select your YAML or JSON file. If your template is stored in an Amazon S3 bucket, select Amazon S3 URL and paste the direct link to the file.
- Click Next to move to the Specify stack details page. Here, you must provide a Stack name. This name must be unique within your AWS region and is used to identify the group of resources in the console.
- Fill in the Parameters section. If your template includes parameters (such as instance types, key pair names, or VPC IDs), you will see input fields here. Enter the specific values required for your current environment. If the template has default values, they will be pre-filled.
- Click Next to reach the Configure stack options page. Here you can specify Tags. Tags are key-value pairs (e.g., Environment: Production) that help you organize and track costs for the resources created by this stack. You can also define an IAM role for CloudFormation to use to create the resources; if left blank, CloudFormation uses your current user permissions.
- Click Next to arrive at the Review page. Carefully scroll through the summary. Check the template body to ensure it is the correct version and verify that all parameter values are accurate. Pay close attention to the Capabilities section at the bottom. If your template creates IAM users or roles, you must check the box acknowledging that the template might create custom IAM resources.
- Click Submit. AWS will now begin the creation process.
- Navigate to the Events tab of your new stack. You will see a real-time log of resources being created. A resource status of
CREATE_IN_PROGRESSindicates the action is underway, andCREATE_COMPLETEindicates success. - Wait until the overall stack status changes to
CREATE_COMPLETE. Once this happens, you can navigate to the Outputs tab to find important information generated during creation, such as the public IP address of an EC2 instance or the DNS name of a Load Balancer.
Common Mistakes
One of the most frequent errors is a mismatch in IAM permissions. Users often assume that because they have access to the CloudFormation console, the service has permission to create the resources listed in the template. If the IAM user creating the stack does not have permission to create an S3 bucket or an EC2 instance, the stack will fail during the creation phase. Always ensure your IAM policy covers every resource type mentioned in the YAML/JSON file.
Another common mistake is neglecting the Capabilities acknowledgment. When a template contains AWS::IAM::Role or AWS::IAM::User resources, AWS requires an explicit confirmation to prevent the accidental creation of privileged identities. If you forget to check this box on the final review page, the stack creation will fail immediately with an error stating that capabilities are missing.
Circular dependencies are also a frequent hurdle. This happens when Resource A depends on Resource B, but Resource B also depends on Resource A. CloudFormation cannot resolve this logic and will hang or fail. Ensure your DependsOn attributes are logically sequenced and do not create a loop.
Incorrectly formatted YAML is a recurring issue. YAML is sensitive to indentation. A single misplaced space in a nested property can lead to a Template format error: Every Resource must have a Type message. Using a dedicated IDE with a YAML linter is highly recommended to avoid these syntax errors before uploading.
Tips
Use Parameters instead of hard-coding values. By using parameters for things like instance sizes or environment names, you can use the same template to deploy a "Dev" environment and a "Prod" environment without editing the code itself. This makes your infrastructure truly portable and reusable.
Leverage Mappings for region-specific values. If your template needs to deploy to multiple AWS regions, use the Mappings section to define different AMI IDs for each region. This prevents the stack from failing when you try to launch it in a region where a specific hard-coded AMI does not exist.
Implement Stack Policies to protect critical resources. A stack policy allows you to define which resources can be updated during a stack update. For example, you can set a policy that prevents the accidental deletion or replacement of a production database instance while still allowing the update of associated security groups.
Utilize DeletionPolicy for stateful resources. For resources like RDS databases or S3 buckets containing critical data, set the DeletionPolicy to Retain. This ensures that if the stack is deleted, the actual data storage is kept intact in your account rather than being permanently destroyed along with the stack.
Troubleshooting
If your stack fails to create, the first step is to check the Events tab. Look for the first single resource that entered the CREATE_FAILED state. The Status reason column will provide the specific error message. If the error is Insufficient permissions, review your IAM policies to ensure the user has the rights to create that specific resource.
If the stack is stuck in CREATE_IN_PROGRESS for an unusually long time, it is often due to a resource timeout. This frequently happens with EC2 instances that fail health checks or Load Balancers that cannot find healthy targets. Check the underlying resource in its respective console (e.g., the EC2 console) to see if the instance is stuck in a boot loop or failing to initialize.
When a stack fails during creation for the first time, AWS will typically trigger a Rollback. This means AWS will delete every resource it successfully created up to that point to return your account to its original state. If you want to debug the failure without the resources being deleted, you can enable Rollback configuration during the creation process and select Preserve successfully provisioned resources. This allows you to inspect the failed resource before manually deleting the stack.
If you encounter a Template format error, validate your template using the AWS CLI command aws cloudformation validate-template. This tool checks the syntax of your JSON or YAML file without attempting to deploy it, allowing you to find indentation or structural errors quickly.
Frequently Asked Questions
Can I update a stack after it has been created?
Yes, you can use the Update action in the console to upload a revised template or change parameter values. CloudFormation will determine the difference between the current state and the new template and apply only the necessary changes.
What is the difference between a stack and a nested stack?
A standard stack is a single collection of resources. A nested stack is a stack created as a resource within another stack. This allows you to create modular templates that can be reused across different projects.
How do I delete a stack without deleting the resources?
You cannot delete a stack and keep the resources unless you have previously set the DeletionPolicy to Retain for those specific resources in the template. Otherwise, deleting the stack removes everything associated with it.
Can I import existing resources into a CloudFormation stack?
Yes, AWS provides a resource import feature that allows you to bring existing AWS resources under the management of a CloudFormation stack without recreating them from scratch.
What happens if a stack update fails?
By default, CloudFormation will perform a rollback, reverting all resources to their previous stable state. You can configure the stack to stop the rollback if you need to investigate the cause of the failure.
Does CloudFormation cost money to use?
The CloudFormation service itself is free for most standard AWS resources. You only pay for the AWS resources (like EC2 instances or S3 buckets) that the stack creates and manages.