Skip to content

Commit e0b7ab9

Browse files
Create Terraform.md
1 parent 3dd3ee0 commit e0b7ab9

File tree

1 file changed

+208
-0
lines changed

1 file changed

+208
-0
lines changed

DevOps/Terraform.md

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
About Terraform CLI
2+
Terraform, a tool created by Hashicorp in 2014, written in Go, aims to build, change and version control your infrastructure. This tool
3+
have a powerfull and very intuitive Command Line Interface.
4+
Installation
5+
Install through curl
6+
$ curl -O https://releases.hashicorp.com/terraform/
7+
0.11.10/terraform_0.11.10_linux_amd64.zip
8+
$ sudo unzip terraform_0.11.10_linux_amd64.zip
9+
-d /usr/local/bin/
10+
$ rm terraform_0.11.10_linux_amd64.zip
11+
OR install through tfenv: a Terraform version manager
12+
First of all, download the tfenv binary and put it in your PATH.
13+
$ git clone https://github.com/Zordrak/tfenv.git
14+
~/.tfenv
15+
$ echo 'export PATH="$HOME/.tfenv/bin:$PATH"'
16+
>> $HOME/bashrc
17+
Then, you can install desired version of terraform:
18+
$ tfenv install 0.11.10
19+
Usage
20+
Show version
21+
$ terraform --version
22+
Terraform v0.11.10
23+
Init Terraform
24+
$ terraform init
25+
It’s the rst command you need to execute. Unless, terraform
26+
plan, apply, destroy and import will not work. The command
27+
terraform init will install :
28+
terraform modules
29+
eventually a backend
30+
and provider(s) plugins
31+
Init Terraform and don’t ask any input
32+
$ terraform init -input=false
33+
Change backend conguration during the init
34+
$ terraform init -backend-config=cfg/s3.dev.tf -
35+
reconfigure
36+
-reconfigure is used in order to tell terraform to not copy the
37+
existing state to the new remote state location.
38+
Get
39+
This command is useful when you have dened some modules.
40+
Modules are vendored so when you edit them, you need to get
41+
again modules content.
42+
$ terraform get -update=true
43+
When you use modules, the rst thing you’ll have to do is to do a
44+
terraform get. This pulls modules into the .terraform directory.
45+
Once you do that, unless you do another terraform get -
46+
update=true, you’ve essentially vendored those modules.
47+
Plan
48+
The plan step check conguration to execute and write a plan to apply to target infrastructure provider.
49+
$ terraform plan -out plan.out
50+
It’s an important feature of Terraform that allows a user to see which actions Terraform will perform prior to making any changes,
51+
increasing condence that a change will have the desired effect once applied.
52+
When you execute terraform plan command, terraform will scan all *.tf les in your directory and create the plan.
53+
Apply
54+
Now you have the desired state so you can execute the plan.
55+
$ terraform apply plan.out
56+
Good to know: Since terraform v0.11+, in an interactive mode (non
57+
CI/CD/autonomous pipeline), you can just execute terraform apply command which will print out which actions TF will
58+
perform. By generating the plan and applying it in the same command,Terraform can guarantee that the execution plan won’t change,
59+
without needing to write it to disk. This reduces the risk of potentially-sensitive data being left behind, or accidentally
60+
checked into version control.
61+
$ terraform apply
62+
Apply and auto approve
63+
$ terraform apply -auto-approve
64+
Apply and dene new variables value
65+
$ terraform apply -auto-approve
66+
-var tags-repository_url=${GIT_URL}
67+
Apply only one module
68+
$ terraform apply -target=module.s3
69+
This -target option works with terraform plan too.
70+
Destroy
71+
$ terraform destroy
72+
Delete all the resources!
73+
A deletion plan can be created before:
74+
$ terraform plan –destroy
75+
-target option allow to destroy only one resource, for example a
76+
S3 bucket :
77+
$ terraform destroy -target aws_s3_bucket.my_bucket
78+
Debug
79+
The Terraform console command is useful for testing interpolations before using them in congurations. Terraform
80+
console will read congured state even if it is remote.
81+
$ echo "aws_iam_user.notif.arn" | terraform console
82+
arn:aws:iam::123456789:user/notif
83+
Graph
84+
$ terraform graph | dot –Tpng > graph.png
85+
Visual dependency graph of terraform resources.
86+
Validate
87+
Validate command is used to validate/check the syntax of the
88+
Terraform les. A syntax check is done on all the terraform les in
89+
the directory, and will display an error if any of the les doesn’t
90+
validate. The syntax check does not cover every syntax common
91+
issues.
92+
$ terraform validate
93+
Providers
94+
You can use a lot of providers/plugins in your terraform denition
95+
resources, so it can be useful to have a tree of providers used by
96+
modules in your project.
97+
$ terraform providers
98+
.
99+
├── provider.aws ~> 1.24.0
100+
├── module.my_module
101+
│ ├── provider.aws (inherited)
102+
│ ├── provider.null
103+
│ └── provider.template
104+
└── module.elastic
105+
└── provider.aws (inherited)
106+
State
107+
Pull remote state in a local copy
108+
$ terraform state pull > terraform.tfstate
109+
Push state in remote backend storage
110+
$ terraform state push
111+
This command is usefull if for example you riginally use a local tf
112+
state and then you dene a backend storage, in S3 or Consul…
113+
How to tell to Terraform you moved a ressource in a
114+
module?
115+
If you moved an existing resource in a module, you need to update
116+
the state:
117+
$ terraform state mv aws_iam_role.role1 module.mymodul
118+
How to import existing resource in Terraform?
119+
If you have an existing resource in your infrastructure provider,
120+
you can import it in your Terraform state:
121+
$ terraform import aws_iam_policy.elastic_post
122+
arn:aws:iam::123456789:policy/elastic_post
123+
Workspaces
124+
To manage multiple distinct sets of infrastructure
125+
resources/environments.
126+
Instead of create a directory for each environment to manage, we
127+
need to just create needed workspace and use them:
128+
Create workspace
129+
This command create a new workspace and then select it
130+
$ terraform workspace new dev
131+
Select a workspace
132+
$ terraform workspace select dev
133+
List workspaces
134+
$ terraform workspace list
135+
default
136+
* dev
137+
prelive
138+
Show current workspace
139+
$ terraform workspace show
140+
dev
141+
Tools
142+
jq
143+
jq is a lightweight command-line JSON processor. Combined with
144+
terraform output it can be powerful.
145+
Installation
146+
For Linux:
147+
$ sudo apt-get install jq
148+
or
149+
$ yum install jq
150+
For OS X:
151+
$ brew install jq
152+
Usage
153+
For example, we dend outputs in a module and when we execute
154+
terraform apply outputs are displayed:
155+
$ terraform apply
156+
...
157+
Apply complete! Resources: 0 added, 0 changed,
158+
0 destroyed.
159+
Outputs:
160+
elastic_endpoint = vpc-toto-12fgfd4d5f4ds5fngetwe4.
161+
eu-central-1.es.amazonaws.com
162+
We can extract the value that we want in order to use it in a script
163+
for example. With jq it’s easy:
164+
$ terraform output -json
165+
{
166+
"elastic_endpoint": {
167+
"sensitive": false,
168+
"type": "string",
169+
"value": "vpc-toto-12fgfd4d5f4ds5fngetwe4.
170+
eu-central-1.es.amazonaws.com"
171+
}
172+
}
173+
$ terraform output -json | jq '.elastic_endpoint.value
174+
"vpc-toto-12fgfd4d5f4ds5fngetwe4.eu-central-1.
175+
es.amazonaws.com"
176+
Terraforming
177+
If you have an existing AWS account for examples with existing
178+
components like S3 buckets, SNS, VPC … You can use
179+
terraforming tool, a tool written in Ruby, which extract existing
180+
AWS resources and convert it to Terraform les!
181+
Installation
182+
$ sudo apt install ruby or $ sudo yum install ruby
183+
and
184+
$ gem install terraforming
185+
Usage
186+
Pre-requisites :
187+
Like for Terraform, you need to set AWS credentials
188+
$ export AWS_ACCESS_KEY_ID="an_aws_access_key"
189+
$ export AWS_SECRET_ACCESS_KEY="a_aws_secret_key"
190+
$ export AWS_DEFAULT_REGION="eu-central-1"
191+
You can also specify credential prole in ~/.aws/credentials_s and
192+
with _–prole option.
193+
$ cat ~/.aws/credentials
194+
[aurelie]
195+
aws_access_key_id = xxx
196+
aws_secret_access_key = xxx
197+
aws_default_region = eu-central-1
198+
$ terraforming s3 --profile aurelie
199+
Usage
200+
$ terraforming --help
201+
Commands:
202+
terraforming alb # ALB
203+
...
204+
terraforming vgw # VPN Gateway
205+
terraforming vpc # VPC
206+
Example:
207+
$ terraforming s3 > aws_s3.tf
208+
Remarks: As you can see, terraforming can’t extract for the moment API gateway resources so you need to write it manually.

0 commit comments

Comments
 (0)