UNPKG

@aws-amplify/cli-internal

Version:
74 lines 4.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.resolveOutputs = void 0; const amplify_cli_core_1 = require("@aws-amplify/amplify-cli-core"); const cfn_tree_walker_1 = require("./cfn-tree-walker"); const client_cloudcontrol_1 = require("@aws-sdk/client-cloudcontrol"); async function resolveOutputs(params) { var _a; const { template, stackOutputs, stackResources, cloudControl } = params; const cloned = JSON.parse(JSON.stringify(template)); const templateOutputs = (_a = cloned.Outputs) !== null && _a !== void 0 ? _a : {}; const templateResources = cloned.Resources; if (!templateResources) { throw new amplify_cli_core_1.AmplifyError('CFNOutputError', { message: 'Template is missing Resources section', }); } const refLookup = buildRefLookup(templateOutputs, stackOutputs); cloned.Resources = (await (0, cfn_tree_walker_1.walkCfnTree)(templateResources, async (node) => { var _a, _b, _c, _d; if ('Ref' in node && typeof node.Ref === 'string' && Object.keys(node).length === 1) { const value = refLookup.get(node.Ref); const physicalId = (_a = stackResources.find((r) => r.LogicalResourceId === node.Ref)) === null || _a === void 0 ? void 0 : _a.PhysicalResourceId; const resolved = value !== null && value !== void 0 ? value : physicalId; if (resolved !== undefined) return resolved; } if ('Fn::GetAtt' in node && Array.isArray(node['Fn::GetAtt']) && Object.keys(node).length === 1) { const [logicalId, attrName] = node['Fn::GetAtt']; const stackResource = stackResources.find((r) => r.LogicalResourceId === logicalId); if (!stackResource) { throw new amplify_cli_core_1.AmplifyError('CFNOutputError', { message: `Unable to find resource with id ${logicalId}` }); } if (((_b = stackResource.ResourceType) === null || _b === void 0 ? void 0 : _b.startsWith('Custom::')) || stackResource.ResourceType === 'AWS::CloudFormation::CustomResource') { return undefined; } const physicalId = stackResource.PhysicalResourceId; const response = await cloudControl.send(new client_cloudcontrol_1.GetResourceCommand({ TypeName: stackResource.ResourceType, Identifier: physicalId })); const props = JSON.parse((_d = (_c = response.ResourceDescription) === null || _c === void 0 ? void 0 : _c.Properties) !== null && _d !== void 0 ? _d : '{}'); const value = props[attrName]; if (value !== undefined) return value; } return undefined; })); for (const [outputKey, outputDef] of Object.entries(templateOutputs)) { const runtimeOutput = stackOutputs.find((o) => o.OutputKey === outputKey); if (!(runtimeOutput === null || runtimeOutput === void 0 ? void 0 : runtimeOutput.OutputValue)) { throw new amplify_cli_core_1.AmplifyError('CFNOutputError', { message: `Stack output '${outputKey}' has no runtime value`, }); } outputDef.Value = runtimeOutput.OutputValue; } return cloned; } exports.resolveOutputs = resolveOutputs; function buildRefLookup(templateOutputs, stackOutputs) { const refLookup = new Map(); for (const [outputKey, outputDef] of Object.entries(templateOutputs)) { const value = outputDef.Value; if (typeof value !== 'object' || value === null) continue; const runtimeOutput = stackOutputs.find((o) => o.OutputKey === outputKey); if (!(runtimeOutput === null || runtimeOutput === void 0 ? void 0 : runtimeOutput.OutputValue)) continue; const record = value; if ('Ref' in record && typeof record.Ref === 'string') { refLookup.set(record.Ref, runtimeOutput.OutputValue); } } return refLookup; } //# sourceMappingURL=cfn-output-resolver.js.map