You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: topics/terraform/README.md
+52-40Lines changed: 52 additions & 40 deletions
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Terraform
1
+
# Terraform
2
2
3
3
-[Terraform](#terraform)
4
4
-[Exercises](#exercises)
@@ -71,7 +71,7 @@
71
71
- Full automation: In the past, resource creation, modification and removal were handled manually or by using a set of tooling. With Terraform or other IaC technologies, you manage the full lifecycle in an automated fashion.<br>
72
72
- Modular and Reusable: Code that you write for certain purposes can be used and assembled in different ways. You can write code to create resources on a public cloud and it can be shared with other teams who can also use it in their account on the same (or different) cloud><br>
73
73
- Improved testing: Concepts like CI can be easily applied on IaC based projects and code snippets. This allow you to test and verify operations beforehand
74
-
-
74
+
-
75
75
</b></details>
76
76
77
77
<details>
@@ -80,7 +80,7 @@
80
80
- Declarative: Terraform uses the declarative approach (rather than the procedural one) in order to define end-status of the resources
81
81
- No agents: as opposed to other technologies (e.g. Puppet) where you use a model of agent and server, with Terraform you use the different APIs (of clouds, services, etc.) to perform the operations
82
82
- Community: Terraform has strong community who constantly publishes modules and fixes when needed. This ensures there is good modules maintenance and users can get support quite quickly at any point
83
-
-
83
+
-
84
84
</b></details>
85
85
86
86
<details>
@@ -186,7 +186,7 @@ Run `terraform apply`. That will apply the changes described in your .tf files.
186
186
A user should be careful with this command because there is no way to revert it. Sure, you can always run again "apply" but that can take time, generates completely new resources, etc.
187
187
</b></details>
188
188
189
-
### Dependencies
189
+
### Dependencies
190
190
191
191
<details>
192
192
<summary>Sometimes you need to reference some resources in the same or separate .tf file. Why and how it's done?</summary><br><b>
@@ -201,7 +201,7 @@ In your AWS instance it would like that:
Yes, when there is a dependency between different Terraform resources, you want the resources to be created in the right order and this is exactly what Terraform does.
217
217
218
-
To make it ever more clear, if you have a resource X that references the ID of resource Y, it doesn't makes sense to create first resource X because it won't have any ID to get from a resource that wasn't created yet.
218
+
To make it ever more clear, if you have a resource X that references the ID of resource Y, it doesn't makes sense to create first resource X because it won't have any ID to get from a resource that wasn't created yet.
219
219
</b></details>
220
220
221
221
<details>
@@ -237,7 +237,7 @@ The output is in DOT - A graph description language.
237
237
<details>
238
238
<summary>Where can you find publicly available providers?</summary><br><b>
239
239
240
-
In the [Terraform Registry](https://registry.terraform.io/browse/providers)
240
+
In the [Terraform Registry](https://registry.terraform.io/browse/providers)
241
241
</b></details>
242
242
243
243
<details>
@@ -419,10 +419,10 @@ True
419
419
- The file `terraform.tfvars`
420
420
- Environment variable
421
421
- Using `-var` or `-var-file`
422
-
422
+
423
423
According to variable precedence, which source will be used first?</summary><br><b>
424
424
425
-
The order is:
425
+
Terraform loads variables in the following order, with later sources taking precedence over earlier ones:
426
426
427
427
- Environment variable
428
428
- The file `terraform.tfvars`
@@ -487,9 +487,9 @@ You have multiple hardcoded values that repeat themselves in different sections,
487
487
488
488
```
489
489
variable "app_id" {
490
-
type = string
490
+
type = string
491
491
description = "The id of application"
492
-
default = "some_value"
492
+
default = "some_value"
493
493
}
494
494
```
495
495
@@ -638,7 +638,7 @@ data "aws_vpc" "default {
638
638
}
639
639
```
640
640
641
-
You can retrieve the ID attribute this way: `data.aws_vpc.default.id`
641
+
You can retrieve the ID attribute this way: `data.aws_vpc.default.id`
642
642
</b></details>
643
643
644
644
<details>
@@ -716,6 +716,9 @@ Since a provisioner can run a variety of actions, it's not always feasible to pl
716
716
717
717
<details>
718
718
<summary>What is <code>local-exec</code> and <code>remote-exec</code> in the context of provisioners?</summary><br><b>
719
+
720
+
<code>local-exec</code> provisioners run commands on the machine where Terraform is executed, while <code>remote-exec</code> provisioners run commands on the remote resource.
721
+
719
722
</b></details>
720
723
721
724
<details>
@@ -747,11 +750,6 @@ There are quite a few cases you might need to use them:
747
750
Output variables are named values that are sourced from the attributes of a module. They are stored in terraform state, and can be used by other modules through <code>remote_state</code>
748
751
</b></details>
749
752
750
-
<details>
751
-
<summary>Explain <code>remote-exec</code> and <code>local-exec</code></summary><br><b>
752
-
</b></details>
753
-
754
-
755
753
<details>
756
754
<summary>Explain "Remote State". When would you use it and how?</summary><br><b>
757
755
Terraform generates a `terraform.tfstate` json file that describes components/service provisioned on the specified provider. Remote
@@ -833,7 +831,7 @@ There is more than one answer to this question. It's very much depends on whethe
833
831
- tfstate contains credentials in plain text. You don't want to put it in publicly shared location
834
832
- tfstate shouldn't be modified concurrently so putting it in a shared location available for everyone with "write" permissions might lead to issues. (Terraform remote state doesn't has this problem).
835
833
- tfstate is an important file. As such, it might be better to put it in a location that has regular backups and good security.
836
-
834
+
837
835
As such, tfstate shouldn't be stored in git repositories. secured storage such as secured buckets, is a better option.
838
836
839
837
</b></details>
@@ -855,7 +853,7 @@ In general, storing state file on your computer isn't a problem. It starts to be
855
853
856
854
- Don't edit it manually. tfstate was designed to be manipulated by terraform and not by users directly.
857
855
- Store it in secured location (since it can include credentials and sensitive data in general)
858
-
- Backup it regularly so you can roll-back easily when needed
856
+
- Backup it regularly so you can roll-back easily when needed
859
857
- Store it in remote shared storage. This is especially needed when working in a team and the state can be updated by any of the team members
860
858
- Enabled versioning if the storage where you store the state file, supports it. Versioning is great for backups and roll-backs in case of an issue.
861
859
@@ -902,7 +900,7 @@ Let's say we chose use Amazon s3 as a remote Terraform backend where we can stor
902
900
4. Block public access
903
901
5. Handle locking. One way is to add DB for it
904
902
6. Add the point you'll want to run init and apply commands to avoid an issue where you at the same time create the resources for remote backend and also switch to a remote backend
905
-
7. Once resources were created, add Terraform backend code
903
+
7. Once resources were created, add Terraform backend code
906
904
907
905
```
908
906
terraform {
@@ -911,7 +909,7 @@ terraform {
911
909
}
912
910
}
913
911
```
914
-
7. Run `teraform init` as it will configure the backend
912
+
7. Run `terraform init` as it will configure the backend
<summary>The following resource tries to use for_each loop on a list of string but it fails, why?
1159
+
<summary>The following resource tries to use for_each loop on a list of strings but it fails, why?
1151
1160
1152
1161
```
1153
1162
resource “google_compute_instance” “instances” {
1154
-
1163
+
1155
1164
for_each = var.names
1156
1165
name = each.value
1157
1166
}
@@ -1261,11 +1270,11 @@ output "name_and_age" {
1261
1270
</b></details>
1262
1271
1263
1272
<details>
1264
-
<summary>You have a map variable, called "users", with the keys "name" (string) and "age" (float). Define an output map variable with the key being name in uppercase and value being age in the closest whole number </summary><br><b>
1273
+
<summary>You have a map variable, called "users", with the keys "name" (string) and "age" (number). Define an output map variable with the key being name in uppercase and value being age in the closest whole number </summary><br><b>
1265
1274
1266
1275
```
1267
1276
output "name_and_age" {
1268
-
value = {for name, age in var.users : upper(name) => floor(age)
1277
+
value = {for name, age in var.users : upper(name) => floor(age)
1269
1278
}
1270
1279
```
1271
1280
@@ -1357,7 +1366,7 @@ Renders a template file and returns the result as string.
1357
1366
<details>
1358
1367
<summary>You are trying to use templatefile as part of a module and you use a relative path to load a file but sometimes it fails, especially when others try to reuse the module. How can you deal with that?</summary><br><b>
1359
1368
1360
-
Switch relative paths with what is known as path references. These are fixes paths like module root path, module expression file path, etc.
1369
+
Switch relative paths with what is known as path references. These are fixes: paths like module root path, module expression file path, etc.
1361
1370
1362
1371
</b></details>
1363
1372
@@ -1387,7 +1396,7 @@ False. terraform console is ready-only.
1387
1396
<details>
1388
1397
<summary>Explain what <code>depends_on</code> used for and given an example</summary><br><b>
1389
1398
1390
-
`depends_on` used to create a dependency between resources in Terraform. For example, there is an application you would like to deploy in a cluster. If the cluster isn't ready (and also managed by Terraform of course) then you can't deploy the app. In this case, you will define "depends_on" in the app configuration and its value will be the cluster resource.
1399
+
`depends_on` used to create an explicit dependency between resources in Terraform. For example, there is an application you would like to deploy in a cluster. If the cluster isn't ready (and also managed by Terraform of course) then you can't deploy the app. In this case, you will define "depends_on" in the app configuration and its value will be the cluster resource.
1391
1400
1392
1401
</b></details>
1393
1402
@@ -1490,7 +1499,7 @@ module "amazing_module" {
1490
1499
<details>
1491
1500
<summary>What should be done every time you modify the source parameter of a module?</summary><br><b>
1492
1501
1493
-
`terraform init` should be executed as it takes care of downloading and installing the module from the new path.
1502
+
`terraform get -update` should be executed as it takes care of downloading and installing the module from the new path.
1494
1503
</b></details>
1495
1504
1496
1505
<details>
@@ -1550,9 +1559,11 @@ It's does NOT create the definitions/configuration for creating such infrastruct
1550
1559
<summary>You have a Git repository with Terraform files but no .gitignore. What would you add to a .gitignore file in Terraform repository?</summary><br><b>
1551
1560
1552
1561
```
1553
-
.terraform
1562
+
**/.terraform/*
1554
1563
*.tfstate
1555
-
*.tfstate.backup
1564
+
*.tfstate.*
1565
+
*.tfvars
1566
+
*.tfvars.json
1556
1567
```
1557
1568
1558
1569
You don't want to store state file nor any downloaded providers in .terraform directory. It also doesn't makes sense to share/store the state backup files.
@@ -1562,17 +1573,18 @@ You don't want to store state file nor any downloaded providers in .terraform di
1562
1573
### AWS
1563
1574
1564
1575
<details>
1565
-
<summary>What happens if you update user_data in the following case apply the changes?
1576
+
<summary>What happens if you update user_data in the following case and apply the changes?
1566
1577
1567
1578
```
1568
1579
resource "aws_instance" "example" {
1569
1580
ami = "..."
1570
1581
instance_type = "t2.micro"
1571
1582
1572
-
user_data = <<-EOF
1573
-
#!/bin/bash
1574
-
echo "Hello, World" > index.xhtml
1575
-
EOF
1583
+
user_data = <<-EOF
1584
+
#!/bin/bash
1585
+
echo "Hello, World" > index.xhtml
1586
+
EOF
1587
+
}
1576
1588
```
1577
1589
</summary><br><b>
1578
1590
@@ -1703,7 +1715,7 @@ provider "aws" {
1703
1715
```
1704
1716
</summary><br><b>
1705
1717
1706
-
It's not secure! you should never store credentials in plain text this way.
1718
+
It's not secure! you should never store credentials in plain text this way.
1707
1719
1708
1720
</b></details>
1709
1721
@@ -1787,7 +1799,7 @@ terraform_project/
1787
1799
1788
1800
Each environment has its own backend (as you don't want to use the same authentication and access controls for all environments)
1789
1801
1790
-
Going further, under each environment you'll separate between comoponents, applications and services
1802
+
Going further, under each environment you'll separate between components, applications and services
0 commit comments