在Cloudformation上更新堆栈时旋转了AWS机密

时间:2019-06-12 14:00:20

标签: amazon-web-services amazon-cloudformation amazon-rds aws-secrets-manager

我有一个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

1 个答案:

答案 0 :(得分:0)

您需要在单独的堆栈中分离数据库和数据库配置。数据库基础结构和db密码不是经常更新的内容,而代码将处于不断变化的生命周期中。您的ECS服务可以使用Fn :: ImportValue从另一个堆栈中引用您的密码。在此处检查文档:

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