Last active
February 24, 2021 13:31
-
-
Save itsjwala/52cf3c8621b218cc51f1a482830c3c87 to your computer and use it in GitHub Desktop.
parse terraform file to create aws_s3_bucket resource
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parse (object,bucket) { | |
// console.log(object) | |
Object.keys(object).forEach(function (key) { | |
let val = object[key]; | |
if (val == null) { | |
if (key == "bucket_prefix" || key == "acl" | |
|| key == "website_domain" | |
|| key == "website_endpoint") { | |
//skip | |
return | |
} | |
else if (key == "force_destroy") { | |
process.stdout.write(`${key} = false\n`) | |
} | |
else { | |
process.stdout.write(`${key} = ""\n`) | |
} | |
} | |
else if (typeof(val) == "string"){ | |
if (key == "region" || key == "bucket_domain_name" | |
|| key == "bucket_regional_domain_name" | |
|| (key == "id" && val == bucket) | |
|| val == "" ) { | |
//skip | |
return | |
} | |
process.stdout.write(`${key} = "${val}"\n`) | |
} | |
else if (typeof (val) == "boolean" || typeof (val) == "number") { | |
process.stdout.write(`${key} = ${val}\n`) | |
} | |
else if (Array.isArray(val)){ | |
if (key == "website") { | |
//skip | |
return | |
} | |
// nested array of objects | |
// eg: lifecycle_rule = [{}, {}] | |
// naive check | |
if(typeof (val[0]) == "object") { | |
val.forEach((a, idx) => { | |
// if() | |
process.stdout.write(`${key} { \n`) | |
parse(a,bucket) | |
process.stdout.write(`}\n`) | |
}) | |
} | |
// empty array | |
// eg: "object_lock_configuration": [] | |
else if (val.length == 0) { | |
// skip | |
} | |
// plain array | |
// eg: "permissions": [ "READ_ACP", "WRITE"] | |
else { | |
process.stdout.write(`${key} = ${JSON.stringify(val)} \n`) | |
} | |
} | |
else if (typeof (val) == "object") { | |
// empty object | |
// eg : "tags": {} | |
if (Object.keys(val).length == 0 || key == "grant") { | |
// skip | |
} | |
else { | |
process.stdout.write(`${key} = {\n`) | |
parse(val,bucket) | |
process.stdout.write(`}\n`) | |
} | |
} | |
}); | |
} | |
let bucket = process.argv[2] | |
let region = require('child_process').execSync( | |
`aws s3api get-bucket-location --bucket ${bucket} | jq ".LocationConstraint"`, | |
).toString().trim(); | |
let provider = "" | |
if (region == "null") { | |
provider = "aws" | |
} | |
else { | |
provider = `aws.${region}` | |
} | |
require('child_process').execSync( | |
`echo " resource aws_s3_bucket ${bucket.replace(/\./g, "_")} { provider = ${provider} }" > tmp.tf` | |
); | |
require('child_process').execSync( | |
`terraform14 import aws_s3_bucket."${bucket.replace(/\./g, "_")}" "${bucket}"`); | |
const fs = require('fs') | |
let rr = JSON.parse(fs.readFileSync('terraform.tfstate')) | |
let idx = rr['resources'].map((a, idx) => { if (a['name'] == bucket.replace(/\./g, "_")) return idx }).filter(a => a != undefined)[0] | |
let attributes = rr['resources'][idx]['instances'][0]['attributes'] | |
process.stdout.write(`resource aws_s3_bucket ${bucket.replace(/\./g, "_")} {\n provider = ${provider} \n `) | |
parse(attributes,bucket) | |
process.stdout.write(`}\n\n\n\n`) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
run using
generates
Note - This fits my use case, please change the script as per your need