AWS 密钥在 Cloudformation 上更新堆栈时轮换



我有一个Cloudformation脚本,它调用多个嵌套堆栈,例如创建数据库的堆栈。我首先在机密管理器中创建一个密钥,然后在数据库实例中使用它作为用户名和密码。我不想暂时启用自动密钥轮换。

我的问题是每次更新堆栈时,它都会生成一个新密钥,该密钥不会传播到数据库。因此,当我更新的 ECS 服务尝试连接到数据库时,它使用了错误的密码,因此它无法变得稳定,然后必须回滚所有内容。

为什么我的密码被轮换,即使我没有配置它?有没有办法避免这种情况?如果不能,我是否应该添加 AWS::SecretsManager::SecretTargetAttachment,以便至少将更改传播到数据库?

DBSecret:
  Type: "AWS::SecretsManager::Secret"
  Properties:
    Name: !Join ['', [!Ref ProductName, '-', !Ref EnvironmentName, '-', !Ref DBName, '-db-secret']]
    Description: Secret to be used for the database 
    KmsKeyId: !Ref KmsKeyId
    GenerateSecretString:
      SecretStringTemplate: !Join ['', ['{"username": "', !Ref DBUser , '"}']]
      GenerateStringKey: "password"
      PasswordLength: 30
      ExcludeCharacters: '"@/'
    Tags:
      - Key: Name
        Value: !Join ['', [!Ref ProductName, '-', !Ref EnvironmentName, '-', !Ref DBName, '-db-secret']]
PostgresDb:
  Type: AWS::RDS::DBInstance
  Properties:
    AllocatedStorage: !Ref DBAllocatedStorage
    AutoMinorVersionUpgrade: 'true'
    VPCSecurityGroups:
      - Ref: SecurityGroup
    DBName: !Ref DBName
    DBInstanceClass: !Ref DBInstanceClass
    DBSubnetGroupName: !Ref DBSubnetGroup
    Engine: postgres
    EngineVersion: !Ref DBVersion
    MasterUsername: !Join ['', ['{{resolve:secretsmanager:', !Ref DBSecret , ':SecretString:username}}']]
    MasterUserPassword: !Join ['', ['{{resolve:secretsmanager:', !Ref DBSecret , ':SecretString:password}}']]
    MultiAZ: !Ref DBMultiAZ
    StorageType: gp2
    BackupRetentionPeriod: 7
    StorageEncrypted: !Ref DBEncrypted
    # Only add the KMS key if the db is going to be encrypted
    KmsKeyId: !If [IsEncrypted, !Ref KmsKeyId, !Ref "AWS::NoValue"]
    MonitoringInterval: !If [HasEnhancedMonitoring, !Ref DBEnhancedMonitoringInterval, "0"]
    MonitoringRoleArn: !If [HasEnhancedMonitoring, !Ref DBMonitoringRoleARN, !Ref "AWS::NoValue"]
    Port: 5432
    Tags:
      - Key: Name
        Value: !Ref DBIdentifier
您需要

在单独的堆栈中分离数据库和数据库配置。数据库基础结构和数据库密码不是您经常更新的东西,而您的代码将处于不断变化的连续生命周期中。您的 ECS 服务可以使用 Fn::导入值从其他堆栈引用您的密码。在此处查看文档:

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-importvalue.html

最新更新