Package: mbonig.secret
@matthewbonig/secretsThe AWS Secrets ManagerSecretconstruct has a big footgun,if you update thegenerateSecretStringproperty, the secret gets recreated!This isn't exactly a flaw of the CDK, but of how CloudFormation handles this property.So, this library has a single construct with a single intention, to allow you to update thegenerateSecretStringproperty without recreating the secret.Design PhilosophySecrets are the AWS-preferred method for passing configuration values to runtime components. However, with the existing
secret it's painful to manage the contents of a secret over the life of a project. You can't provide all your configuration
values directly in yourgenerateSecretStringproperty because you'll then likely expose sensitive
IaC. However, you also can't just leave this field completely blank because it will either make post-deployment changes
to the secret more error-prone (as someone may manually enter in field names incorrectly) or it will make it impossible
for some services to work at all until a post-deployment change is made, like ECS.So, this construct is designed to make it so you can update thegenerateSecretStringproperty without recreating the secret.
This allows you to define the basic shape of a secret through your IaC ensuring that post-deployment updates are done
with fewer errors.It is a fundamental principle of this construct that:The values stored in secrets are required to be updated manually outside of the IaC process.The shape of the secret is defined in the IaC process.Changes to the shape of the secret are made through the IaC process.Changes to the shape and values of the secret in IaC do not affect fields and values that were not changed in IaC.Changes made to the value of the secret through an outside process are retained unless explicitly changed through IaC.[!WARNING]
If you have an existing aws-cdk-lib/aws_secretsmanager.Secret, you can replace it with this new construct. However,
when you update your stack the existing value will be completely wiped out and re-created using the new construct.
Make a backup of your secret before using this new construct on an existing secret.Usageimport{Secret}from'@matthewbonig/secrets';//....newSecret(this,'MySecret',{generateSecretString:{generateStringKey:'password',secretStringTemplate:JSON.stringify({username:'my-username',password:'some-password',}),},});This is a drop-in replacement, and has the same API surface area as the originalaws_secretsmanager.Secretconstruct. The difference is that thegenerateSecretStringproperty can be updated without recreating the secret.There are a few different scenarios when you make changes to thegenerateSecretStringproperty:No change: If you don't change thegenerateSecretStringproperty, the secret will not be updated.Change: If you change thegenerateSecretStringproperty, by adding a new property, the secret will be updated, and only the new property will be changed. For example, if you add 'api-key' to the object then the secret will get the additional 'api-key' field added to it and all other properties will not be affected.Change: If you update the value of an existing property on thegenerateSecretStringproperty, the secret will be updated, and only the updated property will be changed. For example, if you change the value of 'password' to a new value, then only the 'password' property will be updated on the secret.Change: If you change thegenerateStringKeyfield, then a new field will be added to the secret. The previously generated field will not be removed from the secret.Change: If you change any of the properties that define how thegenerateStringKeyshould be generated, like theexcludePunctuationproperty, then the field specified by thegenerateStringKeywill be regenerated with the new parameters and the other fields will remain unchanged.Remove: If you remove a property from thegenerateSecretStringproperty, the secret will be updated, and the property will be removed from the secret and all other properties will remain unchanged.ExampleLet's begin with a simple secret with two fields,usernameandpassword.newSecret(this,'MySecret',{generateSecretString:{generateStringKey:'password',secretStringTemplate:JSON.stringify({username:'my-username',password:'some-password',}),},});You can update the fields manually. Let's say you update the password field:{"username":"my-username","password":"new-password"}Later, you update the Secret and add a new field to thegenerateSecretStringproperty:newSecret(this,'MySecret',{generateSecretString:{generateStringKey:'password',secretStringTemplate:JSON.stringify({username:'my-username',password:'some-password',someNewField:'some-new-value',}),},});When deployed, thesomeNewFieldwill be added to the secret but the other fields will remain unchanged.Later on, you can also update thegenerateSecretStringproperty and update an existing field:newSecret(this,'MySecret',{generateSecretString:{generateStringKey:'password',secretStringTemplate:JSON.stringify({username:'my-username',password:'some-new-password',someNewField:'some-new-value',}),},});Now the value forpasswordwill be updated to the new value without changing the values ofusernameorsomeNewField.Finally, you can remove a field from thegenerateSecretStringproperty, likesomeNewField:newSecret(this,'MySecret',{generateSecretString:{generateStringKey:'password',secretStringTemplate:JSON.stringify({username:'my-username',password:'some-new-password',}),},The value will be removed from the secret without affecting the other fields.