-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yaml
219 lines (204 loc) · 6.22 KB
/
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Notify AWS Billing (NAB)
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: "Notification Settings"
Parameters:
- NotifyDaysInterval
- DisplayAccountId
- EmailAddress
- SlackWebhookUrl
- LineAccessToken
Parameters:
NotifyDaysInterval:
Type: Number
Default: 1
MinValue: 1
MaxValue: 31
Description: "Choose the interval of notification. (1-31)"
DisplayAccountId:
Type: String
Default: false
AllowedValues:
- true
- false
Description: "If you want to add the account ID to the notification message, set true. If not, set false."
EmailAddress:
Type: String
Default: ""
Description: "If you want to notify by Email, set Email Address. If not, leave it blank."
SlackWebhookUrl:
Type: String
Default: ""
NoEcho: true
Description: "If you want to notify by Slack, set Slack Webhook URL. If not, leave it blank."
LineAccessToken:
Type: String
Default: ""
NoEcho: true
Description: "If you want to notify by LINE, set LINE Notify Access Token. If not, leave it blank."
Conditions:
OnEmail: !Not [!Equals [!Ref EmailAddress, ""]]
OnSlack: !Not [!Equals [!Ref SlackWebhookUrl, ""]]
OnLine: !Not [!Equals [!Ref LineAccessToken, ""]]
OnAccountId: !Equals [!Ref DisplayAccountId, true]
Resources:
NABTopicToEmail:
Type: AWS::SNS::Topic
Condition : OnEmail
Properties:
TopicName: !Sub ${AWS::StackName}-nab-topic
Subscription:
- Endpoint: !Ref EmailAddress
Protocol: email
NABSecretForSlack:
Type: AWS::SecretsManager::Secret
Condition : OnSlack
Properties:
Description: "Slack Webhook URL"
SecretString: !Sub '{"info": "${SlackWebhookUrl}"}'
Name: !Sub /${AWS::StackName}-nab-secret/slack
NABSecretForLine:
Type: AWS::SecretsManager::Secret
Condition : OnLine
Properties:
Description: "LINE Access Token"
SecretString: !Sub '{"info": "${LineAccessToken}"}'
Name: !Sub /${AWS::StackName}-nab-secret/line
NABFunctionRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub ${AWS::StackName}-nab-function-role
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: "sts:AssumeRole"
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
NABCEAccessPolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
ManagedPolicyName: !Sub ${AWS::StackName}-nab-ce-access-policy
Roles:
- !Ref NABFunctionRole
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "ce:GetCostAndUsage"
Resource: "*"
NABEmailPolicy:
Type: AWS::IAM::ManagedPolicy
Condition: OnEmail
Properties:
ManagedPolicyName: !Sub ${AWS::StackName}-nab-email-policy
Roles:
- !Ref NABFunctionRole
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "sns:publish"
Resource: !Ref NABTopicToEmail
NABSlackPolicy:
Type: AWS::IAM::ManagedPolicy
Condition: OnSlack
Properties:
ManagedPolicyName: !Sub ${AWS::StackName}-nab-slack-policy
Roles:
- !Ref NABFunctionRole
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "secretsmanager:GetSecretValue"
Resource: !Ref NABSecretForSlack
NABLinePolicy:
Type: AWS::IAM::ManagedPolicy
Condition: OnLine
Properties:
ManagedPolicyName: !Sub ${AWS::StackName}-nab-line-policy
Roles:
- !Ref NABFunctionRole
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "secretsmanager:GetSecretValue"
Resource: !Ref NABSecretForLine
NABFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-nab-function
Handler: index.lambda_handler
Runtime: python3.12
Role: !GetAtt NABFunctionRole.Arn
Timeout: 60
LoggingConfig:
LogFormat: JSON
ApplicationLogLevel: INFO
SystemLogLevel: INFO
Layers:
- arn:aws:lambda:ap-northeast-1:133490724326:layer:AWS-Parameters-and-Secrets-Lambda-Extension:4
Environment:
Variables:
ACCOUNT_ID: !If
- OnAccountId
- !Sub ${AWS::AccountId}
- !Ref AWS::NoValue
MAIL_TOPIC_ARN: !If
- OnEmail
- !Ref NABTopicToEmail
- !Ref AWS::NoValue
SLACK_SECRET_NAME: !If
- OnSlack
- !Sub /${AWS::StackName}-nab-secret/slack
- !Ref AWS::NoValue
LINE_SECRET_NAME: !If
- OnLine
- !Sub /${AWS::StackName}-nab-secret/line
- !Ref AWS::NoValue
CodeUri: function/
NABFunctionScheduler:
Type: AWS::Scheduler::Schedule
Properties:
Name: !Sub ${AWS::StackName}-nab-function-scheduler
Description: "Start Notify AWS Billing Function"
ScheduleExpression: !Sub cron(0 9 */${NotifyDaysInterval} * ? *)
ScheduleExpressionTimezone: "Asia/Tokyo"
FlexibleTimeWindow:
Mode: "OFF"
State: ENABLED
Target:
Arn: !GetAtt NABFunction.Arn
RoleArn: !GetAtt NABFunctionSchedulerRole.Arn
NABFunctionSchedulerRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub ${AWS::StackName}-nab-function-scheduler-role
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: scheduler.amazonaws.com
Action: "sts:AssumeRole"
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaRole
Outputs:
FunctionArn:
Description: "Lambda Function ARN"
Value: !GetAtt NABFunction.Arn
SchedulerArn:
Description: "Scheduler ARN"
Value: !GetAtt NABFunctionScheduler.Arn