-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudformation-template.yaml
273 lines (273 loc) · 8.94 KB
/
cloudformation-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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
AWSTemplateFormatVersion: "2010-09-09"
Metadata:
Author: Moulick Aggarwal
Github: https://github.com/Moulick/kinesis2elastic
License: GNU General Public License v3.0
Description: This template creates all the necessary resources to setup a cross account log delivery for a given account.
Parameters:
FirehoseName:
Type: String
Description: Name of the Firehose DeliveryStream that'll buffer the logs
MinLength: "1"
MaxLength: "64"
Default: "dev"
AllowedPattern: "[a-zA-Z0-9_.-]+"
SourceAccountID:
Type: Number
Description: Source account ID that is allowed to put a PutSubscriptionFilter and hence send logs to the Firehose DeliveryStream. This is a AWS Account ID
Default: 111111111111
MinValue: 000000000000
MaxValue: 999999999999
DestinationUrl:
Type: String
Description: The destination url for the logs. Has to be https
Default: "https://example.com/firehose"
AllowedPattern: "https://.*"
DestinationCredentials:
Type: String
Description: The credentials for the destination url
Default: "YWRtaW46YWRtaW4="
Encryption:
Type: String
Description: Whether to encrypt the logs or not
AllowedValues:
- true
- false
Default: false
Conditions:
Encryption: !Equals
- !Ref Encryption
- true
Resources:
FirehoseRole:
Type: AWS::IAM::Role
Properties:
Description: Allow Firehose to send data to S3 and talk to KMS
RoleName: !Join
- "-"
- - !Ref FirehoseName
- FirehoseRole
- !Ref AWS::Region
AssumeRolePolicyDocument:
Statement:
Effect: Allow
Principal:
Service: firehose.amazonaws.com
Action: sts:AssumeRole
Condition:
StringEquals:
sts:ExternalId: !Ref AWS::AccountId
Policies:
- PolicyName: FirehosePolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- s3:AbortMultipartUpload
- s3:GetBucketLocation
- s3:GetObject
- s3:ListBucket
- s3:ListBucketMultipartUploads
- s3:PutObject
Resource:
- !GetAtt Bucket.Arn
- !Sub
- ${BUCKET}/*
- BUCKET: !GetAtt Bucket.Arn
- !If
- Encryption
- Effect: Allow
Action:
- kms:GenerateDataKey
- kms:Decrypt
Resource: !If
- Encryption
- !GetAtt KMS.Arn
- !Ref AWS::NoValue
Condition:
StringEquals:
kms:ViaService: !Sub s3.${AWS::Region}.${AWS::URLSuffix}
StringLike:
kms:EncryptionContext:aws:s3:arn:
- !GetAtt Bucket.Arn
- !Sub
- ${BUCKET}/*
- BUCKET: !GetAtt Bucket.Arn
- !Ref 'AWS::NoValue'
- Effect: Allow
Action:
- logs:PutLogEvents
Resource:
- !Sub
- arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:${LOG_GROUP}:log-stream:*
- LOG_GROUP: !Ref LogGroup
Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Join [ "-", [ !Ref FirehoseName, !Ref AWS::AccountId ] ]
AccelerateConfiguration:
AccelerationStatus: Enabled
MetricsConfigurations:
- Id: EntireBucket
BucketEncryption: !If
- Encryption
- ServerSideEncryptionConfiguration:
- BucketKeyEnabled: True
ServerSideEncryptionByDefault:
SSEAlgorithm: aws:kms
KMSMasterKeyID: !If
- Encryption
- !GetAtt KMS.Arn
- !Ref AWS::NoValue
- !Ref AWS::NoValue
PublicAccessBlockConfiguration:
BlockPublicAcls: True
BlockPublicPolicy: True
IgnorePublicAcls: True
RestrictPublicBuckets: True
Firehose:
Type: AWS::KinesisFirehose::DeliveryStream
Properties:
DeliveryStreamType: DirectPut
DeliveryStreamName: !Ref FirehoseName
DeliveryStreamEncryptionConfigurationInput: !If
- Encryption
- KeyType: CUSTOMER_MANAGED_CMK
KeyARN: !If
- Encryption
- !GetAtt KMS.Arn
- !Ref AWS::NoValue
- !Ref 'AWS::NoValue'
HttpEndpointDestinationConfiguration:
EndpointConfiguration:
Name: kinesis2elastic
AccessKey: !Ref DestinationCredentials
Url: !Ref DestinationUrl
BufferingHints:
IntervalInSeconds: 60 # 60-900
SizeInMBs: 1 # 1-128
CloudWatchLoggingOptions:
Enabled: true
LogGroupName: !Ref LogGroup
LogStreamName: !Ref LogStreamDestinationDelivery
RequestConfiguration:
CommonAttributes:
- AttributeName: "Moulick"
AttributeValue: "True"
ContentEncoding: NONE # GZIP | NONE
RoleARN: !GetAtt FirehoseRole.Arn
RetryOptions:
DurationInSeconds: 300 # 0-7200
S3BackupMode: AllData # AllData | FailedDataOnly
S3Configuration:
BucketARN: !GetAtt Bucket.Arn
RoleARN: !GetAtt FirehoseRole.Arn
BufferingHints:
SizeInMBs: 1 # 1-128
IntervalInSeconds: 60 # 60-900
CompressionFormat: UNCOMPRESSED # GZIP | HADOOP_SNAPPY | Snappy | UNCOMPRESSED | ZIP
Prefix: SUCCESS/
ErrorOutputPrefix: ERROR/
EncryptionConfiguration: !If
- Encryption
- KMSEncryptionConfig:
AWSKMSKeyARN: !If
- Encryption
- !GetAtt KMS.Arn
- !Ref AWS::NoValue
- NoEncryptionConfig: NoEncryption
CloudWatchLoggingOptions:
Enabled: true
LogGroupName: !Ref LogGroup
LogStreamName: !Ref LogStreamBackupDelivery
CWLtoKinesisFirehoseRole:
Type: AWS::IAM::Role
Properties:
Description: Allow CloudWatch Logs to send data to Kinesis Firehose
RoleName: !Join
- "-"
- - !Ref FirehoseName
- CWLtoKinesisFirehoseRole
- !Ref AWS::Region
AssumeRolePolicyDocument:
Statement:
Effect: Allow
Principal:
Service: !Sub logs.${AWS::Region}.${AWS::URLSuffix}
Action: sts:AssumeRole
Policies:
- PolicyName: Permissions-Policy-For-CWL
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- firehose:*
Resource:
- !Sub arn:aws:firehose:${AWS::Region}:${AWS::AccountId}:*
- !GetAtt Firehose.Arn
LogsDestination:
Type: AWS::Logs::Destination
Properties:
DestinationName: !Ref FirehoseName
DestinationPolicy: !Sub
- |
{
"Version" : "2012-10-17",
"Statement" : [
{
"Sid" : "",
"Effect" : "Allow",
"Principal" : {
"AWS" : "${SourceAccountID}"
},
"Action" : "logs:PutSubscriptionFilter",
"Resource" : "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:destination:${self}"
}
]
}
- SourceAccountID: !Ref SourceAccountID
self: !Ref FirehoseName
RoleArn: !GetAtt CWLtoKinesisFirehoseRole.Arn
TargetArn: !GetAtt Firehose.Arn
KMS:
Type: AWS::KMS::Key
Condition: Encryption
Properties:
Description: KMS Key to encrypt the data in the Firehose and Bucket
Enabled: True
EnableKeyRotation: True
KeyPolicy:
Version: 2012-10-17
Id: key-default-1
Statement:
- Sid: Enable IAM User Permissions
Effect: Allow
Principal:
AWS: !Sub 'arn:aws:iam::${AWS::AccountId}:root'
Action: 'kms:*'
Resource: '*'
KeySpec: SYMMETRIC_DEFAULT
LogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Join [ "/", [ "/aws/kinesisfirehose", !Ref FirehoseName ] ]
RetentionInDays: 30
LogStreamDestinationDelivery:
Type: AWS::Logs::LogStream
Properties:
LogGroupName: !Ref LogGroup
LogStreamName: DestinationDelivery
LogStreamBackupDelivery:
Type: AWS::Logs::LogStream
Properties:
LogGroupName: !Ref LogGroup
LogStreamName: BackupDelivery
Outputs:
Destination:
Value: !GetAtt LogsDestination.Arn
Description: Arn of the Cloudwatch Logs Destination. Used to create the subscritpion filter.
Firehose:
Value: !GetAtt Firehose.Arn
Description: Arn of the Firehose.