AWS CloudFormation Template - Configure EC2 Instance Type for Production and Development Environments

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:

yaml
EC2Instance: Type: "AWS::EC2::Instance" Properties: InstanceType: !If [CreateProdResources, c1.xlarge, m1.large]

Here's an explanation of why this is the correct answer:

CloudFormation templates allow you to create, manage, and update AWS resources in a repeatable and automated way. In this case, you want to use the same template for both production and development environments, but with different instance types for the EC2 instance resource.

To achieve this, you can use a CloudFormation Condition resource called CreateProdResources that determines whether the environment is production or not. This condition can be used to control the creation of resources in your stack.

The !If intrinsic function allows you to conditionally create resources based on a specified condition. In this case, the InstanceType property is set to c1.xlarge if the CreateProdResources condition is true (meaning the environment is production), and m1.large if the condition is false (meaning the environment is not production, i.e., it's development).

Option A is incorrect because !Ref is used to get the value of a parameter or resource, but c1.xlarge and m1.large are not parameters or resources.

Option C is incorrect because Fn::Not is a function that returns the logical negation of a condition, and it does not accept a list of values as its arguments.

Option D is incorrect because !Equals is a function that returns a Boolean value indicating whether two values are equal, and it does not accept a list of values as its arguments.

Therefore, option B is the correct way to configure the EC2 instance type in the CloudFormation template.