Terraform Basics : Creating a simple file with contents using terraform

Share At:

Creating Module Dependencies in Terraform 0.13 | by Roger Berlind |  HashiCorp Solutions Engineering Blog | Medium

What is Terraform?

Terraform is an infrastructure as code (IaC) tool that allows you to build, change, and version infrastructure safely and efficiently. This includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc. Terraform can manage both existing service providers and custom in-house solutions.

Terraform Modules

A module is a collection of .tf and/or .tf.json files kept together in a directory.

A Terraform module only consists of the top-level configuration files in a directory; nested directories are treated as completely separate modules, and are not automatically included in the configuration.

Terraform evaluates all of the configuration files in a module, effectively treating the entire module as a single document. Separating various blocks into different files is purely for the convenience of readers and maintainers, and has no effect on the module’s behavior.

A Terraform module can use module calls to explicitly include other modules into the configuration. These child modules can come from local directories (nested in the parent module’s directory, or anywhere else on disk), or from external sources like the Terraform Registry.

Terraform Providers

Terraform relies on plugins called “providers” to interact with cloud providers, SaaS providers, and other APIs.

Terraform configurations must declare which providers they require so that Terraform can install and use them.

Examples of providers are:

  • AWS.
  • Azure.
  • Google Cloud Platform.
  • Kubernetes.
  • Oracle Cloud Infrastructure.
  • Alibaba Cloud
  • local

What is Local Provider ?

The Local provider is used to manage local resources, such as files. There is only one resource available for Local provider : local_file

Below is the sample tarraform module to create a file with some content.


local_file : Resource type

foo : Resource name

content : (Argument) The content of file to create. ( Conflicts with sensitive_content and content_base64)

Lab: Below example is a demostration on how to create a file using local_file resourse in Terraform

  • Let’s change to the directory where our main.tf file exists
  • let’s see what the module consists of
  • Let’s initialize the working directory containing Terraform configuration files
  • let’s create the execution plan using “terraform plan” command.
  • Let’s execute the actions proposed in a Terraform plan using “terraform apply” command:
  • Upon successful completion, you will see something like below:
  • Let’s verify if the file has been created. also check its content:
  • If you want to mask the content of a sensitive file which should not be displayed while running “terraform plan” command, you need to write the main.tf configuration file like below:
  • Now, when you run “terraform plan” command, the content of the file will not be displayed on screen:

That’s it. Hope you have enjoyed the tutorial.

Happy Learning !!!

Share At:
0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Back To Top

Contact Us