How Do You Make CloudFormation Create an RDS DB Instance Before an EC2 Instance?

Answer Correct answer: B — Add the DependsOn attribute to the EC2 instance resource referencing the RDS DB instance's logical name so CloudFormation creates the database first.

A company uses an AWS Cloud Formation template to provision an Amazon EC2 instance and an Amazon RDS DB instance. A CloudOps engineer must update the template to ensure that the DB instance is created before the EC2 instance is launched. What should the CloudOps engineer do to meet this requirement?

  1. Add a wait condition to the template. Update the EC2 instance user data script to send a signal after the EC2 instance is started.
  2. Add the DependsOn attribute to the EC2 instance resource, and provide the logical name of the RDS resource. Correct Answer
  3. Change the order of the resources in the template so that the RDS resource is listed before the EC2 instance resource.
  4. Create multiple templates. Use AWS CloudFormation StackSets to wait for one stack to complete before the second stack is created.

Community Insight

It tests whether you know CloudFormation's dependency graph is driven by DependsOn and intrinsic references — not by the physical order of resources in the template.

CloudFormation builds resources in parallel unless a dependency is declared, so ordering inside the template is irrelevant. This page confirms that adding the DependsOn attribute to the EC2 instance with the RDS resource's logical name is what forces the database to be created first in an SOA-C03 scenario.

Choosing option C and simply moving the RDS resource above the EC2 instance, assuming CloudFormation reads the template top-to-bottom; the service actually provisions independent resources in parallel regardless of listing order.

Community Discussion (1 comments)

Bad_Mat 👍 1 Selected: B
B. Add the DependsOn attribute to the EC2 instance resource, and provide the logical name of the RDS resource

Comments & Corrections

No comments yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Submitted for moderation before publishing. Keep it helpful and respectful.

Expert Analysis

Why the Answer Is Correct

CloudFormation builds a dependency graph and creates every resource that has no dependency in parallel, so explicit ordering must be expressed in the template itself. The DependsOn attribute on the EC2 instance, set to the logical ID of the RDS DB instance, tells CloudFormation to hold the instance until the database resource has been created successfully. This is exactly the requirement stated in the question: the DB instance must exist before the EC2 instance is launched. It is also the cleanest fix because it does not require user data scripts, signals, or splitting the stack. Option B therefore satisfies the requirement directly using a documented template attribute.

Why the Other Options Are Wrong

Option A adds a wait condition plus a cfn-signal call from the EC2 instance's user data, but that makes the EC2 instance wait on itself — a resource cannot signal before it is launched, and RDS readiness would still not be gated. Option C reflects a common misconception: CloudFormation does not honor the line order of resources in the template, so listing RDS first changes nothing. Option D proposes multiple templates coordinated with StackSets, which is designed for deploying stacks across accounts and Regions, not for ordering two resources inside one stack; it also introduces cross-stack coupling and operational overhead for a problem the template can solve in one line.

Community Comment Notes

Community input on this item is thin, but the one recorded learner, Bad_Mat, selected option B — "Add the DependsOn attribute to the EC2 instance resource, and provide the logical name of the RDS resource" — which matches the documented behavior of the attribute. The absence of dissent is consistent with the fact that DependsOn is the canonical CloudFormation ordering mechanism taught for this exam. Note that implicit dependencies are also created whenever one resource references another with Ref or GetAtt, but no such reference is described here, so the explicit attribute is required.

Exam Tip

When a question asks for creation order and no intrinsic function ties the resources together, look immediately for DependsOn on the dependent (later) resource.

Official Reference

Exam Strategy

For any CloudFormation ordering question, ask which resource must wait and then attach DependsOn to that resource with the other resource's logical ID. Eliminate answers based on template line order or on splitting stacks, since those do not change intra-stack creation sequencing.

Frequently Asked Questions

Why doesn't reordering the RDS resource above the EC2 instance in the template work?

CloudFormation resolves a dependency graph and provisions resources without dependencies in parallel; the order resources appear in the template file has no effect on creation order.

When would a DependsOn attribute not be needed between an RDS instance and an EC2 instance?

If the EC2 resource already references the DB with Ref or GetAtt (for example in UserData), CloudFormation infers an implicit dependency and still creates the database first.

More SOA-C03 FAQ →

Related Analysis

Practice All SOA-C03 Questions

Access 100 questions with complete answers and detailed explanations.

View Full SOA-C03 Practice Test →

← Back to SOA-C03 Study Guide