Skip to content

Commit

Permalink
fix: ensure REST API CFN outputs the API ID (#7538)
Browse files Browse the repository at this point in the history
* fix: ensure REST API CFN outputs the API ID

This commit updates the REST API migration logic to ensure
that the API ID is a CFN Output, as the policy consolidation
logic will rely on this during push.

* fix: replace early return with throw

Co-authored-by: Colin Ihrig <colihrig@amazon.com>
  • Loading branch information
cjihrig and cjihrig-aws authored Jun 17, 2021
1 parent 9b7a6b7 commit c3f4128
Showing 1 changed file with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,13 @@ function updateExistingApiCfn(context: $TSContext, api: $TSObject): void {
const apiParamsFile = path.join(resourceDir, API_PARAMS_FILE);
const cfn: any = JSONUtilities.readJson(cfnTemplate, { throwIfNotExist: false }) ?? {};
const parameterJson = JSONUtilities.readJson(paramsFile, { throwIfNotExist: false }) ?? {};
const parameters = cfn?.Parameters ?? {};
const resources = cfn?.Resources ?? {};

if (!cfn) {
throw new Error(`CloudFormation template missing for REST API ${resourceName}`);
}

const parameters = cfn.Parameters ?? {};
const resources = cfn.Resources ?? {};
let modified = false;

for (const parameterName in parameters) {
Expand Down Expand Up @@ -279,6 +284,17 @@ function updateExistingApiCfn(context: $TSContext, api: $TSObject): void {
modified = true;
}
}
} else if (resource.Type === 'AWS::ApiGateway::RestApi') {
cfn.Outputs ??= {};

// Ensure that th REST API's ID is an output of the stack.
if (!cfn.Outputs.ApiId) {
cfn.Outputs.ApiId = {
Description: 'API ID (prefix of API URL)',
Value: { Ref: resourceName },
};
modified = true;
}
}
}

Expand Down

0 comments on commit c3f4128

Please sign in to comment.