aws::secretsmanager的cdk构造库
aws-cdk.aws-secretsmanager的Python项目详细描述
AWS Secrets Manager构造库
constsecretsmanager=require('@aws-cdk/aws-secretsmanager');
在堆栈中创建新机密
为了让secretsmanager自动生成新的机密值, 您可以开始以下操作:
// Default secretconstsecret=newsecretsManager.Secret(this,'Secret');secret.grantRead(role);newiam.User(this,'User',{password: secret.secretValue});// Templated secretconsttemplatedSecret=newsecretsManager.Secret(this,'TemplatedSecret',{generateSecretString:{secretStringTemplate: JSON.stringify({username:'user'}),generateStringKey:'password'}});newiam.User(this,'OtherUser',{userName: templatedSecret.secretValueFromJson('username').toString(),password: templatedSecret.secretValueFromJson('password')});
构造Secret
不允许指定SecretString
属性
在AWS::SecretsManager::Secret
资源中(因为这几乎总是
导致秘密以明文形式出现,并可能被承诺
你的源代码管理)。
如果需要使用预先存在的秘密,推荐的方法是手动。
在aws secretsmanager中设置机密并使用Secret.fromSecretArn
或者Secret.fromSecretAttributes
方法使其在cdk应用程序中可用:
constsecret=secretsmanager.Secret.fromSecretAttributes(scope,'ImportedSecret',{secretArn:'arn:aws:secretsmanager:<region>:<account-id-number>:secret:<secret-name>-<random-6-characters>',// If the secret is encrypted using a KMS-hosted CMK, either import or reference that key:encryptionKey,});
secretsmanager机密值只能在选定的属性集中使用。对于 属性列表,请参见the CloudFormation Dynamic References documentation。
旋转秘密
可以将轮换计划添加到机密中:
constfn=newlambda.Function(...);constsecret=newsecretsmanager.Secret(this,'Secret');secret.addRotationSchedule('RotationSchedule',{rotationLambda: fn,automaticallyAfter: Duration.days(15)});
关于如何实现lambda旋转函数,请参见Overview of the Lambda Rotation Function。
有关rds凭据循环,请参见aws-rds。