Skip to content

Commit 7d0680a

Browse files
authored
feat(ssm): reference latest version of secure string parameters (#18187)
Supported by CF since April 2021 but not yet ported to CDK. See https://aws.amazon.com/about-aws/whats-new/2021/04/now-reference-latest-aws-systems-manager-parameter-values-in-aws-cloudformation-templates-without-specifying-parameter-versions/ Close #17091 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 8cb6a76 commit 7d0680a

File tree

6 files changed

+40
-9
lines changed

6 files changed

+40
-9
lines changed

packages/@aws-cdk/aws-ssm/lib/parameter.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,11 @@ export interface StringParameterAttributes extends CommonStringParameterAttribut
311311
*/
312312
export interface SecureStringParameterAttributes extends CommonStringParameterAttributes {
313313
/**
314-
* The version number of the value you wish to retrieve. This is required for secure strings.
314+
* The version number of the value you wish to retrieve.
315+
*
316+
* @default - AWS CloudFormation uses the latest version of the parameter
315317
*/
316-
readonly version: number;
318+
readonly version?: number;
317319

318320
/**
319321
* The encryption key that is used to encrypt this parameter
@@ -365,7 +367,8 @@ export class StringParameter extends ParameterBase implements IStringParameter {
365367
* Imports a secure string parameter from the SSM parameter store.
366368
*/
367369
public static fromSecureStringParameterAttributes(scope: Construct, id: string, attrs: SecureStringParameterAttributes): IStringParameter {
368-
const stringValue = new CfnDynamicReference(CfnDynamicReferenceService.SSM_SECURE, `${attrs.parameterName}:${Tokenization.stringifyNumber(attrs.version)}`).toString();
370+
const version = attrs.version ? Tokenization.stringifyNumber(attrs.version) : '';
371+
const stringValue = new CfnDynamicReference(CfnDynamicReferenceService.SSM_SECURE, `${attrs.parameterName}:${version}`).toString();
369372

370373
class Import extends ParameterBase {
371374
public readonly parameterName = attrs.parameterName;

packages/@aws-cdk/aws-ssm/test/parameter.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,19 @@ test('StringParameter.fromSecureStringParameterAttributes with encryption key cr
589589
});
590590
});
591591

592+
test('StringParameter.fromSecureStringParameterAttributes without version', () => {
593+
// GIVEN
594+
const stack = new cdk.Stack();
595+
596+
// WHEN
597+
const param = ssm.StringParameter.fromSecureStringParameterAttributes(stack, 'MyParamName', {
598+
parameterName: 'MyParamName',
599+
});
600+
601+
// THEN
602+
expect(stack.resolve(param.stringValue)).toEqual('{{resolve:ssm-secure:MyParamName:}}');
603+
});
604+
592605
test('StringListParameter.fromName', () => {
593606
// GIVEN
594607
const stack = new cdk.Stack();

packages/@aws-cdk/core/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,9 @@ Using AWS Secrets Manager is the recommended way to reference secrets in a CDK a
223223
`SecretValue` also supports the following secret sources:
224224

225225
- `SecretValue.plainText(secret)`: stores the secret as plain text in your app and the resulting template (not recommended).
226-
- `SecretValue.ssmSecure(param, version)`: refers to a secret stored as a SecureString in the SSM Parameter Store.
226+
- `SecretValue.ssmSecure(param, version)`: refers to a secret stored as a SecureString in the SSM
227+
Parameter Store. If you don't specify the exact version, AWS CloudFormation uses the latest
228+
version of the parameter.
227229
- `SecretValue.cfnParameter(param)`: refers to a secret passed through a CloudFormation parameter (must have `NoEcho: true`).
228230
- `SecretValue.cfnDynamicReference(dynref)`: refers to a secret described by a CloudFormation dynamic reference (used by `ssmSecure` and `secretsManager`).
229231

packages/@aws-cdk/core/lib/secret-value.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ export class SecretValue extends Intrinsic {
6767
* Parameter Store. The parameter name is case-sensitive.
6868
*
6969
* @param version An integer that specifies the version of the parameter to
70-
* use. You must specify the exact version. You cannot currently specify that
71-
* AWS CloudFormation use the latest version of a parameter.
70+
* use. If you don't specify the exact version, AWS CloudFormation uses the
71+
* latest version of the parameter.
7272
*/
73-
public static ssmSecure(parameterName: string, version: string): SecretValue {
74-
const parts = [parameterName, version];
73+
public static ssmSecure(parameterName: string, version?: string): SecretValue {
74+
const parts = [parameterName, version ?? ''];
7575
return this.cfnDynamicReference(new CfnDynamicReference(CfnDynamicReferenceService.SSM_SECURE, parts.join(':')));
7676
}
7777

packages/@aws-cdk/core/test/secret-value.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ describe('secret value', () => {
119119

120120
});
121121

122+
test('ssmSecure without version', () => {
123+
// GIVEN
124+
const stack = new Stack();
125+
126+
// WHEN
127+
const v = SecretValue.ssmSecure('param-name');
128+
129+
// THEN
130+
expect(stack.resolve(v)).toEqual('{{resolve:ssm-secure:param-name:}}');
131+
});
132+
122133
test('cfnDynamicReference', () => {
123134
// GIVEN
124135
const stack = new Stack();

packages/aws-cdk-lib/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ Using AWS Secrets Manager is the recommended way to reference secrets in a CDK a
254254
`SecretValue` also supports the following secret sources:
255255

256256
- `SecretValue.plainText(secret)`: stores the secret as plain text in your app and the resulting template (not recommended).
257-
- `SecretValue.ssmSecure(param, version)`: refers to a secret stored as a SecureString in the SSM Parameter Store.
257+
- `SecretValue.ssmSecure(param, version)`: refers to a secret stored as a SecureString in the SSM
258+
Parameter Store. If you don't specify the exact version, AWS CloudFormation uses the latest
259+
version of the parameter.
258260
- `SecretValue.cfnParameter(param)`: refers to a secret passed through a CloudFormation parameter (must have `NoEcho: true`).
259261
- `SecretValue.cfnDynamicReference(dynref)`: refers to a secret described by a CloudFormation dynamic reference (used by `ssmSecure` and `secretsManager`).
260262

0 commit comments

Comments
 (0)