Skip to content

Commit 6a8e010

Browse files
authored
fix: Improve error message in SSMParameterProvider (#1630)
When using a non-existent SSM Parameter with the SSMParameterProvider, the error message `null` was returned by the toolkit instead of a more meaningful error mentioning important details such as the parameter name that was looked up and the account/region where it could not be found. Fixes #1621
1 parent 8b18039 commit 6a8e010

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

packages/aws-cdk/lib/context-providers/ssm-parameters.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import AWS = require('aws-sdk');
12
import { Mode, SDK } from '../api';
23
import { debug } from '../logging';
34
import { ContextProviderPlugin } from './provider';
@@ -18,11 +19,32 @@ export class SSMContextProviderPlugin implements ContextProviderPlugin {
1819
const parameterName = args.parameterName;
1920
debug(`Reading SSM parameter ${account}:${region}:${parameterName}`);
2021

21-
const ssm = await this.aws.ssm(account, region, Mode.ForReading);
22-
const response = await ssm.getParameter({ Name: parameterName }).promise();
22+
const response = await this.getSsmParameterValue(account, region, parameterName);
2323
if (!response.Parameter || response.Parameter.Value === undefined) {
2424
throw new Error(`SSM parameter not available in account ${account}, region ${region}: ${parameterName}`);
2525
}
2626
return response.Parameter.Value;
2727
}
28+
29+
/**
30+
* Gets the value of an SSM Parameter, while not throwin if the parameter does not exist.
31+
* @param account the account in which the SSM Parameter is expected to be.
32+
* @param region the region in which the SSM Parameter is expected to be.
33+
* @param parameterName the name of the SSM Parameter
34+
*
35+
* @returns the result of the ``GetParameter`` operation.
36+
*
37+
* @throws Error if a service error (other than ``ParameterNotFound``) occurs.
38+
*/
39+
private async getSsmParameterValue(account: string, region: string, parameterName: string): Promise<AWS.SSM.GetParameterResult> {
40+
const ssm = await this.aws.ssm(account, region, Mode.ForReading);
41+
try {
42+
return await ssm.getParameter({ Name: parameterName }).promise();
43+
} catch (e) {
44+
if (e.code === 'ParameterNotFound') {
45+
return {};
46+
}
47+
throw e;
48+
}
49+
}
2850
}

0 commit comments

Comments
 (0)