Write your first "Hello, World!" program in Terraform
HashiCorp Terraform is a powerful tool for managing infrastructure through code. It allows you to define various cloud and on-premises resources using easily understandable configuration files that can be versioned, reused, and shared. With Terraform, you can maintain a consistent workflow to provision, manage, and update our infrastructure at every stage. It offers the capability to handle a wide range of components, from fundamental elements like computing, storage, and networking resources to more advanced features such as DNS entries and SaaS functionalities.
This Answer will guide you in writing the “Hello, World!” program using Terraform. We will cover the necessary steps to write a basic configuration file and deploy a simple resource.
Terraform configuration file
Terraform interprets and carries out instructions based on the provided configuration file (.tf). This configuration file is written in HashiCorp Configuration Language (HCL), a user-friendly and machine-readable language designed specifically for Terraform. Its declarative nature allows both humans and machines to understand and work with it easily.
You will write terraform configuration in the following main.tf file:
resource "null_resource" "my_hello_world" {
provisioner "local-exec" {
command = "echo 'Hello, World! from Terraform'"
}
}Code explanation
Line 1: The
resourceblock defines a resource in Terraform. In this case, you are using thenull_resourceresource type, a generic resource with no direct counterpart in the cloud provider. Thenull_resourceresource allows you to execute local actions or commands during the Terraform lifecycle.Line 2: You define a
provisionerblock of typelocal-execinside thenull_resourceblock. This provisioner executes a local command on the machine running Terraform.Line 3: The
commandattribute specifies the command to be executed. In this case, we're using theechocommand to outputHello, World! from Terraformto the console.
Initializing and applying the configuration
You can use
terraform initcommand to initialize the Terraform project. This command downloads the necessary provider plugins and sets up the project.Then, you use the
terraform applycommand to apply the Terraform configuration and create the “Hello, World!” resource. You have to confirm the action by typingyeswhen prompted, or you can useterraform apply -auto-approveif you want to confirm the action automatically.
Output
You will see the following output in the terminal.
Congratulations! You have successfully created your first Terraform resource. The Hello, World! from Terraform message should be displayed in the terminal above as part of the Terraform apply process.
Conclusion
In this Answer, you learned how to write your first “Hello, World!” program using Terraform. You created a basic Terraform configuration file, deployed a resource that printed a message, and observed the output. This serves as a foundation for understanding Terraform’s infrastructure provisioning capabilities and how to define infrastructure resources using HCL.
Remember that Terraform is a powerful tool capable of managing complex infrastructure deployments. This Answer only scratched the surface, and you can explore more advanced features and provider-specific resources to build and manage infrastructure in a scalable and repeatable manner.
Free Resources