Skip to content

Commit 9e78988

Browse files
committed
Added template logic to conditionally create SNS Topics based on parameter configuration.
1 parent 712173e commit 9e78988

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

cloudformation.yaml

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
AWSTemplateFormatVersion: '2010-09-09'
22
Transform: AWS::Serverless-2016-10-31
33
Description: A simple backup process for EC2 and RDS datastores, tag your resources with 'MakeSnapshot' = 'True' (casesensitive) to have snapshots taken and managed
4+
Parameters:
5+
SuccessSNSTopicOption:
6+
Description: Supply an either '' (disabled), 'CreateSNS' (build new SNS for this stack), '<sns_arn>' (connect to existing Topic).
7+
Default: ""
8+
Type: String
9+
ErrorSNSTopicOption:
10+
Description: Supply an either '' (disabled), 'CreateSNS' (build new SNS for this stack), '<sns_arn>' (connect to existing Topic).
11+
Default: ""
12+
Type: String
13+
14+
Conditions:
15+
EnableSuccessSNSTopic: !Not [!Equals [!Ref SuccessSNSTopicOption, ""]] #
16+
EnableErrorSNSTopic: !Not [!Equals [!Ref ErrorSNSTopicOption, ""]]
17+
18+
CreateSuccessSNSTopic: !And # We only need to create the Success SNS when it is enabled and not supplied
19+
- Condition: EnableSuccessSNSTopic
20+
- !Equals [!Ref SuccessSNSTopicOption, "CreateSNS"]
21+
22+
CreateErrorSNSTopic: !And # We only need to create the Error SNS when it is enabled and not supplied
23+
- Condition: EnableErrorSNSTopic
24+
- !Equals [!Ref ErrorSNSTopicOption, "CreateSNS"]
25+
426
Resources:
527
LambdaExecutionRole:
628
Type: "AWS::IAM::Role"
@@ -62,8 +84,10 @@ Resources:
6284
Resource: "*"
6385
SuccessSNSTopic:
6486
Type: "AWS::SNS::Topic"
87+
Condition : CreateSuccessSNSTopic
6588
ErrorSNSTopic:
6689
Type: "AWS::SNS::Topic"
90+
Condition : CreateErrorSNSTopic
6791
BackupFunction:
6892
Type: AWS::Serverless::Function
6993
Properties:
@@ -82,10 +106,10 @@ Resources:
82106
- ''
83107
- - '{"period_label": "day", "period_format": "%a%H-%M", "keep_count": 14,'
84108
- '"arn": "'
85-
- Ref: SuccessSNSTopic
109+
- !If [EnableSuccessSNSTopic, !If [ CreateSuccessSNSTopic, Ref: SuccessSNSTopic, Ref: SuccessSNSTopicOption], '']
86110
- '", '
87111
- '"error_arn": "'
88-
- Ref: ErrorSNSTopic
112+
- !If [EnableErrorSNSTopic, !If [ CreateErrorSNSTopic, Ref: ErrorSNSTopic, Ref: ErrorSNSTopicOption], '']
89113
- '", '
90114
- '"ec2_region_name": "'
91115
- Ref: AWS::Region
@@ -103,10 +127,10 @@ Resources:
103127
- ''
104128
- - '{"period_label": "week", "period_format": "week-%U", "keep_count": 12,'
105129
- '"arn": "'
106-
- Ref: SuccessSNSTopic
130+
- !If [EnableSuccessSNSTopic, !If [ CreateSuccessSNSTopic, Ref: SuccessSNSTopic, Ref: SuccessSNSTopicOption], '']
107131
- '", '
108132
- '"error_arn": "'
109-
- Ref: ErrorSNSTopic
133+
- !If [EnableErrorSNSTopic, !If [ CreateErrorSNSTopic, Ref: ErrorSNSTopic, Ref: ErrorSNSTopicOption], '']
110134
- '", '
111135
- '"ec2_region_name": "'
112136
- Ref: AWS::Region

upload_lambda.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ set -e
55
REGION="ap-southeast-2"
66

77

8+
if [ -z "$BUCKET" ]; then
9+
echo "Please define the BUCKET variable before executing this script."
10+
exit -1
11+
fi
12+
13+
814
# Install dependancies locally, so they get included in the zip file
915
pip install -r requirements.txt -t lambda
1016

@@ -18,4 +24,5 @@ aws cloudformation deploy \
1824
--template-file generated-cloudformation.yaml \
1925
--stack-name "aws-backup-lambda" \
2026
--region ${REGION} \
21-
--capabilities CAPABILITY_IAM
27+
--capabilities CAPABILITY_IAM \
28+
--parameter-overrides SuccessSNSTopicOption= ErrorSNSTopicOption=CreateSNS

0 commit comments

Comments
 (0)