Skip to content

Commit

Permalink
fix: Improve error message in SSMParameterProvider (#1630)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
RomainMuller committed Jan 29, 2019
1 parent 8b18039 commit 6a8e010
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions packages/aws-cdk/lib/context-providers/ssm-parameters.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import AWS = require('aws-sdk');
import { Mode, SDK } from '../api';
import { debug } from '../logging';
import { ContextProviderPlugin } from './provider';
Expand All @@ -18,11 +19,32 @@ export class SSMContextProviderPlugin implements ContextProviderPlugin {
const parameterName = args.parameterName;
debug(`Reading SSM parameter ${account}:${region}:${parameterName}`);

const ssm = await this.aws.ssm(account, region, Mode.ForReading);
const response = await ssm.getParameter({ Name: parameterName }).promise();
const response = await this.getSsmParameterValue(account, region, parameterName);
if (!response.Parameter || response.Parameter.Value === undefined) {
throw new Error(`SSM parameter not available in account ${account}, region ${region}: ${parameterName}`);
}
return response.Parameter.Value;
}

/**
* Gets the value of an SSM Parameter, while not throwin if the parameter does not exist.
* @param account the account in which the SSM Parameter is expected to be.
* @param region the region in which the SSM Parameter is expected to be.
* @param parameterName the name of the SSM Parameter
*
* @returns the result of the ``GetParameter`` operation.
*
* @throws Error if a service error (other than ``ParameterNotFound``) occurs.
*/
private async getSsmParameterValue(account: string, region: string, parameterName: string): Promise<AWS.SSM.GetParameterResult> {
const ssm = await this.aws.ssm(account, region, Mode.ForReading);
try {
return await ssm.getParameter({ Name: parameterName }).promise();
} catch (e) {
if (e.code === 'ParameterNotFound') {
return {};
}
throw e;
}
}
}

0 comments on commit 6a8e010

Please sign in to comment.