In this lab you will learn about Outputs. Outputs enable you to get access to information about resources within a stack. For example, you can output an EC2 instance’s Public DNS name once it is created.
Furthermore, output values can be imported into other stacks. These are known as cross-stack references.
The Outputs section consists of the key name
Outputs, followed by a colon.
Outputs: Logical ID: Description: Information about the value Value: Value to return Export: Name: Value to export
You can declare a maximum of 60 outputs in a template.
In this Lab, you will:
Copy the code as you go through the topics below.
To get the PublicDnsName of the instance, you will need to use
Fn::GetAtt intrinsic function. Let’s first check the AWS Documentation for available attributes. You can see that PublicDnsName is valid return value for
Add the section below to your template:
Outputs: EC2PublicDNS: Description: 'Public DNS of EC2 instance' Value: !GetAtt WebServerInstance.PublicDnsName
Go to the AWS console and update your stack with a new template.
View the output value on the AWS CloudFormation console, in the Outputs tab.
In this exercise, you should assign an Elastic IP to your EC2 instance. Then, add an output of the Elastic IP to the Outputs section of the template. You should continue using the
AWS::EC2::EIPresource and attach it to your existing EC2 instance.
ElasticIPand add it to the Outputs section of the template.
Check out the AWS Documentation for AWS::EC2::EIP resource
Resources: WebServerInstance: Type: AWS::EC2::Instance Properties: ImageId: !Ref AmiID InstanceType: !FindInMap [EnvironmentToInstanceType, !Ref EnvironmentType, InstanceType] Tags: - Key: Name Value: !Join [ '-', [ !Ref EnvironmentType, webserver ] ] WebServerEIP: Type: 'AWS::EC2::EIP' Properties: Domain: vpc InstanceId: !Ref WebServerInstance Outputs: WebServerPublicDNS: Description: 'Public DNS of EC2 instance' Value: !GetAtt WebServerInstance.PublicDnsName WebServerElasticIP: Description: 'Elastic IP assigned to EC2' Value: !Ref WebServerEIP
Great work! You have now successfully learned how to use Outputs in CloudFormation template.