This repository has been archived by the owner on Dec 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
serverless.yml
191 lines (178 loc) · 6.08 KB
/
serverless.yml
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
service: ApplicationCostMonitoring
plugins:
- serverless-python-requirements
- serverless-iam-roles-per-function
- serverless-pseudo-parameters
- serverless-sam
custom:
stage: "${opt:stage, env:SLS_STAGE, 'dev'}"
profile: "${opt:aws-profile, env:AWS_PROFILE, env:AWS_DEFAULT_PROFILE, 'default'}"
log_level: "${env:LOG_LEVEL, 'INFO'}"
billing_bucket_name: 'applicationcostmonitoring-${self:provider.stage}-billing-reports-#{AWS::AccountId}'
cf-parameters:
# Changing tags on billing reports will alter the report schema which can:
# - break downstream analysis systems dependent on the schema.
# - result in item duplication as AWS will generate a new line item ID and
# this system has to continually reprocess 1st of month on every run.
# - make tracking an item across the month difficult due to change in ID.
#
# Options:
# - ERROR: Error out line item writer. Must remove schema state file or
# remove tags to continue processing.
# - CONTINUE: Just continue processing.
# - RECONCILE: Reprocess the entire report.
schema_change_handling: "${env:SCHEMA_CHANGE_HANDLING, 'CONTINUE'}"
provider:
name: aws
profile: ${self:custom.profile}
stage: ${self:custom.stage}
stackTags:
x-service: ${self:service}
x-stack: ${self:service}-${self:provider.stage}
x-app: ApplicationCostMonitoring
functions:
Publish:
handler: handlers/line-item-publisher.handler
description: "Ingest an S3 object and publish line items."
runtime: python3.6
memorySize: 512
timeout: 300
environment:
LOG_LEVEL: ${self:custom.log_level}
AWS_SNS_TOPIC:
Ref: BillingRecordsSnsTopic
SCHEMA_CHANGE_HANDLING:
Ref: SchemaChangeHandling
iamRoleStatements:
- Effect: "Allow"
Action:
- "s3:ListBucket"
Resource: "arn:aws:s3:::${self:custom.billing_bucket_name}"
- Effect: "Allow"
Action:
- "S3:GetObject"
- "S3:PutObject"
Resource: "arn:aws:s3:::${self:custom.billing_bucket_name}/*"
- Effect: "Allow"
Action:
- "SNS:Publish"
Resource:
- Ref: BillingRecordsSnsTopic
- Effect: "Allow"
Action:
- "Lambda:InvokeFunction"
Resource: "arn:aws:lambda:${self:provider.region}:#{AWS::AccountId}:function:${self:service}-${self:provider.stage}-Publish"
events:
- s3:
# NOTE: Need to use string here.
bucket: '${self:custom.billing_bucket_name}'
event: "s3:ObjectCreated:Put"
rules:
- suffix: ".gz"
- s3:
# NOTE: Need to use string here.
bucket: '${self:custom.billing_bucket_name}'
event: "s3:ObjectCreated:Put"
rules:
- suffix: ".zip"
resources:
Parameters:
SchemaChangeHandling:
Type: String
Description: 'Behavior by Publish function on report schema change.'
Default: '${self:custom.cf-parameters.schema_change_handling}'
Resources:
BillingRecordsSnsTopic:
Type: "AWS::SNS::Topic"
SnsTopicPolicy:
Type: "AWS::SNS::TopicPolicy"
Properties:
PolicyDocument: {
"Version": "2008-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": [
"SNS:GetTopicAttributes",
"SNS:SetTopicAttributes",
"SNS:AddPermission",
"SNS:RemovePermission",
"SNS:DeleteTopic",
"SNS:Subscribe",
"SNS:ListSubscriptionsByTopic",
"SNS:Publish",
"SNS:Receive"
],
"Resource": {
"Fn::Join": [
":",
[
"arn:aws:sns",
Ref: "AWS::Region",
Ref: "AWS::AccountId",
Fn::GetAtt: ["BillingRecordsSnsTopic", "TopicName"]
]
]
},
"Condition": {
"StringEquals": {
"AWS:SourceOwner": {
"Ref": "AWS::AccountId"
}
}
}
}
]
}
Topics:
- Ref: BillingRecordsSnsTopic
BillingS3BucketPolicy:
Type: "AWS::S3::BucketPolicy"
Properties:
Bucket:
Ref: "S3BucketApplicationcostmonitoring${self:provider.stage}billingreportsAWSAccountId"
PolicyDocument: {
"Version": "2008-10-17",
"Id": "Policy1335892530063",
"Statement": [
{
"Sid": "Stmt1335892150622",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::386209384616:root"
},
"Action": [
"s3:GetBucketAcl",
"s3:GetBucketPolicy"
],
"Resource": { "Fn::GetAtt": ["S3BucketApplicationcostmonitoring${self:provider.stage}billingreportsAWSAccountId", "Arn"] }
},
{
"Sid": "Stmt1335892526596",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::386209384616:root"
},
"Action": [
"s3:PutObject"
],
"Resource": { "Fn::Join": ["/", [{ "Fn::GetAtt": ["S3BucketApplicationcostmonitoring${self:provider.stage}billingreportsAWSAccountId", "Arn"] }, "*"]] }
}
]
}
Outputs:
BillingReportS3BucketName:
Description: "S3 bucket where billing reports are delivered to."
Value:
Ref: "S3BucketApplicationcostmonitoring${self:provider.stage}billingreportsAWSAccountId"
Export:
Name: "${self:service}-${self:provider.stage}-BillingReportS3BucketName"
BillingRecordsSnsTopicArn:
Description: "SNS topic ARN where billing records are published to."
Value:
Ref: BillingRecordsSnsTopic
Export:
Name: "${self:service}-${self:provider.stage}-BillingRecordsSnsTopicArn"