-
Notifications
You must be signed in to change notification settings - Fork 0
/
ECR_Notification_Template.yaml
196 lines (194 loc) · 5.52 KB
/
ECR_Notification_Template.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
AWSTemplateFormatVersion: "2010-09-09"
Description: CloudFormation template for Non-Supported ECR API Calls.
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
-
Label:
default: "Solution Configuration"
Parameters:
- Objective
-
Label:
default: "Amazon SNS Topic Configuration"
Parameters:
- SNSName
- SNSProtocol
- SNSEndpoint
-
Label:
default: "Amazon EventBridge Configuration"
Parameters:
- EventRuleName
- APICallName
ParameterLabels:
SNSName:
default: "Amazon SNS Topic Name:"
SNSProtocol:
default: "Amazon SNS Protocol:"
SNSEndpoint:
default: "Amazon SNS Endpoint (as per SNS Protocol):"
EventRuleName:
default: "Amazon Eventbridge Rule Name:"
APICallName:
default: "Amazon ECR CloudTrail Event Name/API Call ',' seperated:"
Objective:
default: "Solution Objective :"
Parameters:
SNSName:
Type: String
MinLength: 1
MaxLength: 256
AllowedPattern: ^[a-zA-Z0-9\-_]*$
SNSProtocol:
Type: String
Default: email
AllowedValues:
- http
- https
- email
- email-json
- sms
- sqs
- application
- lambda
SNSEndpoint:
Type: String
EventRuleName:
Type: String
MinLength: 1
MaxLength: 64
AllowedPattern: ^[\.\-_A-Za-z0-9]+
APICallName:
Type: CommaDelimitedList
Default: CreateRepository,DeleteRepository,PutImage,ReplicateImage
Objective:
Type: String
Default: SUCCESS
AllowedValues:
- SUCCESS
- FAILURE
Conditions:
StackObjective:
!Equals [!Ref Objective,"FAILURE"]
Resources:
EventRule0:
Type: AWS::Events::Rule
Properties:
EventBusName: default
EventPattern:
source:
- aws.ecr
detail-type:
- AWS API Call via CloudTrail
detail:
eventSource:
- ecr.amazonaws.com
eventName: !Ref APICallName
errorCode:
Fn::If:
- StackObjective
- [ exists: true ]
- [ exists: false ]
Name: !Ref EventRuleName
State: ENABLED
Targets:
- Id: !Sub "Id-${AWS::StackName}-${AWS::Region}-${AWS::AccountId}"
Arn: !Ref MySNSTopic
InputTransformer:
InputPathsMap:
Error_Code:
Fn::If:
- StackObjective
- $.detail.errorCode
- !Ref AWS::NoValue
Error_Message:
Fn::If:
- StackObjective
- $.detail.errorCode
- !Ref AWS::NoValue
Event_Name: $.detail.eventName
Region: $.region
Time: $.time
RepoARN: $.detail.resources[0].ARN
InputTemplate:
Fn::If:
- StackObjective
- >-
"Good Day User, This is to notify that '<Event_Name>' is failed in '<Region>' with '<Error_Code>' with message: '<Error_Message>'.This activity happened on '<Time>'"
- >-
"Good Day User, This is to notify that '<Event_Name>' is successful for '<RepoARN>' in '<Region>'. This activity happened on '<Time>'"
MySNSTopic:
Type: AWS::SNS::Topic
Properties:
Subscription:
- Protocol: !Ref SNSProtocol
Endpoint: !Ref SNSEndpoint
TopicName: !Ref SNSName
KmsMasterKeyId: !Ref KMSKey
KMSKey:
Type: AWS::KMS::Key
Properties:
Description: "AWS KMS key to encypt Amazon SNS topic"
EnableKeyRotation: true
KeyPolicy:
Version: 2012-10-17
Id: Iam-access
Statement:
- Sid: Enable IAM User Permissions
Effect: Allow
Principal:
AWS: !Sub 'arn:aws:iam::${AWS::AccountId}:root'
Action:
- "kms:CreateKey"
- "kms:CreateGrant"
- "kms:DescribeKey"
- "kms:EnableKey"
- "kms:EnableKeyRotation"
- "kms:ListGrants"
- "kms:ListKeyPolicies"
- "kms:ListKeys"
- "kms:ListResourceTags"
- "kms:ListRetirableGrants"
- "kms:UpdateKeyDescription"
- "kms:PutKeyPolicy"
- "kms:RevokeGrant"
- "kms:DisableKey"
- "kms:GetKeyPolicy"
- "kms:GetKeyRotationStatus"
- "kms:GetParametersForImport"
- "kms:TagResource"
- "kms:UntagResource"
- "kms:ScheduleKeyDeletion"
- "kms:CancelKeyDeletion"
Resource: '*'
- Sid: Allow event bridge
Effect: Allow
Principal:
Service: "events.amazonaws.com"
Action:
- kms:GenerateDataKey
- kms:Decrypt
Resource: "*"
MySNSTopicPolicy:
Type: AWS::SNS::TopicPolicy
Properties:
PolicyDocument :
Id: !Ref SNSName
Statement:
-
Action:
- "sns:GetTopicAttributes"
- "sns:SetTopicAttributes"
- "sns:AddPermission"
- "sns:RemovePermission"
- "sns:DeleteTopic"
- "sns:Subscribe"
- "sns:ListSubscriptionsByTopic"
- "sns:Publish"
Effect: Allow
Principal:
Service: "events.amazonaws.com"
Resource: !Ref MySNSTopic
Topics:
- !Ref MySNSTopic