Importing Network Resources for AWS CloudFormation Stacks

Using CloudFormation to Import Network Resources

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 two CloudFormation stacks that need to be managed separately, it's important to ensure that they can communicate with each other. In this scenario, the second stack needs to reference the resources created in the first stack.

To achieve this, you can export the resources in the outputs of the network stack, which can then be imported by the application stack using the intrinsic function Fn::ImportValue.

Here's how you can configure this:

  1. In the network stack, specify the resources that need to be exported as outputs using the Outputs section of the CloudFormation template. For example, you can export a subnet ID as follows:
yaml
Outputs: SubnetID: Description: The ID of the subnet Value: !Ref MySubnet Export: Name: my-network-stack-SubnetID

The Export section specifies the name of the export, which can be any string that you choose. This name is used to import the value in the application stack.

  1. In the application stack, you can import the exported value using the intrinsic function Fn::ImportValue. For example, you can reference the subnet ID in the LaunchConfigurationName property of an Auto Scaling Group as follows:
yaml
Resources: MyAutoScalingGroup: Type: AWS::AutoScaling::AutoScalingGroup Properties: LaunchConfigurationName: Fn::ImportValue: my-network-stack-SubnetID

The Fn::ImportValue function takes the name of the export as an argument, and returns the value that was exported from the other stack.

Using this approach, you can import values from one CloudFormation stack to another, allowing you to manage them separately while still enabling communication between them.

Option A is incorrect because Fn::GetAttr is used to retrieve attributes of a resource, not to import values from another stack.

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

Option D is incorrect because Fn::ExportValue is used to export values, not to import them, and Fn::FindInMap is used to retrieve values from a mapping, not to import values from another stack.