0
0
Terraformcloud~10 mins

Why provisioners run scripts on resources in Terraform - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to run a script on a resource using a provisioner.

Terraform
resource "aws_instance" "example" {
  ami           = "ami-123456"
  instance_type = "t2.micro"

  provisioner "remote-exec" {
    inline = ["[1]"]
  }
}
Drag options to blanks, or click blank then click option'
Aecho Hello World
Bstart service
Cinstall package
Dcreate file
Attempts:
3 left
💡 Hint
Common Mistakes
Using commands that are not valid shell commands.
Trying to run multiple commands without proper syntax.
2fill in blank
medium

Complete the code to specify the connection details for the provisioner.

Terraform
resource "aws_instance" "example" {
  ami           = "ami-123456"
  instance_type = "t2.micro"

  connection {
    type     = "ssh"
    user     = "[1]"
    private_key = file("~/.ssh/id_rsa")
    host     = self.public_ip
  }
}
Drag options to blanks, or click blank then click option'
Aadmin
Bec2-user
Croot
Dubuntu
Attempts:
3 left
💡 Hint
Common Mistakes
Using root which is often disabled for SSH.
Using a user that does not exist on the instance.
3fill in blank
hard

Fix the error in the provisioner block to run a script file on the resource.

Terraform
resource "aws_instance" "example" {
  ami           = "ami-123456"
  instance_type = "t2.micro"

  provisioner "file" {
    source      = "./setup.sh"
    destination = "/tmp/[1]"
  }
}
Drag options to blanks, or click blank then click option'
Asetup.sh
Bsetup
Cscript.sh
Dinstall.sh
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the file extension in the destination path.
Using a different file name that does not match the source.
4fill in blank
hard

Fill both blanks to run a script after copying it to the resource.

Terraform
resource "aws_instance" "example" {
  ami           = "ami-123456"
  instance_type = "t2.micro"

  provisioner "file" {
    source      = "./install.sh"
    destination = "/tmp/[1]"
  }

  provisioner "remote-exec" {
    inline = ["chmod +x /tmp/[2]", "/tmp/install.sh"]
  }
}
Drag options to blanks, or click blank then click option'
Ainstall.sh
Bsetup.sh
Cdeploy.sh
Drun.sh
Attempts:
3 left
💡 Hint
Common Mistakes
Using different file names in the copy and execute steps.
Forgetting to make the script executable before running.
5fill in blank
hard

Fill all three blanks to provision a resource, copy a script, and run it with correct connection details.

Terraform
resource "aws_instance" "example" {
  ami           = "ami-123456"
  instance_type = "t2.micro"

  connection {
    type        = "ssh"
    user        = "[1]"
    private_key = file("~/.ssh/id_rsa")
    host        = self.public_ip
  }

  provisioner "file" {
    source      = "./setup.sh"
    destination = "/tmp/[2]"
  }

  provisioner "remote-exec" {
    inline = ["chmod +x /tmp/[3]", "/tmp/setup.sh"]
  }
}
Drag options to blanks, or click blank then click option'
Aec2-user
Bsetup.sh
Dubuntu
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong SSH user names.
Mismatching script file names between provisioners.