Navigate Terraform workflow Flashcards

1
Q

What is the Core Terraform workflow?

A

The core Terraform workflow has three steps:

  1. Write - Author infrastructure as code.
  2. Plan - Preview changes before applying.
  3. Apply - Provision reproducible infrastructure.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the workflow when you work as an Individual Practitioner?

A

https://www.terraform.io/guides/core-workflow.html#working-as-an-individual-practitioner

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the workflow when you work as a team?

A

https://www.terraform.io/guides/core-workflow.html#working-as-a-team

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the workflow when you work as a large organization?

A

https://www.terraform.io/guides/core-workflow.html#the-core-workflow-
enhanced-by-terraform-cloud

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the command init?

A

The terraform init command is used to initialize a working directory
containing Terraform configuration files.
This is the first command that should be run after writing a new
Terraform configuration or cloning an existing one from version
control.
It is safe to run this command multiple times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

You recently joined a team and you cloned a terraform configuration files from
the version control system. What is the first command you should use?

A

terraform init
This command performs several different initialization steps in order
to prepare a working directory for use.
This command is always safe to run multiple times, to bring the
working directory up to date with changes in the configuration.Though subsequent runs may give errors, this command will never
delete your existing configuration or state.
If no arguments are given, the configuration in the current working
directory is initialized. It is recommended to run Terraform with the
current working directory set to the root directory of the
configuration, and omit the DIR argument.
https://www.terraform.io/docs/commands/init.html

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the flag you should use to upgrade modules and plugins a part of their
respective installation steps?

A

upgrade

terraform init -upgrade

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When you are doing initialization with terraform init, you want to skip backend
initialization. What should you do?

A

terraform init -backend=false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
When you are doing initialization with terraform init, you want to skip child
module installation. What should you do?
A

terraform init -get=false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When you are doing initialization where do all the plugins stored?

A

On most operationg systems
~/.terraform.d/plugins

on Windows
%APPDATA%\terraform.d\plugins

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When you are doing initialization with terraform init, you want to skip plugin
installation. What should you do?

A

terraform init -get-plugins=false
Skips plugin installation. Terraform will use plugins installed in
the user plugins directory, and any plugins already installed for the
current working directory. If the installed plugins aren’t sufficient
for the configuration, init fails.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the command terraform validate does?

A

The terraform validate command validates the configuration files in a
directory, referring only to the configuration and not accessing any
remote services such as remote state, provider APIs, etc.
Validate runs checks that verify whether a configuration is
syntactically valid and internally consistent, regardless of any
provided variables or existing state.
It is thus primarily useful for general verification of reusable
modules, including correctness of attribute names and value types.
https://www.terraform.io/docs/commands/validate.html

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the command plan do?

A

The terraform plan command is used to create an execution plan.
Terraform performs a refresh, unless explicitly disabled, and then
determines what actions are necessary to achieve the desired state
specified in the configuration files.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the command apply do?

A

The terraform apply command is used to apply the changes required to
reach the desired state of the configuration, or the pre-determined
set of actions generated by a terraform plan execution plan.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

You are applying the infrastructure with the command apply and you don’t want
to do interactive approval. Which flag should you use?

A

terraform apply -auto-approve

https://www.terraform.io/docs/commands/apply.html

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the command destroy do?

A

The terraform destroy command is used to destroy the Terraform-managed infrastructure.

17
Q

How do you preview the behavior of the command terraform destroy?

A

terraform plan -destroy

18
Q

What are implicit and explicit dependencies?

A

Implicit dependency:
By studying the resource attributes used in interpolation expressions, Terraform can automatically infer when one resource depends on another.
Terraform uses this dependency information to determine the correct order in which to create the different resources.
Implicit dependencies via interpolation expressions are the primary way to inform Terraform about these relationships, and should be used whenever possible.

Explicit dependency:
Sometimes there are dependencies between resources that are not visible to Terraform. The depends_on argument is accepted by any resource and accepts a list of resources to create explicit
dependencies for.

19
Q

Give an example of implicit dependency?

A
In the example below, the reference to aws_instance.example.id
creates an implicit dependency on the aws_instance named example.
provider "aws" {
profile = "default"
region = "us-east-1"
}
resource "aws_instance" "example" {
ami = "ami-b374d5a5"
instance_type = "t2.micro"
}
resource "aws_eip" "ip" {
vpc = true
instance = aws_instance.example.id
}
20
Q

Give an example of explicit dependency?

A

In the example below, an application we will run on our EC2 instance
expects to use a specific Amazon S3 bucket, but that dependency is
configured inside the application code and thus not visible to
Terraform. In that case, we can use depends_on to explicitly declare the dependency

resource "aws_s3_bucket" "example" {
bucket = "some_bucket"
acl = "private"
}
resource "aws_instance" "example" {
ami = "ami-2757f631"
instance_type = "t2.micro"
}
depends_on = [aws_s3_bucket.example]
}
21
Q

How do you save the execution plan?

A

terraform plan -out=tfplan
you can use that file with apply
terraform apply tfplan

22
Q

You have started writing terraform configuration and you are using some sample
configuration as a basis. How do you copy the example configuration into your
working directory?

A

terraform init -from-module=MODULE-SOURCE

https://www.terraform.io/docs/commands/init.html#copy-a-source-module

23
Q

What is the flag you should use with the terraform plan to get detailed on the exit codes?

A

terraform plan -detailed-exitcode
Return a detailed exit code when the command exits. When provided,
this argument changes the exit codes and their meanings to provide
more granular information about what the resulting plan contains:
* 0 = Succeeded with empty diff (no changes)
* 1 = Error
* 2 = Succeeded with non-empty diff (changes present)

24
Q

How do you target only specific resources when you run a terraform plan?

A

-target=resource - A Resource Address to target. This flag can be used multiple times. See below for more information.

25
Q

How do you update the state prior to checking differences when you run a
terraform plan?

A

terraform plan -refresh=true

26
Q

The behavior of any terraform destroy command can be previewed at any time
with an equivalent terraform plan -destroy command. Is this true?

A

True

27
Q

You have the following file and created two resources docker_image and
docker_container with the command terraform apply and you go to the terminal and
delete the container with the command docker rm. You come back to your
configuration and run the command again. Does terraform recreates the resource?

A

Yes. Terraform creates the resource again since the execution plan says two resources and the terraform always maintains the desired state

28
Q

You created a VM instance on AWS cloud provider with the terraform
configuration and you log in AWS console and removed the instance. What does the
next apply do?

A

It creates the instance again

29
Q

You have the following file and created two resources docker_image and
docker_container with the command terraform plan and you go to the terminal and
delete the container with the command docker rm. You come back to your
configuration and run the command again. What is the output of the command plan?

A
Terraform will perform the following actions:
# docker_container.nginx will be created
Plan: 1 to add, 0 to change, 0 to destroy.