enable MFA delete for s3 buckets

0

Is it possible to enable MFA delete for more than one S3 bucket? could it be possible to use a Python script with Boto3 for that?

Tal
已提问 1 个月前296 查看次数
2 回答
2

Hi,

please follow this detailled guidance to allow S3 MFA delete for a bucket: https://repost.aws/knowledge-center/s3-bucket-mfa-delete

You have a boto3 example for 1 bucket here: https://gist.github.com/jicowan/29868780e3c78ba7d48e1d501e58ef3f

If you iterate this exemple in a loop on the buckets that you want to enable, you will implement what you need.

Best,

Didier

profile pictureAWS
专家
已回答 1 个月前
profile picture
专家
已审核 1 个月前
profile picture
专家
已审核 1 个月前
1

Yes, it is possible to enable MFA delete for multiple S3 buckets using a Python script with Boto3. Here's a general outline of how you can achieve this :-

The sample of the script you can use

import boto3

# Initialize Boto3 S3 client
s3_client = boto3.client('s3')

# List of S3 bucket names
bucket_names = ['bucket1', 'bucket2', 'bucket3']

# MFA serial number and MFA token
mfa_serial = 'arn:aws:iam::123456789012:mfa/user'
mfa_token = '123456'

# Enable MFA delete for each bucket
for bucket_name in bucket_names:
    # Enable versioning for the bucket
    s3_client.put_bucket_versioning(
        Bucket=bucket_name,
        VersioningConfiguration={
            'Status': 'Enabled'
        }
    )

    # Enable MFA delete for the bucket
    s3_client.put_bucket_mfa(
        Bucket=bucket_name,
        MFA='{} {}'.format(mfa_serial, mfa_token),
        Enabled=True
    )

    print(f'MFA delete enabled for {bucket_name}')

print('All buckets updated successfully')

Replace 'bucket1', 'bucket2', 'bucket3' with the names of your S3 buckets, and 'arn:aws:iam::123456789012:mfa/user' with your MFA serial number and '123456' with your MFA token.

Run the Script: Execute the Python script, and it will enable MFA delete for each specified S3 bucket.

Please ensure that you have the necessary permissions to modify the versioning and MFA delete settings for the S3 buckets. Additionally, use caution when making changes to production resources.

profile picture
专家
已回答 1 个月前
profile picture
专家
已审核 1 个月前
  • It looks like there is no MFA in "s3_client.put_bucket". I have tried to add: s3_client.put_bucket_versioning( Bucket=bucket_name, VersioningConfiguration={ 'Status': 'Enabled', 'MFADelete': 'Enabled' } ) like in the "aws s3api put-bucket-versioning --bucket <bucket name> --versioning-configuration Status=Enabled,MFADelete=Enabled --mfa "<serial number> <mfa token>"" CLI command but didnt work as well

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则