Skip to content

Instantly share code, notes, and snippets.

@kharandziuk
Created July 21, 2022 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kharandziuk/9444651ca258d40812fb8eff875b666f to your computer and use it in GitHub Desktop.
Save kharandziuk/9444651ca258d40812fb8eff875b666f to your computer and use it in GitHub Desktop.
terraform import tasks
import sys
from invoke import task
@task(name="import")
def _import(c, name, id):
print(id)
resource_type, resource_name = name.split(".")
txt = f'resource "{resource_type}" "{resource_name}" {{}}'
cmd = f"echo '{txt}' > temp.tf"
c.run(cmd, echo=True)
cmd = f"terraform import -no-color {name} '{id}'"
c.run(cmd, echo=True)
cmd = f"terraform state show -no-color {name} > temp.tf"
c.run(cmd, echo=True)
c.run("terraform plan", echo=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment