【问题标题】:How to enable server side encryption for multiple S3 buckets using AWS CLI?如何使用 AWS CLI 为多个 S3 存储桶启用服务器端加密?
【发布时间】:2021-12-17 14:32:59
【问题描述】:
我有大约 100 个 S3 存储桶,我想使用 AWS CLI 为这些存储桶启用 SSE 加密。
为此,我浏览了一些 AWS 文档。似乎我可以使用以下命令:
aws s3api 存储桶加密
--bucket 我的桶
--server-side-encryption-configuration '{"Rules": [{"ApplyServerSideEncryptionByDefault": {"SSEAlgorithm": "AES256"}}]}'
但我想排除几个桶。我该怎么做?
【问题讨论】:
标签:
linux
amazon-web-services
amazon-s3
encryption
aws-cli
【解决方案1】:
你说你在 Linux 上运行,所以你可以使用 shell 循环。
首先,将存储桶列表存储在文件中(sed 命令是必要的,因为aws s3 ls 将时间戳信息添加到输出中):
aws s3 ls | sed -e 's/.* //' > /tmp/$$
然后,编辑此文件并删除您不想更新的所有存储桶。
最后,循环运行你的命令:
for b in $(cat /tmp/$$) ; do YOUR_COMMAND_HERE ; done
【解决方案2】:
一般来说,这应该小心执行,因为它会影响除被排除的桶之外的所有桶
确保你知道自己在做什么。
#!/bin/bash
# excluded buckets list
excluded_list="my-excluded-bucket-1|my-excluded-bucket-2|my-excluded-bucket-3"
aws s3 ls | awk '{print $NF}' | grep -vE "$excluded_list"
echo "#############################################################"
echo "# WARNING: The above s3 buckets encryption will be updated. #"
echo "#############################################################"
read -p "Continue (y/n)?" choice
case "$choice" in
y|Y ) echo "yes";;
n|N ) echo "no";exit;;
* ) echo "invalid";exit;;
esac
for b in $(aws s3 ls | awk '{print $NF}' | grep -vE "$excluded_list"); do
aws s3api put-bucket-encryption --bucket $b --server-side-encryption-configuration '{"Rules": [{"ApplyServerSideEncryptionByDefault": {"SSEAlgorithm": "AES256"}}]}'
done