Skip to content

Instantly share code, notes, and snippets.

@onyxhat
Created May 23, 2022 20:15
Show Gist options
  • Save onyxhat/fb705c4a07750d1a55d0cba2ff1bf3e9 to your computer and use it in GitHub Desktop.
Save onyxhat/fb705c4a07750d1a55d0cba2ff1bf3e9 to your computer and use it in GitHub Desktop.
Terraformer Export Helper
[CmdletBinding()]
param (
[Parameter()]
[string]
$aws_profile = "VirtixProd",
[Parameter()]
[string[]]
$aws_region = @("us-east-2"),
[Parameter()]
[string[]]
$aws_resources = @("*")
)
Begin {
$ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path
[string]$regions = $aws_region -join ','
[string]$resources = $aws_resources -join ','
[string]$path_pattern = "{output}/{service}/"
}
Process {
foreach ($r in $aws_region) {
terraformer import aws --regions=$regions `
--profile=$aws_profile `
--path-output="${aws_profile}/${r}" `
--path-pattern=$path_pattern `
--resources=$resources
}
terraformer import aws --profile=$aws_profile `
--path-output="${aws_profile}/no_region" `
--path-pattern=$path_pattern `
--resources=$resources
}
End {
$junk_files = Get-ChildItem -Recurse -Path "${ScriptDir}" | ? { $_.Name -in @("outputs.tf", "provider.tf", "terraform.tfstate", "variables.tf") } | Select-Object -ExpandProperty FullName
$junk_files | Remove-Item -Force
$empty_dirs = Get-ChildItem -Path $ScriptDir -Directory -Recurse | ? { (Get-ChildItem $_.FullName).Count -eq 0 } | Select-Object -ExpandProperty FullName
$empty_dirs | Remove-Item -Force
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment