-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yml
166 lines (160 loc) · 5.03 KB
/
template.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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Parameters:
EnvironmentParam:
Description: The Deployment Environment
Type: String
AllowedValues:
- 'npe'
- 'prod'
Default: 'npe'
Mappings:
S3Buckets:
npe:
Bucket1: "<bucket-name>" # Enter bucket name
prod:
Bucket1: "<bucket-name>" # Enter bucket name
Resources:
AclUpdateFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/
Handler: s3-acl-update.lambda_handler
Runtime: python3.8
Timeout: 900
Policies:
- Statement:
- Sid: S3ObjectPolicy
Effect: Allow
Action:
- s3:GetObjectAcl
- s3:PutObjectAcl
Resource:
# Add all the buckets you want to update here
- !Join [ '', ['arn:aws:s3:::', !FindInMap [S3Buckets, !Ref EnvironmentParam, "Bucket1"], '/*' ] ]
- Sid: S3ListPolicy
Effect: Allow
Action:
- s3:List*
Resource: '*'
- Sid: STSAssumeRole
Effect: Allow
Action:
- sts:AssumeRole
Resource: "arn:aws:iam::<Source_account_id:role/<source_account_role>" # Enter ARN specific to your source AWS account
- Sid: SSM
Effect: Allow
Action:
- ssm:DescribeParameters
- ssm:GetParameterHistory
- ssm:DescribeDocumentParameters
- ssm:GetParametersByPath
- ssm:GetParameters
- ssm:GetParameter
Resource: !Sub "arn:aws:ssm:us-west-2:${AWS::AccountId}:parameter/${EnvironmentParam}/slack_webhook/aws_alerts/LAMBDA_ALERTS"
Environment:
Variables:
slackChannel : "<slack_channel>" # Enter Slack channel name you created the webhook for
SSM_PARAMETER: !Sub "/${EnvironmentParam}/slack_webhook/aws_alerts/LAMBDA_ALERTS"
Events:
CloudwatchEvent:
Type: CloudWatchEvent
Properties:
Pattern:
source:
- aws.s3
detail-type:
- 'AWS API Call via CloudTrail'
detail:
eventSource:
- s3.amazonaws.com
eventName:
- PutObject
requestParameters:
bucketName:
# Add all the buckets you want to include in the cloudwatch event here
- !FindInMap [S3Buckets, !Ref EnvironmentParam, "Bucket1"]
Tags:
"billing-env" : !Ref EnvironmentParam
"billing-type" : "storage"
"Path" : "cloudformation/"
"StackName": !Sub '${AWS::StackName}'
CloudtrailLogBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub '${EnvironmentParam}-${AWS::StackName}-cloudtrail-log-bucket'
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
AccessControl: Private
PublicAccessBlockConfiguration:
BlockPublicAcls: TRUE
BlockPublicPolicy: TRUE
IgnorePublicAcls: TRUE
RestrictPublicBuckets: TRUE
Tags:
-
Key: "billing-env"
Value: !Ref EnvironmentParam
-
Key: "billing-type"
Value: "storage"
-
Key: "Path"
Value: "cloudformation/"
-
Key: "StackName"
Value: !Sub '${AWS::StackName}'
CloudtrailLogBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref CloudtrailLogBucket
PolicyDocument:
Statement:
-
Sid: S3GetBucketAclPolicy
Effect: Allow
Principal:
Service: cloudtrail.amazonaws.com
Action:
- s3:GetBucketAcl
Resource: !Sub '${CloudtrailLogBucket.Arn}'
-
Sid: S3PutObjectPolicy
Effect: Allow
Principal:
Service: cloudtrail.amazonaws.com
Action:
- s3:PutObject
Resource: !Sub '${CloudtrailLogBucket.Arn}/AWSLogs/${AWS::AccountId}/*'
Condition:
StringEquals:
s3:x-amz-acl: bucket-owner-full-control
S3ObjectCloudTrail:
DependsOn: CloudtrailLogBucketPolicy
Type: AWS::CloudTrail::Trail
Properties:
S3BucketName: !Ref CloudtrailLogBucket
IsLogging: TRUE
EventSelectors:
- IncludeManagementEvents: false
ReadWriteType: All
DataResources:
- Type: AWS::S3::Object
Values:
# Add all the buckets you want to update here
- !Join [ '', ['arn:aws:s3:::', !FindInMap [S3Buckets, !Ref EnvironmentParam, "Bucket1"], '/' ] ]
Tags:
-
Key: "billing-env"
Value: !Ref EnvironmentParam
-
Key: "billing-type"
Value: "storage"
-
Key: "Path"
Value: "cloudformation/"
-
Key: "StackName"
Value: !Sub '${AWS::StackName}'