Updated Terraform: Missing answer, clarified some questions, removed duplicate, fixed typos (#10323)

This commit is contained in:
Chris Schindlbeck 2024-08-31 10:14:29 +02:00 committed by GitHub
parent 4863152cef
commit 193c4302d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 74 additions and 44 deletions

View File

@ -422,7 +422,7 @@ True
According to variable precedence, which source will be used first?</summary><br><b> According to variable precedence, which source will be used first?</summary><br><b>
The order is: Terraform loads variables in the following order, with later sources taking precedence over earlier ones:
- Environment variable - Environment variable
- The file `terraform.tfvars` - The file `terraform.tfvars`
@ -487,9 +487,9 @@ You have multiple hardcoded values that repeat themselves in different sections,
``` ```
variable "app_id" { variable "app_id" {
type = string type = string
description = "The id of application" description = "The id of application"
default = "some_value" default = "some_value"
} }
``` ```
@ -716,6 +716,9 @@ Since a provisioner can run a variety of actions, it's not always feasible to pl
<details> <details>
<summary>What is <code>local-exec</code> and <code>remote-exec</code> in the context of provisioners?</summary><br><b> <summary>What is <code>local-exec</code> and <code>remote-exec</code> in the context of provisioners?</summary><br><b>
<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.
</b></details> </b></details>
<details> <details>
@ -747,11 +750,6 @@ There are quite a few cases you might need to use them:
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> 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>
</b></details> </b></details>
<details>
<summary>Explain <code>remote-exec</code> and <code>local-exec</code></summary><br><b>
</b></details>
<details> <details>
<summary>Explain "Remote State". When would you use it and how?</summary><br><b> <summary>Explain "Remote State". When would you use it and how?</summary><br><b>
Terraform generates a `terraform.tfstate` json file that describes components/service provisioned on the specified provider. Remote Terraform generates a `terraform.tfstate` json file that describes components/service provisioned on the specified provider. Remote
@ -911,7 +909,7 @@ terraform {
} }
} }
``` ```
7. Run `teraform init` as it will configure the backend 7. Run `terraform init` as it will configure the backend
</b></details> </b></details>
@ -1044,14 +1042,25 @@ resource "some_resource" "some_name" {
</b></details> </b></details>
<details> <details>
<summary>You have a list variable called "users". How to access the second item in that list and attribute called "name"?</summary><br><b> <summary>You have a list variable called "users" with an object containing a name attribute like this:<br>
```
variable "users" {
type = list(object({
name = string
age = number
}))
}
```
How to access the name attribute of the second item in that list?</summary><br><b>
`users[1].name` `users[1].name`
</b></details> </b></details>
<details> <details>
<summary>You have a list variable called "users". How to access attribute "name" of all items?</summary><br><b> <summary>Given the same list, how to access attribute "name" of all items?</summary><br><b>
`users[*].name` `users[*].name`
@ -1147,7 +1156,7 @@ resource “google_compute_instance” “instances” {
</b></details> </b></details>
<details> <details>
<summary>The following resource tries to use for_each loop on a list of string but it fails, why? <summary>The following resource tries to use for_each loop on a list of strings but it fails, why?
``` ```
resource “google_compute_instance” “instances” { resource “google_compute_instance” “instances” {
@ -1261,7 +1270,7 @@ output "name_and_age" {
</b></details> </b></details>
<details> <details>
<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> <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>
``` ```
output "name_and_age" { output "name_and_age" {
@ -1357,7 +1366,7 @@ Renders a template file and returns the result as string.
<details> <details>
<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> <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>
Switch relative paths with what is known as path references. These are fixes paths like module root path, module expression file path, etc. Switch relative paths with what is known as path references. These are fixes: paths like module root path, module expression file path, etc.
</b></details> </b></details>
@ -1387,7 +1396,7 @@ False. terraform console is ready-only.
<details> <details>
<summary>Explain what <code>depends_on</code> used for and given an example</summary><br><b> <summary>Explain what <code>depends_on</code> used for and given an example</summary><br><b>
`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. `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.
</b></details> </b></details>
@ -1490,7 +1499,7 @@ module "amazing_module" {
<details> <details>
<summary>What should be done every time you modify the source parameter of a module?</summary><br><b> <summary>What should be done every time you modify the source parameter of a module?</summary><br><b>
`terraform init` should be executed as it takes care of downloading and installing the module from the new path. `terraform get -update` should be executed as it takes care of downloading and installing the module from the new path.
</b></details> </b></details>
<details> <details>
@ -1550,9 +1559,11 @@ It's does NOT create the definitions/configuration for creating such infrastruct
<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> <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>
``` ```
.terraform **/.terraform/*
*.tfstate *.tfstate
*.tfstate.backup *.tfstate.*
*.tfvars
*.tfvars.json
``` ```
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. 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
### AWS ### AWS
<details> <details>
<summary>What happens if you update user_data in the following case apply the changes? <summary>What happens if you update user_data in the following case and apply the changes?
``` ```
resource "aws_instance" "example" { resource "aws_instance" "example" {
ami = "..." ami = "..."
instance_type = "t2.micro" instance_type = "t2.micro"
user_data = <<-EOF user_data = <<-EOF
#!/bin/bash #!/bin/bash
echo "Hello, World" > index.xhtml echo "Hello, World" > index.xhtml
EOF EOF
}
``` ```
</summary><br><b> </summary><br><b>
@ -1787,7 +1799,7 @@ terraform_project/
Each environment has its own backend (as you don't want to use the same authentication and access controls for all environments) Each environment has its own backend (as you don't want to use the same authentication and access controls for all environments)
Going further, under each environment you'll separate between comoponents, applications and services Going further, under each environment you'll separate between components, applications and services
``` ```

View File

@ -11,6 +11,8 @@ resource "aws_s3_bucket" "some_bucket" {
} }
``` ```
Attention: Since S3 buckets are globally unique, you will likely have to rename the bucket as someone else might have named it that way already.
## Objectives ## Objectives
1. Rename an existing S3 bucket and make sure it's still tracked by Terraform 1. Rename an existing S3 bucket and make sure it's still tracked by Terraform

View File

@ -11,6 +11,8 @@ resource "aws_s3_bucket" "some_bucket" {
} }
``` ```
Attention: Since S3 buckets are globally unique, you will likely have to rename the bucket as someone else might have named it that way already.
## Objectives ## Objectives
1. Rename an existing S3 bucket and make sure it's still tracked by Terraform 1. Rename an existing S3 bucket and make sure it's still tracked by Terraform
@ -24,10 +26,12 @@ aws s3 mb s3://some-new-bucket-123
# Sync old bucket to new bucket # Sync old bucket to new bucket
aws s3 sync s3://some-old-bucket s3://some-new-bucket-123 aws s3 sync s3://some-old-bucket s3://some-new-bucket-123
# Remove the old bucket from Terraform's state # Option 1 (remove and import)
## Remove the old bucket from Terraform's state
terraform state rm aws_s3_bucket.some_bucket terraform state rm aws_s3_bucket.some_bucket
# Import new bucket to Terraform's state ## Import new bucket to Terraform's state
terraform import aws_s3_bucket.some_bucket some-new-bucket-123 terraform import aws_s3_bucket.some_bucket some-new-bucket-123
: ' : '
@ -38,6 +42,18 @@ The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform. your Terraform state and will henceforth be managed by Terraform.
' '
# Option 2 (move)
## Move the old bucket from Terraform's state to the new one
terraform state mv aws_s3_bucket.some_bucket some-new-bucket-123
: '
Move "aws_s3_bucket.some_bucket" to "aws_s3_bucket.some-new-bucket-123"
Successfully moved 1 object(s).
'
# Modify Terraform file
# Modify the Terraform definition to include the new name # Modify the Terraform definition to include the new name
# resource "aws_s3_bucket" "some_bucket" { # resource "aws_s3_bucket" "some_bucket" {
# bucket = "some-new-bucket-123" # bucket = "some-new-bucket-123"