Complete a 3 Question Survey and Earn a re:Post Badge
Help improve AWS Support Official channel in re:Post and share your experience - complete a quick three-question survey to earn a re:Post badge!
如何在私有 VPC 中輪換 Secrets Manager 密碼?
我嘗試為 Amazon Virtual Private Cloud (Amazon VPC) 中的 AWS 服務輪換 AWS Secrets Manager 密碼。但是,作業失敗,並且 Amazon CloudWatch Logs 顯示 AWS Lambda 任務逾時。
簡短描述
Secrets Manager 無法輪換在 Amazon VPC 私有子網路中執行的 AWS 服務密碼,因為這些子網路無法存取網際網路。
解決方法
**重要:**在開始之前,請確定您已安裝並設定 AWS Command Line Interface (AWS CLI)。
**注意:**如果您在執行 AWS CLI 命令時收到錯誤,請參閱對 AWS CLI 錯誤進行疑難排解。此外,請確定您使用的是最新的 AWS CLI 版本。
依照下列指示設定 Amazon VPC 介面端點,以存取您的 Secrets Manager Lambda 函數和私有 Amazon Relational Database Service (Amazon RDS) 執行個體。在下列範例中,使用了 Amazon VPC 中名為 vpc-0abb11f5a28a8abe7 的私有 Aurora RDS 執行個體。
為 Secrets Manager VPC 端點、RDS 執行個體和 Lambda 輪換函數建立 SG
請遵循以下指示,使用 AWS CLI 建立安全群組 (SG)。
1.Secrets Manager Amazon VPC 端點的 SG:
**注意:**將 vpc-id vpc-0abb11f5a28a8abe7 替換為您的 VPC ID。
$ aws ec2 create-security-group --vpc-id vpc-0abb11f5a28a8abe7 --group-name SMVPCEndpointSG --description "secretsmanager VPCEndpoint SG" { "GroupId": "sg-vpc-endpoint" }
2.Lambda 輪換函數的安全群組:
$ aws ec2 create-security-group --vpc-id vpc-0abb11f5a28a8abe7 --group-name LambdaFunctionSG --description "Lambda Rotation Function SG" { "GroupId": "sg-lambda-function" }
3.(選用) 為 RDS 執行個體建立 SG:
**注意:**如果您的 RDS 執行個體只使用預設安全群組,則必須執行此步驟。
$ aws ec2 create-security-group --vpc-id vpc-0abb11f5a28a8abe7 --group-name RDSInstanceSG --description "RDS Instance SG" { "GroupId": "sg-rds-instance" }
將規則新增至 Amazon VPC 端點和 RDS 執行個體 SG
1.取得 VPC 的 CIDR 範圍:
$ aws ec2 describe-vpcs --vpc-ids vpc-0a05c93c7ef7a8a1c --query 'Vpcs[].CidrBlock' --output text 10.0.0.0/16
2.設定 Amazon VPC 端點的安全群組規則,以允許來自 VPC 連接埠 443 上的傳入流量:
$ aws ec2 authorize-security-group-ingress --group-id sg-vpc-endpoint --protocol tcp --port 443 --cidr 10.0.0.0/16
3.設定 RDS 執行個體 SG 以允許來自 Lambda 函數安全群組的傳入連線:
注意:
- 將 your-rds-security-group 替換為您的 SG (現有 SG 或選擇性 RDS 執行個體 SG)。
- 將 your-db-port 替換為資料庫設定使用的連接埠。
$ aws ec2 authorize-security-group-ingress --group-id your-rds-security-group --protocol tcp --port your-db-port --source-group sg-lambda-function
將 SGS 附加到 AWS 資源
1.如果您建立選擇性 RDS 執行個體 SG,請修改 RDS 執行個體組態:
**注意:**將 your-existing-rds-security-groups 替換為附加到 RDS 執行個體的一個或多個群組。
$ aws rds modify-db-instance --db-instance-identifier your-rds-instance --vpc-security-group-ids sg-rds-instance your-existing-rds-security-groups
2.依照指示更新 Lambda 函數組態:
$ aws lambda update-function-configuration --function-name your-lambda-function \ --vpc-config SubnetIds=subnet-076c28105d486f3bd,subnet-0af00c796ccdc725f,SecurityGroupIds=sg-lambda-function
為 Secrets Manager 服務建立 Amazon VPC 介面端點,並將其與 SG 建立關聯
請依照建立介面端點的指示進行操作:
**注意:**將 your-region 替換為您的 AWS 區域和用於 RDS 執行個體的子網路 ID。
$ aws ec2 create-vpc-endpoint --vpc-id vpc-0abb11f5a28a8abe7 --vpc-endpoint-type Interface \ --service-name com.amazonaws.your-region.secretsmanager --subnet-ids subnet-076c28105d486f3bd subnet-0af00c796ccdc725f \ --security-group-ids sg-vpc-endpoint
**重要:**您的 Amazon VPC 必須啟用 DNS 主機名稱和 DNS 解析屬性。如需詳細資訊,請參閱檢視和更新 VPC 的 DNS 支援。
確認 Secrets Manager 是否可以輪換密碼
1.請依照輪換 Secrets Manager 密碼的說明進行操作:
**注意:**將 your-secret 替換為您的 Secrets Manager 密碼。
$ aws secretsmanager rotate-secret --secret-id your-secret
Secrets Manager 會重試上一個輪換。
**注意:**由於之前嘗試輪換密碼失敗,您可能會收到類似以下內容的輸出:
An error occurred (InvalidRequestException) when calling the RotateSecret operation: A previous rotation isn't complete. That rotation will be reattempted.
2.在 AWS Lambda 主控台中監控該函數。如果輪換成功,則 Amazon CloudWatch 日誌串流會包含類似以下項目:
[INFO] 2019-10-22T07:59:32.627Z 96179023-5b67-4e98-a057-885f68bc69f2 finishSecret: Successfully set AWSCURRENT stage to version 175b5e38-341f-4cd0-8c58-2b1e49769642 for secret arn:aws:secretsmanager:your-region:your-account:secret:your-secret
3.擷取 Secrets Manager 密碼以確認其已成功輪換:
**注意:**將 your-secret-arn 替換為您的 Secrets Manager 密碼 ARN。
aws secretsmanager get-secret-value --secret-id your-secret-arn
**注意:**Secrets Manager 輪換功能會在背景非同步執行。輪換功能可能需要幾分鐘的時間才能完成。

相關內容
- 已提問 2 年前lg...
- 已提問 3 個月前lg...
- 已提問 9 個月前lg...
- 已提問 2 年前lg...