-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3-cleanup-automation.yaml
64 lines (58 loc) · 1.87 KB
/
s3-cleanup-automation.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
AWSTemplateFormatVersion: '2010-09-09'
Description: Clean S3 Bucket Every 3 Days
Resources:
S3BucketCleaningFunction:
Type: 'AWS::Lambda::Function'
Properties:
FunctionName: S3BucketCleaningFunction
Runtime: python3.11
Handler: index.lambda_handler
Role: !GetAtt LambdaExecutionRole.Arn
Code:
ZipFile: |
import boto3
def clean_s3_bucket(bucket_name):
s3 = boto3.resource('s3')
bucket = s3.Bucket(bucket_name)
for obj in bucket.objects.all():
obj.delete()
def lambda_handler(event, context):
bucket_name = 'your-bucket-name'
clean_s3_bucket(bucket_name)
return {
'statusCode': 200,
'body': 'S3 bucket cleaned successfully'
}
LambdaExecutionRole:
Type: 'AWS::IAM::Role'
Properties:
RoleName: LambdaExecutionRole
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: 'sts:AssumeRole'
Policies:
- PolicyName: S3BucketCleaningPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 's3:ListBucket'
- 's3:DeleteObject'
Resource:
- 'arn:aws:s3:::your-bucket-name'
- 'arn:aws:s3:::your-bucket-name/*'
S3BucketCleaningRule:
Type: 'AWS::Events::Rule'
Properties:
Name: S3BucketCleaningRule
Description: Rule to trigger S3 bucket cleaning every 3 days
ScheduleExpression: rate(3 days)
State: ENABLED
Targets:
- Arn: !GetAtt S3BucketCleaningFunction.Arn
Id: TargetFunction