Terraform Interview Questions


You are testing out a new environment and need to ensure you are using the right syntax to reference resources. What are two commands that can help you discover and test the correct interpolation syntax?

– terraform console
– terraform show
How would name a Docker image resource for reference later?

– resources “docker_image”
How do you specify the location of the module you are referencing in the root/main.tf file?

– source = “./modulename”
You need to deploy a development and a production environment simultaneously on the same machine. What feature of Terraform allows you to deploy multiple environments with independent states?

– workspace

You are writing a Terraform script to deploy Docker containers for your organization. You have several items to deploy, what word does Terraform use to refer to each item it deploys?

– Resources
If you change the port of a Docker container in your Terraform script and run a “terraform apply”, what will happen to your deployment?

– The old resource is destroyed and the new one takes its place.
You are referencing infrastructure in an output file. In this resource: “resource “docker_image” “ghost””, which string is the resource ID?

– ghost
You need to be able to specify different variable definitions based on whether the environment is a production or development area. What type of variables rely on key-value pairs in order to be defined dynamically based on other provided information?

– Maps

You are trying to search your system for the file that contains all of your variable definitions. The previous admin has lost it. What extension does the file that holds your variable definitions have?

– .tfvars

Which of the following is an accurate use description for Terraform?

– Terraform is used for building, changing, and versioning infrastructure with safely and efficiently.

You need to output the IP address of your docker container with ID “container_id” in order to provide access to your coworkers. How would you script this output value?

– value = “${docker_container.container_id.ip_address}”

Which provisioner should you use to run a command on the Terraform server during deployment?

– local-exec

At your company, you need to provide a deployment with variables that are defined interactively when applying. For the “image” variable, you want to ensure the user knows this refers to the name of the image. How would you word this in your variable block?

– description = “name of the image”

You are trying to decide how to define your variables. What are three common files in which you can specify the value of a variable without having to use a function to extract them? (Choose all that apply)

– variables.tf
– main.tf
– terraform.tfvars

You have a variable that changes often and you want the ability to modify it during your Terraform apply, either via an external file or by entering it interactively. How should this variable be expressed in the variables.tf file?

– variable “image” {}

The best place to learn Terraform is :Linux Academy

Common commands in Terraform


$ terraform help

Common commands:

apply              Builds or changes infrastructure

console            Interactive console for Terraform interpolations

destroy            Destroy Terraform-managed infrastructure

env                Workspace management

fmt                Rewrites config files to canonical format

get                Download and install modules for the configuration

graph              Create a visual graph of Terraform resources

import             Import existing infrastructure into Terraform

init               Initialize a Terraform working directory

output             Read an output from a state file

plan               Generate and show an execution plan

providers          Prints a tree of the providers used in the config

push               Upload this Terraform module to Atlas to run

refresh            Update local state file against real resources

show               Inspect Terraform state or plan

taint              Manually mark a resource for recreation

provider.docker: Error pinging Docker server: Got permission denied while trying to connect to the Docker daemon socket


provider.docker: Error pinging Docker server: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/_ping: dial unix /var/run/docker.sock: connect: permission denied

 

aryan@aryan-VirtualBox:~/docker$ terraform plan
Refreshing Terraform state in-memory prior to plan…
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
————————————————————————

Error: Error running plan: 1 error(s) occurred:

* provider.docker: Error pinging Docker server: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/_ping: dial unix /var/run/docker.sock: connect: permission denied
aryan@aryan-VirtualBox:~/docker$

try it with sudo command

aryan@aryan-VirtualBox:~/docker$ sudo terraform apply

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:

+ docker_image.image_id
id: <computed>
latest: <computed>
name: “ghost:latest”
Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
Terraform will perform the actions described above.
Only ‘yes’ will be accepted to approve.

Enter a value: yes

docker_image.image_id: Creating…
latest: “” => “<computed>”
name: “” => “ghost:latest”
docker_image.image_id: Still creating… (10s elapsed)
docker_image.image_id: Still creating… (20s elapsed)
docker_image.image_id: Creation complete after 20s (ID: sha256:f32905852788d222be7b05357a906cf9…2f50b7cea5a0ecf229d892d9d9ghost:latest)

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
aryan@aryan-VirtualBox:~/docker$

Steps to install terraform on Ubuntu / Ubuntu cloud server :


  1. Install unzip
    sudo apt-get install unzip
    
  2. Download latest version of the terraform
    wget https://releases.hashicorp.com/terraform/0.11.13/terraform_0.11.13_linux_amd64.zip
    
  3. Extract the downloaded file archive
    unzip terraform_0.11.13_linux_amd64.zip
    
  4. Move the executable into a directory searched for executables
    sudo mv terraform /usr/local/bin/
    
  5. Run it
    terraform --version

 

aryan@aryan-VirtualBox:~$ sudo apt-get install unzip
[sudo] password for aryan:
Reading package lists… Done
Building dependency tree
Reading state information… Done
unzip is already the newest version (6.0-21ubuntu1).
0 upgraded, 0 newly installed, 0 to remove and 5 not upgraded.
aryan@aryan-VirtualBox:~$ wget https://releases.hashicorp.com/terraform/0.11.13/terraform_0.11.13_linux_amd64.zip
–2019-05-03 11:59:38– https://releases.hashicorp.com/terraform/0.11.13/terraform_0.11.13_linux_amd64.zip
Resolving releases.hashicorp.com (releases.hashicorp.com)… 151.101.1.183, 151.101.193.183, 151.101.129.183, …
Connecting to releases.hashicorp.com (releases.hashicorp.com)|151.101.1.183|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 21128942 (20M) [application/zip]
Saving to: ‘terraform_0.11.13_linux_amd64.zip’

terraform_0.11.13_l 100%[===================>] 20.15M 69.3MB/s in 0.3s

2019-05-03 11:59:38 (69.3 MB/s) – ‘terraform_0.11.13_linux_amd64.zip’ saved [21128942/21128942]

aryan@aryan-VirtualBox:~$ unzip terraform_0.11.13_linux_amd64.zip
Archive: terraform_0.11.13_linux_amd64.zip
inflating: terraform
aryan@aryan-VirtualBox:~$ sudo mv terraform /usr/local/bin/
aryan@aryan-VirtualBox:~$ terraform –version
Terraform v0.11.13