user15927821
user15927821

Reputation: 13

How do I use lambda function to enable versioning on multiple S3 buckets?

I am tasked to write a lambda function that would enable versioning on multiple S3 buckets. How would I do this?

Upvotes: 1

Views: 486

Answers (2)

dossani
dossani

Reputation: 1948

The below lambda code in python v3.8 will enable versioning for all S3 buckets in your account.

import json
import boto3


def lambda_handler(event, context):
    s3 = boto3.client('s3')
    response = s3.list_buckets()

    for bucket in response['Buckets']:
        s3.put_bucket_versioning(Bucket=bucket["Name"],
                             VersioningConfiguration={
                                 'MFADelete': 'Disabled',
                                 'Status': 'Enabled',
                             },
        )
    
    print("Enabled versioning on bucket : ", bucket["Name"])
    
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

For more details, please refer to the AWS SDK for Python (Boto3) here.

Upvotes: 1

seniorwebdev0211
seniorwebdev0211

Reputation: 21

You can use serverless for backend. And you can deploy it to S3 buckets and AWS Lambda functions.

  1. Install serverless cli in your environment.
  2. Create serverless application by serverless command
  3. You can deploy it to S3 buckets and Lambda.
serverless deploy

You can reference more details here

Upvotes: 0

Related Questions