如何在私有 VPC 中轮换 Secrets Manager 密钥?

3 分钟阅读
0

我尝试在 Amazon Virtual Private Cloud(Amazon VPC)中为 AWS 服务轮换 AWS Secrets Manager 密钥。然而,操作失败,并且 Amazon CloudWatch Logs 显示 AWS Lambda 任务已超时。

简短描述

Secrets Manager 无法为在 Amazon VPC 私有子网中运行的 AWS 服务轮换密码,因为这些子网不具有访问互联网的权限。

解决方法

**重要事项:**在开始之前,请确保您已经安装配置了 AWS 命令行界面 (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 轮换函数创建安全组

按照以下说明使用 AWS CLI 创建安全组 (SG)

1.为 Secrets Manager Amazon VPC 端点创建安全组:

**注意:**请将 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 实例创建安全组:

**注意:**如果您的 RDS 实例仅使用默认安全组,则此步骤是必需的。

$ aws ec2 create-security-group --vpc-id vpc-0abb11f5a28a8abe7 --group-name RDSInstanceSG --description "RDS Instance SG"
{
    "GroupId": "sg-rds-instance"
}

向 Amazon VPC 端点和 RDS 实例安全组添加规则

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 实例安全组以允许来自 Lambda 函数安全组的入站连接:

注意:

  • 请将 your-rds-security-group 替换为您的安全组(现有安全组或可选 RDS 实例安全组)。
  • 请将 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

将安全组附加到 AWS 资源

1.如果您创建了可选 RDS 实例安全组,请修改 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 接口端点并将其与安全组关联

按照说明创建接口端点

**注意:**请将 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 轮换函数在后台异步运行。轮换函数可能需要几分钟才能完成。

AWS 官方
AWS 官方已更新 2 年前