nerdexam
HashiCorp

TA-002-P · Question #401

You're writing a Terraform configuration that needs to read input from a local file called id_rsa.pub. Which built-in Terraform function can you use to import the file's contents as a string?

The correct answer is D. file("id_rsa.pub"). The file() function reads a local file and returns its contents as a string, making it ideal for importing public keys or other text files into Terraform configurations.

Read, generate, and modify configuration

Question

You're writing a Terraform configuration that needs to read input from a local file called id_rsa.pub. Which built-in Terraform function can you use to import the file's contents as a string?

Options

  • Afileset("id_rsa.pub")
  • Bfilebase64("id_rsa.pub")
  • Ctemplatefile("id_rsa.pub")
  • Dfile("id_rsa.pub")

How the community answered

(54 responses)
  • A
    4% (2)
  • C
    2% (1)
  • D
    94% (51)

Why each option

The file() function reads a local file and returns its contents as a string, making it ideal for importing public keys or other text files into Terraform configurations.

Afileset("id_rsa.pub")

fileset() returns a set of file paths matching a glob pattern within a directory, not the contents of a single file.

Bfilebase64("id_rsa.pub")

filebase64() reads a file and returns its contents encoded as a Base64 string, not a plain string.

Ctemplatefile("id_rsa.pub")

templatefile() renders a template file by substituting variable expressions and requires a variables map as a second argument; it is not used for simple file import.

Dfile("id_rsa.pub")Correct

The file() function reads the contents of a file at the given path and returns it as a string. It is the standard Terraform built-in for importing raw file content - such as an SSH public key - directly into a resource argument like aws_key_pair.public_key.

Concept tested: Terraform file() built-in function usage

Source: https://developer.hashicorp.com/terraform/language/functions/file

Topics

#Terraform functions#Local file access#Configuration syntax

Community Discussion

No community discussion yet for this question.

Full TA-002-P Practice