Skip to content

Instantly share code, notes, and snippets.

@ystoneman
Created February 27, 2022 21:09
Show Gist options
  • Save ystoneman/6bafcb995960b623cf8ef6b512ce8bcc to your computer and use it in GitHub Desktop.
Save ystoneman/6bafcb995960b623cf8ef6b512ce8bcc to your computer and use it in GitHub Desktop.
An example of how you can conditionally create resources in CloudFormation using Fn::If or !If for short.
AWSTemplateFormatVersion: 2010-09-09
Parameters:
SnsTopicArn:
Type: String
Default: ''
Description: >-
A pre-existing SNS topic to send S3 notifications to.
If not provided, a new topic will be created.
MyEmail:
Type: String
Description: The email address where you want to receive messages.
Conditions:
SnsTopicNotProvided: !Equals [!Ref SnsTopicArn, '']
Resources:
MyTopic:
Condition: SnsTopicNotProvided
Type: 'AWS::SNS::Topic'
MySubscription:
Type: AWS::SNS::Subscription
Properties:
Endpoint: !Ref MyEmail
Protocol: email
TopicArn: !If [SnsTopicNotProvided, !Ref MyTopic, !Ref SnsTopicArn]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment