
- Ensures one or more resources are created only after specified resources finish creation.
- Accepts either a single logical resource name or a list of logical names.
- Impacts deletion order because CloudFormation deletes resources in reverse creation order.
- Is useful when there is no direct reference relationship between dependent resources.
For more details, see the AWS CloudFormation documentation:
Below is a simple CloudFormation YAML example that creates an S3 bucket and a bucket policy. The BucketPolicy resource uses DependsOn to ensure it is created only after the bucket exists.
- “MyBucket” and “BucketPolicy” are logical resource names in the same CloudFormation template.
- The Bucket property uses
!Refto reference the bucket name. - The PolicyDocument grants the public principal (
"*") thes3:GetObjectaction for all objects in the bucket by using!Subto construct the bucket ARN.
Note: CloudFormation automatically creates implicit dependencies when a resource is referenced (for example, using
!Ref or !GetAtt). In many cases (including this example where Bucket: !Ref MyBucket is used), explicit DependsOn is unnecessary. Use DependsOn when you need to enforce ordering but there is no property reference or when you must control a precise creation/deletion sequence.Warning: Overusing DependsOn can make templates harder to maintain and slower to deploy because it forces sequential creation. Prefer implicit dependencies where possible. Use DependsOn sparingly—only when implicit references cannot express the required ordering.
- AWS CloudFormation documentation: DependsOn attribute
- AWS CloudFormation best practices
- Amazon S3 documentation