aws / aws/aws-cdk

aws-secretsmanager: Include versionId / versionStage in secrets in Cloudformation templates to be able to force changes

Open
#23,645 6 comments 22 reactions 0 assignees View on GitHub
@aws-cdk/aws-secretsmanager feature-request p3
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the feature

Hi,

I would like to propose a change to the existing way the `SecretValue` is synthesised from CDK code to Cloudformation templates.

Currently when updating a secret in the Secretsmanager, it generates a versionId for that secret. This versionId can be used in Cloudformation templates dynamically. Cloudformation can detect changes to the versionId and deploy resources accordingly when resources are updated.

[AWS Documentation about this subject:](https://aws.amazon.com/premiumsupport/knowledge-center/cloudformation-stack-update-secrets/)

When looking at one of the main ways to add secrets to your Cloudformation templates through CDK, it is said that the following needs to be done when changing secrets: `If you rotate the value in the Secret, you must also change at least one property on the resource where you are using the secret, to force CloudFormation to re-read the secret.`

Full snippit:
```js
/**
* Creates a `SecretValue` with a value which is dynamically loaded from AWS Secrets Manager.
*
* If you rotate the value in the Secret, you must also change at least one property
* on the resource where you are using the secret, to force CloudFormation to re-read the secret.
*
* @param secretId The ID or ARN of the secret
* @param options Options
*/
static secretsManager(secretId: string, options?: SecretsManagerSecretOptions): SecretValue;
```

However this seems unnecessary. If CDK would include the versionId behind the secret reference (like Cloudformation currently already supports) the change of version would automatically be detected.

There of course might be something I am overlooking here, but I could not find much about this versionId property in the CDK spec. You are able to specify it when retrieving secrets using `SecretsManagerSecretOptions` but then it still is not added to the Cloudformation template.

### Use Case

My main use case for this would be to make it easier to rotate secrets.

With the current method I have to:

- Update the secret in Secretmanager
- Modify arbitrary CDK property in the resource
- Deploy
- Modify arbitrary CDK property in the resource again back to normal
- Deploy again to set property back to normal

By using the versionId I would not have to deploy a second time to reset the property back to normal.

### Proposed Solution

Add the ability through a toggle feature to add the versionId to the Cloudformation template.

**Current**
Example of Cloudformation template created through CDK currently using the `secretsManager()` function:
```
"ClientSecret": "{{resolve:secretsmanager:mysecret:SecretString:MyKey::}}"
```

**Desired**
Example of Cloudformation template desired that CDK would create in this case:
```
"ClientSecret": "{{resolve:secretsmanager:mysecret:SecretString:MyKey::ab01234c-5d67-89ef-01gh-2ijk345l6m78}}"
```

### Other Information

_No response_

### Acknowledgements

- [ ] I may be able to implement this feature request
- [X] This feature might incur a breaking change

### CDK version used

2.50.0

### Environment details (OS name and version, etc.)

MacOs 12.3

Contributor guide

Open the contributing guide

Research direction

Start from the SecretValue.secretsManager entry point and SecretsManagerSecretOptions described in the issue, then trace how the dynamic Secrets Manager reference is synthesized into a CloudFormation template. Determine how an opt-in versionId or versionStage toggle should interact with the existing API and breaking-change concern. Done means the generated reference includes the selected version information and the behavior is covered by the relevant synthesis tests, if present.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.