AWS CloudFormation Template for Dynamic Resource Provisioning

Create Dynamic Cloudformation Templates | AWS SysOps Administrator Exam | Amazon

Question

You have been asked to create a Cloudformation template that would spin up resources on demand.

You have to ensure flexibility in the template so that values are based on the region in which the template is launched.

Which of the following section in the template can help you accomplish this?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

Correct Answer: D.

This is given in the AWS Documentation.

The optional Mappings section matches a key to a corresponding set of named values.

For example, if you want to set values based on a region, you can create a mapping that uses the region name as a key and contains the values you want to specify for each specific region.

You use the Fn::FindInMap intrinsic function to retrieve values in a map.

Option A is incorrect since this section is used to define the actual creation of resources.

Option B is incorrect since it includes statements that define when a resource is created or when a property is defined.

Option C is incorrect since this is used to declare output values that you can import into other stacks.

For more information on the mappings section in Cloudformation, please refer to the below URL-

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/mappings-section-structure.htm

The correct answer is D. Mappings.

Mappings in a CloudFormation template allow you to create a static mapping between keys and corresponding values. These mappings are typically used to define values that differ based on the region or availability zone in which the resources are being launched.

For example, if you need to define an Amazon Machine Image (AMI) for a particular region, you could create a mapping for each region that specifies the corresponding AMI ID. When the template is launched in a particular region, the appropriate mapping would be used to determine the correct AMI ID.

Here's an example of how you could use a mapping to define an AMI ID based on the region:

yaml
Mappings: RegionMap: us-east-1: AMI: ami-0ff8a91507f77f867 us-west-2: AMI: ami-0a10b2721688ce9d2 eu-west-1: AMI: ami-0d729a60af1b2c046

In this example, we define a mapping called RegionMap that includes three regions: us-east-1, us-west-2, and eu-west-1. For each region, we specify the corresponding AMI ID. We can then reference this mapping in our resource definitions using the Fn::FindInMap function:

yaml
Resources: EC2Instance: Type: "AWS::EC2::Instance" Properties: ImageId: !FindInMap [RegionMap, !Ref "AWS::Region", AMI] InstanceType: t2.micro ...

In this example, we reference the RegionMap mapping and use the Fn::FindInMap function to look up the appropriate AMI ID based on the region in which the template is launched. The !Ref "AWS::Region" function returns the current region, which is used as the key in the mapping lookup.

By using mappings in your CloudFormation templates, you can make your resources more flexible and reusable across different regions and availability zones.