Sub topic 8
Exposing values with Outputs
This chapter assumes you can read a YAML template, know the Ref and Fn::GetAtt functions, and have created a stack with the AWS CLI. The Outputs section declares values that a stack makes available after it is created. Those values can be read by people, scripts, or other stacks.
What an output is
Each entry in Outputs has a logical name and a Value. It can also have a Description and an Export. The Value is usually built with intrinsic functions, because the useful values (ARNs, URLs, generated names) are only known after CloudFormation creates the resources.
Outputs:
BucketArn:
Description: ARN of the application bucket
Value: !GetAtt AppBucket.Arn
Export:
Name: !Sub "${AWS::StackName}-BucketArn"The section has these rules:
- A template can declare up to 200 outputs.
- Output logical names must be alphanumeric and unique within the template.
Valuecan useRef,Fn::GetAtt,Fn::Sub,Fn::Join,Fn::If, and similar functions. It can also use pseudo parameters.Descriptionis optional but helps readers.
Reading outputs
Once the stack reaches a complete state, you can read its outputs from the CLI.
aws cloudformation describe-stacks \
--stack-name demo \
--query "Stacks[0].Outputs"
aws cloudformation describe-stacks \
--stack-name demo \
--query "Stacks[0].Outputs[?OutputKey=='BucketArn'].OutputValue" \
--output textThe second command uses a JMESPath filter, which is handy in deployment scripts. The console shows the same data on the stack's Outputs tab.
Exports
Adding Export with a Name publishes the value so other stacks in the same account and Region can import it with Fn::ImportValue. Export names must be unique within an account and Region, so including ${AWS::StackName} in the name is a common way to avoid collisions.
Resources:
ReaderPolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: s3:GetObject
Resource: !Join
- ""
- - !ImportValue demo-BucketArn
- "/*"An export is not just a label. While any stack imports it, CloudFormation refuses to delete the exporting stack. It also refuses to change or remove the exported value. Plan for that before you export something that will need to change.
Conditional outputs
An output can carry a Condition, so it appears only when that condition is true. Use this when the value refers to a resource that exists only in some environments.
Conditions:
IsProd: !Equals [!Ref EnvName, prod]
Outputs:
ReplicaArn:
Condition: IsProd
Value: !GetAtt ReplicaBucket.ArnCommon errors
- Unsupported attribute.
Fn::GetAttfails if the resource type does not expose that attribute. Check the resource's documentation. - Duplicate export name. Creation fails if another stack in the same Region already exports that name.
- Changing an imported export. An update fails while another stack uses the export. Update or delete the consumers first.
- Sensitive data. Outputs are visible to anyone who can describe the stack. Never output secrets.
Exercise
A stack exports net-VpcId. Another stack imports it. You try to delete the first stack and the request fails. Why, and what must you do?
Show answer
CloudFormation blocks deleting a stack whose exports are in use. Remove the Fn::ImportValue references from the consuming stack (or delete that stack), then delete the exporting stack.
Exercise
Write the CLI command that prints only the value of the output BucketArn from the stack demo as plain text.
Show answer
aws cloudformation describe-stacks --stack-name demo --query "Stacks[0].Outputs[?OutputKey=='BucketArn'].OutputValue" --output text