Get the App
SLTechnology News&Howtos  ›  Servers  › 

AWS Lambda Automation and Python-automatically create S3 Bucket lifecycle

Shulou Source: shulou.com Published: 2022-06-02 07:38:12 09月20日 Update

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

Tags: Functions times permissions events code backups actual screenshots effects data logs space beans allocation monitoring running configuration automation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Tech Info Linux Shulou Technology Redmi