Skip to content

Instantly share code, notes, and snippets.

@dehowell
Created March 26, 2018 00:18
Show Gist options
  • Save dehowell/db3f5500fe4fb09b0c1973a3443b7131 to your computer and use it in GitHub Desktop.
Save dehowell/db3f5500fe4fb09b0c1973a3443b7131 to your computer and use it in GitHub Desktop.
Getting Serverless variables from terraform state
const exec = require('child_process').exec;
function terraformOutput(name, tfModule) {
let mod = tfModule != undefined ? `-module ${tfModule}` : '';
let cmd = `terraform output -json ${mod}`;
let outputs = new Promise((resolve, reject) => {
exec(cmd, {'cwd': 'infrastructure'},
(error, stdout, stderr) => {
if (error) reject(error);
else resolve(stdout);
}
)
});
return outputs
.then(JSON.parse)
.then(d => d[name].value);
}
module.exports = () => {
return {
web_bucket: terraformOutput('web_bucket', 'hosting'),
publisher_role: terraformOutput('publisher_role')
}
}
custom: ${file(./config.js)}
# then refer to the variables with ${self:custom.web_bucket}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment