AWS CloudFormation Template for Installing Web Servers on EC2 Instances

AWS CloudFormation Template for Installing Web Servers on EC2 Instances

Prev Question Next Question

Question

You are designing a cloudformation template to install a set of web servers on EC2 Instances.

The following User data needs to be passed to the EC2 Instances.

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

Answer - A.

Below is an example on how the user data can be passed in a cloudformation template.

{

"Resources" : {

"WebServerInstance": {

"Type": "AWS::EC2::Instance",

"Properties" :{

"InstanceType" : "t2.micro",

"ImageId" : "ami-6f198a0c",

"UserData": {

"Fn::Base64": {

"Fn::Join": [

"\n",

[

"#!/bin/bash",

"sudo apt-get update",

sudo apt-get install -y nginx]]}}

}

}

}

}

For more information on User data in cloudformation templates, please refer to the below link:

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/deploying.applications.html

When launching EC2 instances through CloudFormation, user data can be passed to the instances to execute custom scripts or perform any other actions as required. The user data can be passed to the EC2 instance in the following ways:

A. In the properties section of the EC2 Instance in the resources section In this option, the user data can be added in the "UserData" property of the EC2 instance resource. This property accepts a string value that can be used to pass any required data or scripts to the instance. The user data can be specified as plain text or as a Base64-encoded string.

B. In the properties section of the EC2 Instance in the Output section The Output section of a CloudFormation template is used to export values from the stack that can be used by other stacks or resources. In this option, the user data can be added to the Output section as a string value and then imported to the EC2 instance resource using the Fn::ImportValue function.

C. In the Metadata section of the EC2 Instance in the resources section The Metadata section of an EC2 instance is used to provide information about the instance to other resources in the stack. In this option, the user data can be added to the Metadata section of the EC2 instance resource and then accessed from within the instance using the EC2 metadata service.

D. In the Metadata section of the EC2 Instance in the Output section. This option is not valid, as the Metadata section of an EC2 instance resource cannot be added to the Output section of a CloudFormation template.

In summary, the most common and recommended way to pass user data to an EC2 instance in CloudFormation is by adding it to the "UserData" property of the EC2 instance resource in the properties section of the resources section.