-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathencrypt_s3_bucket.yaml
134 lines (117 loc) · 4.25 KB
/
encrypt_s3_bucket.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
AWSTemplateFormatVersion: 2010-09-09
Description: >
Builds AWS resources that checks the encryption status of a newly created S3 bucket.
An EventBridge rule is used to trigger a Lambda function, which then automatically adds default
encryption to the bucket.
Resources:
#----------------------------------
# EventBridge
#----------------------------------
CreateBucketEventRule:
Type: AWS::Events::Rule
Properties:
Description: Identifies S3 bucket creation events
State: ENABLED # by default
Targets:
- Arn: !GetAtt EncryptBucketFunction.Arn
Id: TargetEncryptBucketFunction
EventPattern:
source:
- aws.s3
detail-type:
- AWS API Call via CloudTrail
detail:
eventSource:
- s3.amazonaws.com
eventName:
- CreateBucket
#----------------------------------
# Lambda
#----------------------------------
EncryptBucketFunction:
Type: AWS::Lambda::Function
Properties:
Handler: index.lambda_handler
Runtime: python3.8
Role: !GetAtt EncryptBucketFunctionRole.Arn
Timeout: 300
Code:
ZipFile: |
import boto3
from botocore.exceptions import ClientError
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
"""Lambda function triggered by EventBridge rule that detects the
creation of an S3 bucket.
"""
try:
# gather data from event
curr_region = event['region']
s3 = boto3.client('s3', region_name=curr_region)
detail = event['detail']
bucket_name = detail['requestParameters']['bucketName']
account_id = detail['userIdentity']['accountId']
check_encryption_status(bucket_name, s3, account_id, logger)
except ClientError as e:
logger.info(
f'{e}. Enabling encryption on Bucket {bucket_name}...'
)
set_up_encryption(s3, bucket_name, account_id, logger)
check_encryption_status(bucket_name, s3, account_id, logger)
except Exception as e:
raise e
def check_encryption_status(bucket_name, s3, account_id, logger):
response = s3.get_bucket_encryption(
Bucket=bucket_name,
ExpectedBucketOwner=account_id
)
logger.info(f'Current encryption status: {response}')
return response
def set_up_encryption(s3, bucket_name, account_id, logger):
response = s3.put_bucket_encryption(
Bucket=bucket_name,
ServerSideEncryptionConfiguration={
'Rules': [
{
'ApplyServerSideEncryptionByDefault': {
'SSEAlgorithm': 'AES256'
}
},
]
},
ExpectedBucketOwner=account_id
)
return response
EncryptBucketFunctionRole:
Type: AWS::IAM::Role
Properties:
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'
Policies:
- PolicyName: CheckEncryptionStatusAndAddBucketEncryption
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- s3:GetEncryptionConfiguration
- s3:PutEncryptionConfiguration
Resource: "*"
PermissionForEventsToInvokeLambda:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref EncryptBucketFunction
Action: "lambda:InvokeFunction"
Principal: "events.amazonaws.com"
SourceArn: !GetAtt CreateBucketEventRule.Arn