Skip to content

Instantly share code, notes, and snippets.

@LoganArnett
Created November 17, 2016 13:50
Show Gist options
  • Save LoganArnett/e719dde57075a24a10a0fed73b822660 to your computer and use it in GitHub Desktop.
Save LoganArnett/e719dde57075a24a10a0fed73b822660 to your computer and use it in GitHub Desktop.
Terraform Import Helpers

NOTE: Currently all of these require going through these collections that are returned and wrapping them in an Object.

Parse Tags in Resources

This helps to just get the cleaned up tags from the imported format in the console

const tags = [Array of Tag Objects];
tags.forEach(parseTags);

function parseTags(tag) {
	var result = 'tags {\n';
	Object.keys(tag).forEach(key => {
		result += `\t${key.replace(/tags./g, '')} = "${tag[key]}"\n`
	});
	result += '}'
	console.log(result)
}

Parse Settings for Elastic Beanstalk Environment

This helps to parse out the long collection of settings and display in the console

function mapSettings(settings) {
	if (Array.isArray(settings)) {
		return settings.forEach(formatSettings);
	}
	return new Error('Settings must be an array');
}

function formatSettings(settings) {
	var namespace, name, value;
	Object.keys(settings).forEach(key => {
		if (key.includes('name') && key.includes('namespace')) {
			namespace = settings[key];
		} else if (key.includes('name') && !key.includes('namespace')) {
			name = settings[key];
		} else {
			value = settings[key]	
		}
	})
	if (value === "") return; // check for default or undefined values and remove as TF will handle them
	if (namespace && name && value) {
		console.log((`setting {\n \tnamespace = "${namespace}"\n \tname = "${name}"\n \tvalue = "${value}" \n}\n`))	
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment