AWS CloudFormation Template - Edit Section for Load Balancer DNS Retrieval

CloudFormation Template - Edit Section for Load Balancer DNS

Prev Question Next Question

Question

You have written a CloudFormation template that creates 1 elastic load balancer fronting 2 EC2 instances.

Which section of the template should you edit so that the DNS of the load balancer is returned upon creation of the stack?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

Answer - C.

The below example shows a simple CloudFormation template.

It creates an EC2 instance based on the AMI - ami-d6f32ab5

When the instance is created, it will output the AZ in which it is created.

{

"Resources": {

"MyEC2Instance": {

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

"Properties": {

"ImageId": "ami-d6f32ab5"

}

}

},

"Outputs": {

"Availability": {

"Description": "The Instance ID",

"Value":

{ "Fn::GetAtt" : [ "MyEC2Instance", "AvailabilityZone" ]}

}

}

}

To understand more on CloudFormation, please visit the URL:

https://aws.amazon.com/cloudformation/

The correct answer is C. Outputs.

In AWS CloudFormation, an output section is used to declare the values that should be returned after a stack is created or updated. Outputs allow you to retrieve information about resources that were created by a stack, such as IP addresses, URLs, and DNS names.

In this scenario, we want to retrieve the DNS of the Elastic Load Balancer (ELB) that is created by the CloudFormation template. Therefore, we need to add an Outputs section to the template that specifies the DNS name of the ELB as an output.

Here's an example of how to add an output section to the CloudFormation template:

yaml
Resources: MyLoadBalancer: Type: AWS::ElasticLoadBalancing::LoadBalancer Properties: ... MyEC2Instance1: Type: AWS::EC2::Instance Properties: ... MyEC2Instance2: Type: AWS::EC2::Instance Properties: ... Outputs: LoadBalancerDNS: Value: !GetAtt MyLoadBalancer.DNSName

In this example, the output section is defined after the Resources section. It declares an output called "LoadBalancerDNS" that returns the DNS name of the "MyLoadBalancer" resource.

The "!GetAtt" function is used to retrieve the value of the "DNSName" attribute of the "MyLoadBalancer" resource. This function returns a string that contains the DNS name of the ELB.

Once the CloudFormation stack is created or updated, you can retrieve the DNS name of the ELB by using the AWS CLI or the AWS Management Console.

For example, if you use the AWS CLI, you can run the following command to retrieve the value of the "LoadBalancerDNS" output:

scss
aws cloudformation describe-stacks --stack-name MyStackName --query 'Stacks[0].Outputs[?OutputKey=='LoadBalancerDNS'].OutputValue' --output text

This command retrieves the value of the "LoadBalancerDNS" output for the CloudFormation stack called "MyStackName".