Search Tutorials


Top Terraform Interview Questions (2021) For Experienced Professionals | JavaInUse

Terraform Interview Questions (2021) For Experienced Professionals


In this post we will look at Terraform Interview questions. Examples are provided with explanation.


  1. What is Terraform?
  2. What are the main features of Terraform?
  3. What are the best practices when using Terraform?
  4. How to create a SSH in Terraform when using AWS?
  5. How to upgrade terraform to a specific version?
  6. How to set up a lambda function triggered by a scheduled event source when using Terraform?
  7. How to make use of AND/OR operators in Terraform?
  8. How to attach AWS managed policies to a role for Terraform?
  9. How to index all attributes when using DynamoDB with Terraform?

What is Terraform?

Terraform is a product developed by Hashi Corp. Hashi Corp is a company with a strong focus on DevOps tools, such as console, a highly available distributed key-value storage or vagrant. It is a tool to reproduce development environments using the same provisioners as production. Terraform, as the name hints, allows you to create infrastructure and cloud data centers in a declarative way, keeping track of what was created where. It allows you to apply changes to the infrastructure from the code perspective. Your infrastructure is described as the code and as such it can follow its life cycle. The first thing we need to do is download and install terraform.
Terraform is in fact a command line tool, it runs in Windows Powershell, Windows Command Line Processor, any Unix shell like Bash or C shell, or whatever your preference is.

What are the main features of Terraform?

  • Implement multi-cloud deployment environment with minimal effort
  • Using Terraform allows users to automate the entire infrastructure management
  • It is a declarative infrastructure as code tool
  • Development costs are comparitively low
  • Less provision time

What are the best practices when using Terraform?

  1. We highly recommend storing the Terraform code for each of your environments (e.g. stage, prod, qa) in separate sets of templates
  2. Create infrastructure in two modules - infrastructure-modules and infrastructure-live
  3. Create modules based on logical collections of various resources
   



How to create a SSH in Terraform when using AWS?

SSH private keys for Terraform can be created using tls_private_key
variable "terraform_key" {}

resource "tls_private_key" "example" {
  algorithm = "RSA"
  rsa_bits  = 4096
}

resource "aws_key_pair" "generated_key" {
  terraform_key   = var.terraform_key
  public_key = tls_private_key.example.public_key_openssh
}

data "aws_ami" "ubuntu" {
  most_recent = true

  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }

  owners = ["0977"] # Canonical
}

resource "aws_instance" "web" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t2.micro"
  terraform_key      = aws_key_pair.generated_key.terraform_key

  tags {
    Name = "Terraform Example"
  }
}

How to upgrade terraform to a specific version ?

After installation using brew install tfenv for installing terraform we can select the specific version -
$ tfenv list-remote
0.13.0
0.13.0-rc1
0.13.0-beta2
0.13.0-beta1
0.13.0
0.11.15
...

$ tfenv install 0.11.15
[INFO] Installing Terraform v0.11.15
[INFO] Downloading release tarball from https://releases.hashicorp.com/terraform/0.11.15/terraform_0.11.14_darwin_amd64.zip
...
[INFO] Installation of terraform v0.11.15 successful
[INFO] Switching to v0.11.14
[INFO] Switching completed

How to set up a lambda function triggered by a scheduled event source when using Terraform?

This can be achieved using aws_cloudwatch_event_target as follows -
resource "aws_lambda_function" "check_javainuse" {
    filename = "check_javainuse.zip"
    function_name = "checkFoo"
    role = "arn:aws:iam::424242:role/something"
    handler = "index.handler"
}

resource "aws_cloudwatch_event_rule" "check_every_ten_minutes" {
    name = "every-ten-minutes"
    description = "Fires every ten minutes"
    schedule_expression = "rate(10 minutes)"
}

resource "aws_cloudwatch_event_target" "check_javainuse_every_ten_minutes" {
    rule = ""
    target_id = "check_javainuse"
    arn = ""
}

resource "aws_lambda_permission" "allow_cloudwatch_to_call_check_foo" {
    statement_id = "AllowExecutionFromCloudWatch"
    action = "lambda:InvokeFunction"
    function_name = ""
    principal = "events.amazonaws.com"
    source_arn = ""
}

How to make use of AND/OR operators in Terraform?

Terraform has no defined binary types. How ever we can make use of Simple math for interpolations.
count = signum( + )

How to attach AWS managed policies to a role for Terraform??

This can be achieved using Data Source: aws_iam_policy
data "aws_iam_policy" "ReadWriteAccess" {
  arn = "arn:aws:iam::aws:policy/ReadWriteAccess"
}

How to index all attributes when using DynamoDB with Terraform ?

This can be achieved using the AttributeDefinition
resource "aws_dynamodb_table" "test-javainuse" {
  name           = "javainuse-table-name"
  read_capacity  = 5
  write_capacity = 5
  hash_key       = "javainuse-attribute"

  attribute {
    name = "javainuse-attribute"
    type = "S"
  }
}


See Also

Spring Boot Interview Questions Apache Camel Interview Questions Drools Interview Questions Java 8 Interview Questions Enterprise Service Bus- ESB Interview Questions. JBoss Fuse Interview Questions Top ElasticSearch frequently asked interview questions