Configuring CloudFormation Stacks for Resource Import | AWS DOP-C01 Exam Question

Configuring CloudFormation Stacks for Resource Import

Prev Question Next Question

Question

Your team plans to launch two CloudFormation stacks to create resources for a web application.

These two stacks need to be managed separately.

The first stack is a network stack that includes network resources such as subnets and security groups.

The second stack is an application stack used to launch an Auto Scaling group resource.

The ASG needs to refer to the resources created by the network stack.

How would you configure the application stack to import the network resources properly?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

Correct Answer: C.

Option A is incorrect because in the network stack, you should export the resources in the outputs of the stack.

The following example exports the VPC ID:

"Outputs" : {

"VPCId" : {

"Description" : "VPC ID",

"Value" :{ "Ref" : "VPC" },

"Export" : { "Name" : {"Fn::Sub": "${AWS::StackName}-VPCID" }}

}

}

Option B is incorrect because Fn::Sub is used to replace a string.

To import values that have been exported, the intrinsic function Fn::ImportValue should be used.

Option C is CORRECT because the intrinsic function Fn::ImportValue can return the value of the outputs exported by the network stack and should be used in this scenario.

Option D is incorrect because there is no intrinsic function Fn::ExportValue.

Besides, Fn::FindInMap cannot be used to create cross-stack references.

Reference:

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/walkthrough-crossstackref.html

The correct answer is C. Export the resources in the outputs of the network stack. In the application stack, import the resources with the intrinsic function Fn::ImportValue.

When launching a CloudFormation stack, you can divide the resources to be created into separate stacks. This is particularly useful when you want to manage different sets of resources independently.

In this scenario, there are two stacks: one for network resources and another for launching an Auto Scaling group resource. The ASG resource requires information about the network resources created by the network stack.

To allow the second stack to access the resources created by the first stack, you must export the necessary resources from the first stack and import them in the second stack.

The first step is to export the resources in the outputs of the network stack. The outputs of the stack contain information about the resources created in that stack. You can export output values using the intrinsic function Fn::Export or by specifying Export property in the output block.

Next, in the application stack, you can import the resources using the intrinsic function Fn::ImportValue. This function allows you to import the value of an output exported by another stack in the same region. You specify the name of the output that you want to import as an argument to Fn::ImportValue.

Option A is incorrect because Fn::GetAtt returns the value of a specific attribute from a resource in the template, while the answer requires exporting and importing the values of resources created in separate CloudFormation stacks.

Option B is incorrect because Fn::Sub is used for string substitution, not for importing values from other stacks.

Option D is incorrect because Fn::FindInMap is used to retrieve values from a mapping declared in the CloudFormation template, not for exporting or importing values across CloudFormation stacks.

In conclusion, to import resources from one stack to another, you must export the required resources from the first stack and import them into the second stack using Fn::ImportValue.