Create a CloudFormation Stack with Dependent Resources

Ensure Web Server Creation After Database Server Creation

Prev Question Next Question

Question

You are designing a cloudformation stack which involves the creation of a web server and a database server.

You need to ensure that the web server in the stack gets created after the database server is created.

How can you achieve this?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

Answer - D.

The AWS Documentation mentions.

With the DependsOn attribute you can specify that the creation of a specific resource follows another.

When you add a DependsOn attribute to a resource, that resource is created only after the creation of the resource specified in the DependsOn attribute.

For more information on the DependsOn attribute, please visit the below url.

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-dependson.html

When creating a CloudFormation stack with multiple resources, it's important to ensure that certain resources are created in a specific order. In this case, we want to create the database server before the web server.

There are a few different ways to achieve this in CloudFormation, but the most straightforward way is to use the "DependsOn" attribute.

Option A: Ensure that the database server is defined first and before the web server in the CloudFormation template. The stack creation normally goes in order to create the resources. This is not entirely correct. While it is true that CloudFormation creates resources in the order they are defined in the template, it's not a reliable way to ensure that one resource is created before another. There could be other factors at play (such as resource dependencies or resource creation time) that could cause the web server to be created before the database server.

Option B: Ensure that the database server is defined as a child of the web server in the CloudFormation template. This is also not correct. In CloudFormation, child resources are created at the same time as their parent resource, so this would not ensure that the database server is created before the web server.

Option C: Ensure that the web server is defined as a child of the database server in the CloudFormation template. This is not correct for the same reasons as option B.

Option D: Use the DependsOn attribute to ensure that the database server is created before the web server. This is the correct answer. The "DependsOn" attribute in CloudFormation allows you to specify dependencies between resources. When you specify a dependency, CloudFormation ensures that the dependent resource is created after the resource it depends on. In this case, we would specify that the web server depends on the database server.

Here is an example CloudFormation template that uses "DependsOn" to ensure the correct resource creation order:

yaml
Resources: DatabaseServer: Type: AWS::RDS::DBInstance ... WebServer: Type: AWS::EC2::Instance DependsOn: DatabaseServer ...

In this example, the "WebServer" resource depends on the "DatabaseServer" resource, so CloudFormation will ensure that the database server is created before the web server.