Skip to main content

Terraform Cheatsheet

ยท 2 min read
Carlos Angulo Mascarell

This post explains the next topics:

  • How to set up a shortcut for Terraform in the Terminal
  • The Terraform commands I use the most

Create an alias for TF in your Terminal

Please add the next code to your profile depending if you are using PowerShell or bash:

New-Alias -Name tf -Value terraform -Force
function tffmt { tf fmt -recursive }
function tfa { tf apply -auto-approve }
function tfd { tf destroy -auto-approve }

TF Commands

Command
tf init
tf fmt --recursive
tf plan -out plan.out
tf apply -auto-approve
tf destroy -auto-approve
tf state ls
tf state rm ADDR
tf import -var-file=[FILENAME].tfvars ADDR ID

Examples

TF IMPORT

terraform [global options] import [options] ADDR ID -> Is the resource path as RESOURCE_TYPE.RESOURCE_NAME. Examples:

aws_resourcegroups_group.main
aws_organizations_organizational_unit.apps["journalbot"] -> Resources created without foreach.
module.app_envs["temp-app-envs"].aws_organizations_organizational_unit.envs["prd"] -> Resources created **with foreach**.

Using the ADDR in tf import:

tf import -var-file=base.tfvars RESOURCE_TYPE.RESOURCE_NAME ID_OR_ARN # Resources created without foreach.
tf import -var-file=base.tfvars 'RESOURCE_TYPE.RESOURCE_NAME[\"RESOURCE_KEY\"]' ID_OR_ARN # Resources created **with foreach**. Please note the `''` for the resource path.
mklink link_name.tf original_file_path.tf # execute it in command prompt as admin

References

About me

I'm a Software Engineer with experience as Developer and DevOps. The technologies I have worked with are DotNet, Terraform and AWS. For the last one, I have the Developer Associate certification. I define myself as a challenge-seeker person and team player. I simply give it all to deliver high-quality solutions. On the other hand, I like to analyze and improve processes, promote productivity and document implementations (yes, I'm a developer that likes to document ๐Ÿง‘โ€๐Ÿ’ป).

You can check my experience here.

Personal Blog - cangulo.github.io
GitHub - Carlos Angulo Mascarell - cangulo
LinkedIn - Carlos Angulo Mascarell
Twitter - @AnguloMascarell

Did you like it? Share It!


Comments