How to check if specific resource already exists in CloudFormation script
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Amazon Web Services (AWS) CloudFormation provides a powerful way to define and provision infrastructure as code. However, one of the challenges you may encounter when working with CloudFormation templates is checking whether a specific resource already exists before attempting to create or update it. CloudFormation itself lacks built-in functions to perform direct existence checks for resources. However, there are established techniques and tools to handle these scenarios effectively.
Understanding Resource Management in CloudFormation
Before diving into the solutions, it is essential to understand how CloudFormation manages resources:
- Resource Declaration: In CloudFormation, you declare the desired state of the infrastructure using a YAML or JSON template.
- Idempotency: CloudFormation operates under the principle of idempotency, meaning it maintains the infrastructure as defined without creating duplicates if resources already exist.
- Stack Operations: Resources are managed through stack operations which include creating, updating, or deleting stacks.
Knowing that CloudFormation itself does not query existing infrastructure outside its stack, alternative methods become necessary to verify resource existence.
Methods to Check Resource Existence
While CloudFormation alone does not support direct resource existence checking, combining it with AWS CLI, Lambda functions, or custom scripts in pre-creation or update phases can provide dynamic resource status checks. Here are detailed solutions to consider:
1. AWS CLI
The AWS Command Line Interface (CLI) can be used before deploying a CloudFormation stack to check the existence of resources.
- Example: S3 Bucket Existence Check
- Set Up a Custom Resource:
- Lambda Function for Resource Validation:
- CloudFormation Template Snippet:
- Resource Naming Conventions: Use unique names where possible to reduce collision risks.
- Pre-Deployment Scripts: Implement pre-deployment scripts using AWS CLI to perform sanity checks.
- Monitoring and Alerts: Set up alerts on related resources to catch missed conflicts post-deployment.
Related reading
- How to check whether my user data passing to EC2 instance is working
- How to choose an AWS profile when using boto3 to connect to CloudFront
- How to choose an AWS profile when using boto3 to connect to CloudFront
- How to clear all data from AWS CloudSearch?
- How to check if the docker engine and a docker container are running?
- How to check Kafka server status or details?
- How to close Boto S3 connection?
- How to CNAME to Amazon API Gateway Endpoint

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.