SlideShare une entreprise Scribd logo
1  sur  2
Télécharger pour lire hors ligne
Terraform CLI Cheat Sheet
About Terraform CLI
Terraform, a tool created by Hashicorp in 2014, written in Go, aims
to build, change and version control your infrastructure. This tool
have a powerfull and very intuitive Command Line Interface.
Installation
Install through curl
$ curl -O https://releases.hashicorp.com/terraform/
0.11.10/terraform_0.11.10_linux_amd64.zip
$ sudo unzip terraform_0.11.10_linux_amd64.zip
-d /usr/local/bin/
$ rm terraform_0.11.10_linux_amd64.zip
OR install through tfenv: a Terraform version manager
First of all, download the tfenv binary and put it in your PATH.
$ git clone https://github.com/Zordrak/tfenv.git
~/.tfenv
$ echo 'export PATH="$HOME/.tfenv/bin:$PATH"'
>> $HOME/bashrc
Then, you can install desired version of terraform:
$ tfenv install 0.11.10
Usage
Show version
$ terraform --version
Terraform v0.11.10
Init Terraform
$ terraform init
It’s the rst command you need to execute. Unless, terraform
plan, apply, destroy and import will not work. The command
terraform init will install :
terraform modules
eventually a backend
and provider(s) plugins
Init Terraform and don’t ask any input
$ terraform init -input=false
Change backend con guration during the init
$ terraform init -backend-config=cfg/s3.dev.tf -
reconfigure
-reconfigure is used in order to tell terraform to not copy the
existing state to the new remote state location.
Get
This command is useful when you have de ned some modules.
Modules are vendored so when you edit them, you need to get
again modules content.
$ terraform get -update=true
When you use modules, the rst thing you’ll have to do is to do a
terraform get. This pulls modules into the .terraform directory.
Once you do that, unless you do another terraform get -
update=true, you’ve essentially vendored those modules.
Plan
The plan step check con guration to execute and write a plan to
apply to target infrastructure provider.
$ terraform plan -out plan.out
It’s an important feature of Terraform that allows a user to see
which actions Terraform will perform prior to making any changes,
increasing con dence that a change will have the desired effect
once applied.
When you execute terraform plan command, terraform will scan
all *.tf les in your directory and create the plan.
Apply
Now you have the desired state so you can execute the plan.
$ terraform apply plan.out
Good to know: Since terraform v0.11+, in an interactive mode (non
CI/CD/autonomous pipeline), you can just execute terraform
apply command which will print out which actions TF will
perform.
By generating the plan and applying it in the same command,
Terraform can guarantee that the execution plan won’t change,
without needing to write it to disk. This reduces the risk of
potentially-sensitive data being left behind, or accidentally
checked into version control.
$ terraform apply
Apply and auto approve
$ terraform apply -auto-approve
Apply and de ne new variables value
$ terraform apply -auto-approve
-var tags-repository_url=${GIT_URL}
Apply only one module
$ terraform apply -target=module.s3
This -target option works with terraform plan too.
Destroy
$ terraform destroy
Delete all the resources!
A deletion plan can be created before:
$ terraform plan –destroy
-target option allow to destroy only one resource, for example a
S3 bucket :
$ terraform destroy -target aws_s3_bucket.my_bucket
Debug
The Terraform console command is useful for testing
interpolations before using them in con gurations. Terraform
console will read con gured state even if it is remote.
$ echo "aws_iam_user.notif.arn" | terraform console
arn:aws:iam::123456789:user/notif
Graph
$ terraform graph | dot –Tpng > graph.png
Visual dependency graph of terraform resources.
Validate
Validate command is used to validate/check the syntax of the
Terraform les. A syntax check is done on all the terraform les in
the directory, and will display an error if any of the les doesn’t
validate. The syntax check does not cover every syntax common
issues.
$ terraform validate
Providers
You can use a lot of providers/plugins in your terraform de nition
resources, so it can be useful to have a tree of providers used by
modules in your project.
$ terraform providers
.
├── provider.aws ~> 1.24.0
├── module.my_module
│ ├── provider.aws (inherited)
│ ├── provider.null
│ └── provider.template
└── module.elastic
└── provider.aws (inherited)
State
Pull remote state in a local copy
$ terraform state pull > terraform.tfstate
Push state in remote backend storage
$ terraform state push
This command is usefull if for example you riginally use a local tf
state and then you de ne a backend storage, in S3 or Consul…
How to tell to Terraform you moved a ressource in a
module?
If you moved an existing resource in a module, you need to update
the state:
$ terraform state mv aws_iam_role.role1 module.mymodul
How to import existing resource in Terraform?
If you have an existing resource in your infrastructure provider,
you can import it in your Terraform state:
$ terraform import aws_iam_policy.elastic_post
arn:aws:iam::123456789:policy/elastic_post
Workspaces
To manage multiple distinct sets of infrastructure
resources/environments.
Instead of create a directory for each environment to manage, we
need to just create needed workspace and use them:
Create workspace
This command create a new workspace and then select it
$ terraform workspace new dev
Select a workspace
$ terraform workspace select dev
List workspaces
$ terraform workspace list
default
* dev
prelive
Show current workspace
$ terraform workspace show
dev
Tools
jq
jq is a lightweight command-line JSON processor. Combined with
terraform output it can be powerful.
Installation
For Linux:
$ sudo apt-get install jq
or
$ yum install jq
For OS X:
$ brew install jq
Usage
For example, we de nd outputs in a module and when we execute
terraform apply outputs are displayed:
$ terraform apply
...
Apply complete! Resources: 0 added, 0 changed,
0 destroyed.
Outputs:
elastic_endpoint = vpc-toto-12fgfd4d5f4ds5fngetwe4.
eu-central-1.es.amazonaws.com
We can extract the value that we want in order to use it in a script
for example. With jq it’s easy:
$ terraform output -json
{
"elastic_endpoint": {
"sensitive": false,
"type": "string",
"value": "vpc-toto-12fgfd4d5f4ds5fngetwe4.
eu-central-1.es.amazonaws.com"
}
}
$ terraform output -json | jq '.elastic_endpoint.value
"vpc-toto-12fgfd4d5f4ds5fngetwe4.eu-central-1.
es.amazonaws.com"
Terraforming
If you have an existing AWS account for examples with existing
components like S3 buckets, SNS, VPC … You can use
terraforming tool, a tool written in Ruby, which extract existing
AWS resources and convert it to Terraform les!
Installation
$ sudo apt install ruby or $ sudo yum install ruby
and
$ gem install terraforming
Usage
Pre-requisites :
Like for Terraform, you need to set AWS credentials
$ export AWS_ACCESS_KEY_ID="an_aws_access_key"
$ export AWS_SECRET_ACCESS_KEY="a_aws_secret_key"
$ export AWS_DEFAULT_REGION="eu-central-1"
You can also specify credential pro le in ~/.aws/credentials_s and
with _–pro le option.
$ cat ~/.aws/credentials
[aurelie]
aws_access_key_id = xxx
aws_secret_access_key = xxx
aws_default_region = eu-central-1
$ terraforming s3 --profile aurelie
Usage
$ terraforming --help
Commands:
terraforming alb # ALB
...
terraforming vgw # VPN Gateway
terraforming vpc # VPC
Example:
$ terraforming s3 > aws_s3.tf
Remarks: As you can see, terraforming can’t extract for the
moment API gateway resources so you need to write it manually.
Authors :
@aurelievache
Cloud Dev(Ops) at Continental
v1.0.2

Contenu connexe

Similaire à Terraform cheat sheet.pdf

The hitchhiker's guide to terraform your infrastructure
The hitchhiker's guide to terraform your infrastructureThe hitchhiker's guide to terraform your infrastructure
The hitchhiker's guide to terraform your infrastructureFernanda Martins
 
Configuration management II - Terraform
Configuration management II - TerraformConfiguration management II - Terraform
Configuration management II - TerraformXavier Serrat Bordas
 
Misadventures With Terraform
Misadventures With TerraformMisadventures With Terraform
Misadventures With TerraformMatt Revell
 
Terraform + ansible talk
Terraform + ansible talkTerraform + ansible talk
Terraform + ansible talkJames Strong
 
Using Terraform to manage the configuration of a Cisco ACI fabric.
Using Terraform to manage the configuration of a Cisco ACI fabric.Using Terraform to manage the configuration of a Cisco ACI fabric.
Using Terraform to manage the configuration of a Cisco ACI fabric.Joel W. King
 
MINCS - containers in the shell script (Eng. ver.)
MINCS - containers in the shell script (Eng. ver.)MINCS - containers in the shell script (Eng. ver.)
MINCS - containers in the shell script (Eng. ver.)Masami Hiramatsu
 
Introductory Overview to Managing AWS with Terraform
Introductory Overview to Managing AWS with TerraformIntroductory Overview to Managing AWS with Terraform
Introductory Overview to Managing AWS with TerraformMichael Heyns
 
Debasihish da final.ppt
Debasihish da final.pptDebasihish da final.ppt
Debasihish da final.pptKalkey
 
Collaborative Terraform with Atlantis
Collaborative Terraform with AtlantisCollaborative Terraform with Atlantis
Collaborative Terraform with AtlantisFerenc Kovács
 
Comprehensive Terraform Training
Comprehensive Terraform TrainingComprehensive Terraform Training
Comprehensive Terraform TrainingYevgeniy Brikman
 
Terraform modules and (some of) best practices
Terraform modules and (some of) best practicesTerraform modules and (some of) best practices
Terraform modules and (some of) best practicesAnton Babenko
 
Terraform infraestructura como código
Terraform infraestructura como códigoTerraform infraestructura como código
Terraform infraestructura como códigoVictor Adsuar
 
Managing GCP Projects with Terraform (devfest Pisa 2018)
Managing GCP Projects with Terraform (devfest Pisa 2018)Managing GCP Projects with Terraform (devfest Pisa 2018)
Managing GCP Projects with Terraform (devfest Pisa 2018)Giovanni Toraldo
 

Similaire à Terraform cheat sheet.pdf (20)

The hitchhiker's guide to terraform your infrastructure
The hitchhiker's guide to terraform your infrastructureThe hitchhiker's guide to terraform your infrastructure
The hitchhiker's guide to terraform your infrastructure
 
Terraform tfstate
Terraform tfstateTerraform tfstate
Terraform tfstate
 
Terraform day1
Terraform day1Terraform day1
Terraform day1
 
Terraform training 🎒 - Basic
Terraform training 🎒 - BasicTerraform training 🎒 - Basic
Terraform training 🎒 - Basic
 
Configuration management II - Terraform
Configuration management II - TerraformConfiguration management II - Terraform
Configuration management II - Terraform
 
Misadventures With Terraform
Misadventures With TerraformMisadventures With Terraform
Misadventures With Terraform
 
Terraform + ansible talk
Terraform + ansible talkTerraform + ansible talk
Terraform + ansible talk
 
London HUG 12/4
London HUG 12/4London HUG 12/4
London HUG 12/4
 
Using Terraform to manage the configuration of a Cisco ACI fabric.
Using Terraform to manage the configuration of a Cisco ACI fabric.Using Terraform to manage the configuration of a Cisco ACI fabric.
Using Terraform to manage the configuration of a Cisco ACI fabric.
 
MINCS - containers in the shell script (Eng. ver.)
MINCS - containers in the shell script (Eng. ver.)MINCS - containers in the shell script (Eng. ver.)
MINCS - containers in the shell script (Eng. ver.)
 
Introductory Overview to Managing AWS with Terraform
Introductory Overview to Managing AWS with TerraformIntroductory Overview to Managing AWS with Terraform
Introductory Overview to Managing AWS with Terraform
 
Debasihish da final.ppt
Debasihish da final.pptDebasihish da final.ppt
Debasihish da final.ppt
 
Solaris 10 Container
Solaris 10 ContainerSolaris 10 Container
Solaris 10 Container
 
Collaborative Terraform with Atlantis
Collaborative Terraform with AtlantisCollaborative Terraform with Atlantis
Collaborative Terraform with Atlantis
 
Comprehensive Terraform Training
Comprehensive Terraform TrainingComprehensive Terraform Training
Comprehensive Terraform Training
 
Terraform modules and (some of) best practices
Terraform modules and (some of) best practicesTerraform modules and (some of) best practices
Terraform modules and (some of) best practices
 
FreeBSD: Dev to Prod
FreeBSD: Dev to ProdFreeBSD: Dev to Prod
FreeBSD: Dev to Prod
 
lec4.docx
lec4.docxlec4.docx
lec4.docx
 
Terraform infraestructura como código
Terraform infraestructura como códigoTerraform infraestructura como código
Terraform infraestructura como código
 
Managing GCP Projects with Terraform (devfest Pisa 2018)
Managing GCP Projects with Terraform (devfest Pisa 2018)Managing GCP Projects with Terraform (devfest Pisa 2018)
Managing GCP Projects with Terraform (devfest Pisa 2018)
 

Dernier

Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 

Dernier (20)

Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 

Terraform cheat sheet.pdf

  • 1. Terraform CLI Cheat Sheet About Terraform CLI Terraform, a tool created by Hashicorp in 2014, written in Go, aims to build, change and version control your infrastructure. This tool have a powerfull and very intuitive Command Line Interface. Installation Install through curl $ curl -O https://releases.hashicorp.com/terraform/ 0.11.10/terraform_0.11.10_linux_amd64.zip $ sudo unzip terraform_0.11.10_linux_amd64.zip -d /usr/local/bin/ $ rm terraform_0.11.10_linux_amd64.zip OR install through tfenv: a Terraform version manager First of all, download the tfenv binary and put it in your PATH. $ git clone https://github.com/Zordrak/tfenv.git ~/.tfenv $ echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> $HOME/bashrc Then, you can install desired version of terraform: $ tfenv install 0.11.10 Usage Show version $ terraform --version Terraform v0.11.10 Init Terraform $ terraform init It’s the rst command you need to execute. Unless, terraform plan, apply, destroy and import will not work. The command terraform init will install : terraform modules eventually a backend and provider(s) plugins Init Terraform and don’t ask any input $ terraform init -input=false Change backend con guration during the init $ terraform init -backend-config=cfg/s3.dev.tf - reconfigure -reconfigure is used in order to tell terraform to not copy the existing state to the new remote state location. Get This command is useful when you have de ned some modules. Modules are vendored so when you edit them, you need to get again modules content. $ terraform get -update=true When you use modules, the rst thing you’ll have to do is to do a terraform get. This pulls modules into the .terraform directory. Once you do that, unless you do another terraform get - update=true, you’ve essentially vendored those modules. Plan The plan step check con guration to execute and write a plan to apply to target infrastructure provider. $ terraform plan -out plan.out It’s an important feature of Terraform that allows a user to see which actions Terraform will perform prior to making any changes, increasing con dence that a change will have the desired effect once applied. When you execute terraform plan command, terraform will scan all *.tf les in your directory and create the plan. Apply Now you have the desired state so you can execute the plan. $ terraform apply plan.out Good to know: Since terraform v0.11+, in an interactive mode (non CI/CD/autonomous pipeline), you can just execute terraform apply command which will print out which actions TF will perform. By generating the plan and applying it in the same command, Terraform can guarantee that the execution plan won’t change, without needing to write it to disk. This reduces the risk of potentially-sensitive data being left behind, or accidentally checked into version control. $ terraform apply Apply and auto approve $ terraform apply -auto-approve Apply and de ne new variables value $ terraform apply -auto-approve -var tags-repository_url=${GIT_URL} Apply only one module $ terraform apply -target=module.s3 This -target option works with terraform plan too. Destroy $ terraform destroy Delete all the resources! A deletion plan can be created before: $ terraform plan –destroy -target option allow to destroy only one resource, for example a S3 bucket : $ terraform destroy -target aws_s3_bucket.my_bucket Debug The Terraform console command is useful for testing interpolations before using them in con gurations. Terraform console will read con gured state even if it is remote. $ echo "aws_iam_user.notif.arn" | terraform console arn:aws:iam::123456789:user/notif Graph $ terraform graph | dot –Tpng > graph.png Visual dependency graph of terraform resources. Validate Validate command is used to validate/check the syntax of the Terraform les. A syntax check is done on all the terraform les in the directory, and will display an error if any of the les doesn’t validate. The syntax check does not cover every syntax common issues. $ terraform validate Providers You can use a lot of providers/plugins in your terraform de nition resources, so it can be useful to have a tree of providers used by modules in your project. $ terraform providers . ├── provider.aws ~> 1.24.0 ├── module.my_module │ ├── provider.aws (inherited) │ ├── provider.null │ └── provider.template └── module.elastic └── provider.aws (inherited)
  • 2. State Pull remote state in a local copy $ terraform state pull > terraform.tfstate Push state in remote backend storage $ terraform state push This command is usefull if for example you riginally use a local tf state and then you de ne a backend storage, in S3 or Consul… How to tell to Terraform you moved a ressource in a module? If you moved an existing resource in a module, you need to update the state: $ terraform state mv aws_iam_role.role1 module.mymodul How to import existing resource in Terraform? If you have an existing resource in your infrastructure provider, you can import it in your Terraform state: $ terraform import aws_iam_policy.elastic_post arn:aws:iam::123456789:policy/elastic_post Workspaces To manage multiple distinct sets of infrastructure resources/environments. Instead of create a directory for each environment to manage, we need to just create needed workspace and use them: Create workspace This command create a new workspace and then select it $ terraform workspace new dev Select a workspace $ terraform workspace select dev List workspaces $ terraform workspace list default * dev prelive Show current workspace $ terraform workspace show dev Tools jq jq is a lightweight command-line JSON processor. Combined with terraform output it can be powerful. Installation For Linux: $ sudo apt-get install jq or $ yum install jq For OS X: $ brew install jq Usage For example, we de nd outputs in a module and when we execute terraform apply outputs are displayed: $ terraform apply ... Apply complete! Resources: 0 added, 0 changed, 0 destroyed. Outputs: elastic_endpoint = vpc-toto-12fgfd4d5f4ds5fngetwe4. eu-central-1.es.amazonaws.com We can extract the value that we want in order to use it in a script for example. With jq it’s easy: $ terraform output -json { "elastic_endpoint": { "sensitive": false, "type": "string", "value": "vpc-toto-12fgfd4d5f4ds5fngetwe4. eu-central-1.es.amazonaws.com" } } $ terraform output -json | jq '.elastic_endpoint.value "vpc-toto-12fgfd4d5f4ds5fngetwe4.eu-central-1. es.amazonaws.com" Terraforming If you have an existing AWS account for examples with existing components like S3 buckets, SNS, VPC … You can use terraforming tool, a tool written in Ruby, which extract existing AWS resources and convert it to Terraform les! Installation $ sudo apt install ruby or $ sudo yum install ruby and $ gem install terraforming Usage Pre-requisites : Like for Terraform, you need to set AWS credentials $ export AWS_ACCESS_KEY_ID="an_aws_access_key" $ export AWS_SECRET_ACCESS_KEY="a_aws_secret_key" $ export AWS_DEFAULT_REGION="eu-central-1" You can also specify credential pro le in ~/.aws/credentials_s and with _–pro le option. $ cat ~/.aws/credentials [aurelie] aws_access_key_id = xxx aws_secret_access_key = xxx aws_default_region = eu-central-1 $ terraforming s3 --profile aurelie Usage $ terraforming --help Commands: terraforming alb # ALB ... terraforming vgw # VPN Gateway terraforming vpc # VPC Example: $ terraforming s3 > aws_s3.tf Remarks: As you can see, terraforming can’t extract for the moment API gateway resources so you need to write it manually. Authors : @aurelievache Cloud Dev(Ops) at Continental v1.0.2