liveBook · Manning

CloudFormation output

As explained in Amazon Web Services in Action, Third Edition: An in-depth guide to AWS

A CloudFormation output is a template declaration that exposes a value derived from a provisioned resource or other template data.

A CloudFormation output exposes a value from a resource created by a template. Unlike a parameter, which supplies a value to the template, an output returns one, such as an EC2 instance’s public name. section 4.5.1

Declare outputs in the template’s Outputs section. Set each output’s Value directly or derive it from a resource with !Ref or !GetAtt; a Description can explain what the value represents. section 4.5.1

Expose an EC2 instance ID and public name

Outputs: ID: Value: !Ref Server Description: 'ID of the EC2 instance' PublicName: Value: !GetAtt 'Server.PublicDnsName' Description: 'Public name of the EC2 instance' section 4.5.1

Resources:
  # [...]
  InstanceSecurityGroup:                                   #1
    Type: 'AWS::EC2::SecurityGroup'
    Properties:
      GroupDescription: 'vm'
      VpcId: !Ref VPC
  Instance:                                                #2
    Type: 'AWS::EC2::Instance'
    Properties:
      ImageId: !FindInMap [RegionMap, !Ref 'AWS::Region', AMI]
      InstanceType: 't2.micro'
      IamInstanceProfile: !Ref InstanceProfile
      NetworkInterfaces:
      - AssociatePublicIpAddress: true
        DeleteOnTermination: true
        DeviceIndex: 0
        GroupSet:
        - !Ref InstanceSecurityGroup
        SubnetId: !Ref SubnetA
Outputs:
  InstanceId:                                              #3
    Value: !Ref Instance
    Description: 'EC2 Instance ID (connect via Session Manager)'
  CacheAddress:                                            #4
    Value: !GetAtt 'Cache.RedisEndpoint.Address'
    Description: 'Redis DNS name (resolves to a private IP address)'

See outputs that expose an EC2 instance ID and a cache address for use when connecting to a Redis cluster. section 11.1.2

How Amazon Web Services in Action, Third Edition: An in-depth guide to AWS covers it

How deep it goes
Defines the Outputs structure and demonstrates values derived with Ref and GetAtt, then shows retrieving outputs from a stack.
Examples
Uses an EC2 instance ID, public DNS name, and Redis cache address as output values.
What you can do after
Operate CloudFormation stacks by retrieving instance identifiers and service endpoints from their Outputs tab.
How it connects
Connects CloudFormation templates and resources with parameters and the Ref and GetAtt functions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Amazon Web Services in Action, Third Edition: An in-depth guide to AWS. The section links point to the parts of the book it draws on.