Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

AWS Lambda Automation and Python-automatically create S3 Bucket lifecycle

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Recently, it is often necessary to create some S3 Bucket for backup. Each new Bucket should be configured with lifecycle to automatically delete old data in order to save space and expenses.

Douzi wrote a simple Lambda function to implement automatically. Every time we create a Bucket, it will call the corresponding API,Cloudtrail to monitor the event and send it to Cloudwatch, and Cloudwatch will automatically call my function to create the lifecycle policy.

The following is a simple screenshot.

Create a new Cloudwatch Rule

Corresponding Lambda function

His default IAM already has access to Cloudwatch. I created a new Policy for S3 and assigned it to his IAM role so that the lambda function can access Cloudwatch and S3.

Here is the Python code

Import loggingimport boto3from botocore.exceptions import ClientErrorlifecycle_config_settings = {'Rules': [{' ID': 'Delete Rule',' Filter': {'Prefix':'}, 'Status':' Enabled', 'Expiration': {' Days':100}}]} def put_bucket_lifecycle_configuration (bucket_name Lifecycle_config): "" Set the lifecycle configuration of an Amazon S3 bucket: param bucket_name: string: param lifecycle_config: dict of lifecycle configuration settings: return: True if lifecycle configuration was set, otherwise False "" # Set the configuration S3 = boto3.client ('s3') try: s3.put_bucket_lifecycle_configuration (Bucket=bucket_name LifecycleConfiguration=lifecycle_config) except ClientError as e: return False return Truedef lambda_handler111 (event Context): # TODO implement test_bucket_name = event.get ('detail'). Get (' requestParameters'). Get ('bucketName') print (event) print (event.get (' detail'). Get ('requestParameters'). Get (' bucketName') success = put_bucket_lifecycle_configuration (test_bucket_name) Lifecycle_config_settings) if success: # logging.info ('The lifecycle configuration was set for {test_bucket_name}') print ('The lifecycle configuration was set for {test_bucket_name}')

It actually works, but when I create a new Bucket, it automatically calls this function to add policy.

Here is the log of Cloudwatch

This is the lifecycle policy of the newly built Bucket

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report