Skip to content

Commit

Permalink
feat(Variables): Add support for Terraform state file parsing (#8755)
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Dwyer <Brian.Dwyer@broadridge.com>
  • Loading branch information
bdwyertech committed Jan 15, 2021
1 parent d1c6568 commit 461a396
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/utils/fs/parse.js
Expand Up @@ -18,7 +18,7 @@ const loadYaml = (contents, options) => {

function parse(filePath, contents) {
// Auto-parse JSON
if (filePath.endsWith('.json')) {
if (filePath.endsWith('.json') || filePath.endsWith('.tfstate')) {
return jc.parse(contents);
} else if (filePath.endsWith('.yml') || filePath.endsWith('.yaml')) {
const options = {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/variables/serverless.yml
Expand Up @@ -7,6 +7,8 @@ provider:
custom:
importedFile: ${file(config.json)}
importedFileWithKey: ${file(config.json):foo}
importedTerraformState: ${file(terraform.tfstate)}
importedTerraformStateWithKey: ${file(terraform.tfstate):outputs.listenerarn.type}
awsVariable: ${AWS::Region}
cloudFormationReference: ${AnotherResource}
selfReference: ${self:custom.importedFileWithKey}
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/variables/terraform.tfstate
@@ -0,0 +1,13 @@
{
"version": 4,
"terraform_version": "0.14.4",
"serial": 11,
"lineage": "12ab3c45-abc1-0a1b-1a23-a12b34567c89",
"outputs": {
"listenerarn": {
"value": "arn:aws:elasticloadbalancing:us-west-2:123456789876:listener/app/myapp/1a2b3c4f1a23456b/a1b23c45de6789fa",
"type": "string"
}
},
"resources": []
}
21 changes: 21 additions & 0 deletions test/unit/lib/classes/Variables.test.js
Expand Up @@ -2776,6 +2776,27 @@ describe('test/unit/lib/classes/Variables.test.js', () => {
expect(processedConfig.custom.importedFileWithKey).to.equal('bar');
});

it('should support ${file(...)} syntax for Terraform state', () => {
expect(processedConfig.custom.importedTerraformState).to.deep.equal({
version: 4,
terraform_version: '0.14.4',
serial: 11,
lineage: '12ab3c45-abc1-0a1b-1a23-a12b34567c89',
outputs: {
listenerarn: {
value:
'arn:aws:elasticloadbalancing:us-west-2:123456789876:listener/app/myapp/1a2b3c4f1a23456b/a1b23c45de6789fa',
type: 'string',
},
},
resources: [],
});
});

it('should support ${file(...):key} syntax for Terraform state', () => {
expect(processedConfig.custom.importedTerraformStateWithKey).to.equal('string');
});

it('should ignore native CloudFormation variables', () => {
expect(processedConfig.custom.awsVariable).to.equal('${AWS::Region}');
});
Expand Down

0 comments on commit 461a396

Please sign in to comment.