Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports = (serverlessInstance) => {
})();

if (!result) return { value: null };
const outputs = result.Stacks[0].Outputs;
const outputs = result.Stacks[0].Outputs || [];
const output = outputs.find((x) => x.OutputKey === outputLogicalId);

return { value: output ? output.OutputValue : null };
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/aws/utils/resolve-cf-import-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const ServerlessError = require('../../../serverless-error');

async function resolveCfImportValue(provider, name, sdkParams = {}) {
return provider.request('CloudFormation', 'listExports', sdkParams).then((result) => {
const targetExportMeta = result.Exports.find((exportMeta) => exportMeta.Name === name);
const targetExportMeta = (result.Exports || []).find((exportMeta) => exportMeta.Name === name);
if (targetExportMeta) return targetExportMeta.Value;
if (result.NextToken) {
return resolveCfImportValue(provider, name, { NextToken: result.NextToken });
return resolveCfImportValue(provider, name, { ...sdkParams, NextToken: result.NextToken });
}

throw new ServerlessError(
Expand Down
18 changes: 10 additions & 8 deletions lib/plugins/aws/utils/resolve-cf-ref-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ const ServerlessError = require('../../../serverless-error');

async function resolveCfRefValue(provider, resourceLogicalId, sdkParams = {}) {
return provider
.request(
'CloudFormation',
'listStackResources',
Object.assign(sdkParams, { StackName: provider.naming.getStackName() })
)
.request('CloudFormation', 'listStackResources', {
...sdkParams,
StackName: provider.naming.getStackName(),
})
.then((result) => {
const targetStackResource = result.StackResourceSummaries.find(
const targetStackResource = (result.StackResourceSummaries || []).find(
(stackResource) => stackResource.LogicalResourceId === resourceLogicalId
);
if (targetStackResource) return targetStackResource.PhysicalResourceId;
if (result.NextToken) {
return resolveCfRefValue(provider, resourceLogicalId, { NextToken: result.NextToken });
return resolveCfRefValue(provider, resourceLogicalId, {
...sdkParams,
NextToken: result.NextToken,
});
}

throw new ServerlessError(
`Could not resolve Ref with name ${resourceLogicalId}. Are you sure this value matches one resource logical ID ?`,
`Could not resolve Ref with name ${resourceLogicalId}. Are you sure this value matches a resource logical ID?`,
'CF_REF_RESOLUTION'
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('test/unit/lib/configuration/variables/sources/instance-dependent/get-c
existing: '${cf:existing.someOutput}',
existingInRegion: '${cf(eu-west-1):existing.someOutput}',
noOutput: '${cf:existing.unrecognizedOutput, null}',
noOutputs: '${cf:noOutputs.someOutput, null}',
noStack: '${cf:notExisting.someOutput, null}',
missingAddress: '${cf:}',
invalidAddress: '${cf:invalid}',
Expand All @@ -39,6 +40,9 @@ describe('test/unit/lib/configuration/variables/sources/instance-dependent/get-c
],
};
}
if (StackName === 'noOutputs') {
return { Stacks: [{}] };
}
if (StackName === 'notExisting') {
throw Object.assign(new Error('Stack with id not-existing does not exist'), {
code: 'AWS_CLOUD_FORMATION_DESCRIBE_STACKS_VALIDATION_ERROR',
Expand Down Expand Up @@ -73,6 +77,12 @@ describe('test/unit/lib/configuration/variables/sources/instance-dependent/get-c
if (variablesMeta.get('custom\0noOutput')) throw variablesMeta.get('custom\0noOutput').error;
expect(configuration.custom.noOutput).to.equal(null);
});
it('should resolve null when stack has no outputs', () => {
if (variablesMeta.get('custom\0noOutputs')) {
throw variablesMeta.get('custom\0noOutputs').error;
}
expect(configuration.custom.noOutputs).to.equal(null);
});
it('should resolve null on missing stack', () => {
if (variablesMeta.get('custom\0noStack')) throw variablesMeta.get('custom\0noStack').error;
expect(configuration.custom.noStack).to.equal(null);
Expand Down
32 changes: 32 additions & 0 deletions test/unit/lib/plugins/aws/utils/resolve-cf-import-value.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,36 @@ describe('#resolveCfImportValue', () => {
const result = await resolveCfImportValue(provider, 'exportName');
expect(result).to.equal('exportValue');
});

it('should continue pagination when a page has no exports', async () => {
const requests = [];
const sdkParams = { SomeParam: 'kept' };
const provider = {
request: async (service, method, params) => {
requests.push({ service, method, params });
if (!params.NextToken) return { NextToken: 'next-page' };
return {
Exports: [
{
Name: 'exportName',
Value: 'exportValue',
},
],
};
},
};

const result = await resolveCfImportValue(provider, 'exportName', sdkParams);

expect(result).to.equal('exportValue');
expect(sdkParams).to.deep.equal({ SomeParam: 'kept' });
expect(requests).to.deep.equal([
{ service: 'CloudFormation', method: 'listExports', params: { SomeParam: 'kept' } },
{
service: 'CloudFormation',
method: 'listExports',
params: { SomeParam: 'kept', NextToken: 'next-page' },
},
]);
});
});
61 changes: 61 additions & 0 deletions test/unit/lib/plugins/aws/utils/resolve-cf-ref-value.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,65 @@ describe('#resolveCfRefValue', () => {
const result = await resolveCfRefValue(provider, 'myDB');
expect(result).to.equal('stack-name-db-id');
});

it('should continue pagination when a page has no stack resources', async () => {
const requests = [];
const sdkParams = { SomeParam: 'kept' };
const provider = {
naming: {
getStackName: () => 'stack-name',
},
request: async (service, method, params) => {
requests.push({ service, method, params });
if (!params.NextToken) return { NextToken: 'next-page' };
return {
StackResourceSummaries: [
{
LogicalResourceId: 'myDB',
PhysicalResourceId: 'stack-name-db-id',
},
],
};
},
};

const result = await resolveCfRefValue(provider, 'myDB', sdkParams);

expect(result).to.equal('stack-name-db-id');
expect(sdkParams).to.deep.equal({ SomeParam: 'kept' });
expect(requests).to.deep.equal([
{
service: 'CloudFormation',
method: 'listStackResources',
params: { SomeParam: 'kept', StackName: 'stack-name' },
},
{
service: 'CloudFormation',
method: 'listStackResources',
params: { SomeParam: 'kept', NextToken: 'next-page', StackName: 'stack-name' },
},
]);
});

it('should report a clear error when no resource matches', async () => {
const provider = {
naming: {
getStackName: () => 'stack-name',
},
request: async () => ({ StackResourceSummaries: [] }),
};

let error;
try {
await resolveCfRefValue(provider, 'missingResource');
} catch (caughtError) {
error = caughtError;
}

expect(error).to.have.property(
'message',
'Could not resolve Ref with name missingResource. Are you sure this value matches a resource logical ID?'
);
expect(error).to.have.property('code', 'CF_REF_RESOLUTION');
});
});