Designing a CloudFormation Stack for Managing AWS Resources - DOP-C01 Exam Answer

How to Configure EC2 Instance Type in CloudFormation Template

Prev Question Next Question

Question

You are a DevOps engineer and you need to design a CloudFormation stack to manage AWS resources.

You want to use the same CloudFormation template for both production and development environments.

The stack includes an EC2 instance whose instance type should be c1.xlarge for production and m1.large for development.

You have a Condition resource called CreateProdResources that determines whether or not the environment is production.

The template uses the YAML format.

How would you configure the EC2 instance type in the CloudFormation template?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

Correct Answer - B.

Option A is incorrect because the condition CreateProdResources should not be associated with the EC2 resource itself.

With this configuration, the instance is created only when CreateProdResources is true.

Option B is CORRECT because the !If function returns c1.xlarge when CreateProdResources is true and m1.large when CreateProdResources is false.

This is the expected result.

Option C is incorrect because Fn::Not returns true for a condition that evaluates to false or returns false for a condition that evaluates to true.

It cannot return the instance type such as m1.large or c1.xlarge.

Option D is incorrect because this is not how !Equals works.

!Equals is a condition that returns true if the two values are equal or false if they aren't.

This function !Equals should not be used here.

Reference:

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html, https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/conditions-sample-templates.html.

The correct answer is B. EC2Instance: Type: "AWS::EC2::Instance" Properties: InstanceType: !If [CreateProdResources, c1.xlarge, m1.large]

Explanation:

In this scenario, the goal is to use the same CloudFormation template for both production and development environments. However, the EC2 instance type needs to be different for each environment. The Condition resource called CreateProdResources is used to determine whether the environment is production or not.

Option A is incorrect because the !Ref function is used to get the value of the named parameter or resource. In this case, it is used with an array of values [c1.xlarge, m1.large], which is not a valid syntax for !Ref.

Option C is incorrect because it uses Fn::Not, which is a function that returns the opposite of the specified condition. This syntax is not correct for this scenario, as it should return either c1.xlarge or m1.large based on the condition.

Option D is incorrect because it uses !Equals, which checks if two values are equal. However, it is used with three values [CreateProdResources, c1.xlarge, m1.large], which is not valid syntax for !Equals.

Option B is the correct answer because it uses the !If function to determine the instance type based on the CreateProdResources condition. The !If function takes three parameters: a condition, a value to return if the condition is true, and a value to return if the condition is false. In this case, the condition is CreateProdResources, and the value returned if it is true is c1.xlarge, while the value returned if it is false is m1.large.

Therefore, Option B is the correct configuration for the EC2 instance type in the CloudFormation template.