Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve error message in SSMParameterProvider #1630

Merged
merged 2 commits into from
Jan 29, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}