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

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南