加密EBS卷更换客户托管密钥
问题描述
如果EBS卷不小心使用了AWS默认托管KMS密钥 (aws/ebs
) 加密,无法直接跨账号分享快照。需要转换为客户托管密钥加密后才能分享。
方法概述
通过快照复制重新加密的方式来解决:
- 创建客户托管KMS密钥
- 从原始卷创建快照
- 复制快照并使用客户托管密钥重新加密
- 从重新加密的快照创建新卷
- 可选:替换原始卷
详细操作步骤
步骤1:创建客户托管KMS密钥
# 创建客户托管KMS密钥
aws kms create-key \
--description "Customer managed key for EBS cross-account sharing" \
--region <AWS-REGION>
# 记录返回的KeyId,例如:12345678-1234-1234-1234-123456789012
步骤2:从原始卷创建快照
# 从使用AWS托管密钥的卷创建快照
aws ec2 create-snapshot \
--volume-id <ORIGINAL-VOLUME-ID> \
--description "Snapshot from AWS managed key encrypted volume" \
--tag-specifications 'ResourceType=snapshot,Tags=[{Key=Name,Value=original-snapshot}]' \
--region <AWS-REGION>
# 等待快照完成
aws ec2 describe-snapshots \
--snapshot-ids <SNAPSHOT-ID> \
--region <AWS-REGION>
步骤3:复制快照并重新加密
关键步骤:使用copy-snapshot
命令重新加密
# 复制快照并使用客户托管密钥重新加密
aws ec2 copy-snapshot \
--source-region <AWS-REGION> \
--source-snapshot-id <ORIGINAL-SNAPSHOT-ID> \
--description "Re-encrypted snapshot with customer managed key" \
--kms-key-id <CUSTOMER-MANAGED-KEY-ID> \
--region <AWS-REGION>
# 检查重新加密的快照状态
aws ec2 describe-snapshots \
--snapshot-ids <RE-ENCRYPTED-SNAPSHOT-ID> \
--region <AWS-REGION>
步骤4:从重新加密的快照创建新卷
# 从重新加密的快照创建新卷
aws ec2 create-volume \
--snapshot-id <RE-ENCRYPTED-SNAPSHOT-ID> \
--availability-zone <AVAILABILITY-ZONE> \
--volume-type gp3 \
--tag-specifications 'ResourceType=volume,Tags=[{Key=Name,Value=customer-key-encrypted-volume}]' \
--region <AWS-REGION>
步骤5:验证新卷的加密状态
# 验证新卷使用客户托管密钥加密
aws ec2 describe-volumes \
--volume-ids <NEW-VOLUME-ID> \
--region <AWS-REGION>
# 输出应显示:
# "Encrypted": true,
# "KmsKeyId": "arn:aws:kms:<region>:<account>:key/<customer-managed-key-id>"
步骤6:现在可以跨账号分享快照
# 配置KMS密钥策略允许目标账号访问
aws kms put-key-policy \
--key-id <CUSTOMER-MANAGED-KEY-ID> \
--policy-name default \
--policy file://cross-account-kms-policy.json \
--region <AWS-REGION>
# 分享重新加密的快照
aws ec2 modify-snapshot-attribute \
--snapshot-id <RE-ENCRYPTED-SNAPSHOT-ID> \
--attribute createVolumePermission \
--operation-type add \
--user-ids <TARGET-ACCOUNT-ID> \
--region <AWS-REGION>
详细步骤参考这篇 Blog: 加密EBS快照跨账号复制操作指南
脚本示例
如果有大量 EBS 卷需要替换加密密钥,可以参考以下示例脚本简化执行过程:
#!/bin/bash
# 变量定义
REGION="us-east-1"
ORIGINAL_VOLUME_ID="vol-0123456789abcdef0"
echo "步骤1: 创建客户托管KMS密钥"
CUSTOMER_KEY_ID=$(aws kms create-key \
--description "Customer managed key for EBS cross-account sharing" \
--region $REGION \
--query 'KeyMetadata.KeyId' \
--output text)
echo "创建的客户托管密钥ID: $CUSTOMER_KEY_ID"
echo "步骤2: 从原始卷创建快照"
ORIGINAL_SNAPSHOT_ID=$(aws ec2 create-snapshot \
--volume-id $ORIGINAL_VOLUME_ID \
--description "Snapshot from AWS managed key encrypted volume" \
--region $REGION \
--query 'SnapshotId' \
--output text)
echo "原始快照ID: $ORIGINAL_SNAPSHOT_ID"
echo "等待快照完成..."
aws ec2 wait snapshot-completed \
--snapshot-ids $ORIGINAL_SNAPSHOT_ID \
--region $REGION
echo "步骤3: 复制快照并重新加密"
RE_ENCRYPTED_SNAPSHOT_ID=$(aws ec2 copy-snapshot \
--source-region $REGION \
--source-snapshot-id $ORIGINAL_SNAPSHOT_ID \
--description "Re-encrypted snapshot with customer managed key" \
--kms-key-id $CUSTOMER_KEY_ID \
--region $REGION \
--query 'SnapshotId' \
--output text)
echo "重新加密的快照ID: $RE_ENCRYPTED_SNAPSHOT_ID"
echo "等待重新加密完成..."
aws ec2 wait snapshot-completed \
--snapshot-ids $RE_ENCRYPTED_SNAPSHOT_ID \
--region $REGION
echo "步骤4: 验证加密状态"
aws ec2 describe-snapshots \
--snapshot-ids $RE_ENCRYPTED_SNAPSHOT_ID \
--region $REGION \
--query 'Snapshots[0].{SnapshotId:SnapshotId,Encrypted:Encrypted,KmsKeyId:KmsKeyId}'
echo "转换完成!现在可以配置KMS密钥策略并分享快照了。"
注意事项
- 数据完整性:整个过程不会丢失数据,只是改变加密密钥
- 成本考虑:
- 快照存储费用(临时)
- 数据传输费用(复制过程)
- KMS密钥使用费用
- 时间考虑:大卷的快照和复制可能需要较长时间
- 停机时间:如果需要替换原始卷,可能需要停机
- 权限要求:需要KMS和EC2的相关权限
替换原始卷的步骤(可选)
如果需要为EC2实例完全替换使用 AWS 托管密钥加密的原始EBS卷:
# 1. 停止使用原始卷的实例
aws ec2 stop-instances --instance-ids <INSTANCE-ID>
# 2. 分离原始卷
aws ec2 detach-volume --volume-id <ORIGINAL-VOLUME-ID>
# 3. 附加新卷
aws ec2 attach-volume \
--volume-id <NEW-VOLUME-ID> \
--instance-id <INSTANCE-ID> \
--device /dev/sdf
# 4. 启动实例
aws ec2 start-instances --instance-ids <INSTANCE-ID>
# 5. 清理:删除原始卷和临时快照
aws ec2 delete-volume --volume-id <ORIGINAL-VOLUME-ID>
aws ec2 delete-snapshot --snapshot-id <ORIGINAL-SNAPSHOT-ID>
故障排除
常见错误
"InvalidParameterValue: Invalid KMS key"
- 确认KMS密钥ID正确
- 确认有使用该密钥的权限
"Snapshot copy failed"
- 检查源快照状态是否为completed
- 确认有足够的权限
"Volume creation failed"
- 检查可用区是否正确
- 确认快照状态为completed
参考文档
- Amazon EBS encryption examples - Re-encrypt an encrypted snapshot
- Copy an Amazon EBS snapshot
- Creating Keys - AWS KMS