Your First Terraform Project: Hello World

Search for a command to run...

No comments yet. Be the first to comment.
why we need tools like K8s?

secure web server with nginx..

virtual host concept...

In the realm of Terraform, providers play a pivotal role in connecting your infrastructure definitions with the underlying resources in various cloud platforms or other infrastructure systems. To wield the full power of Terraform, it's essential to g...

As you delve deeper into Terraform, understanding the HashiCorp Configuration Language (HCL) becomes crucial. HCL serves as the foundation for expressing infrastructure as code in a clear, concise, and human-readable manner. In this guide, we'll expl...

Wiz4host Blogs - DevOps
18 posts
Now that you've installed and configured Terraform for your environment, it's time to dive into the exciting world of Infrastructure as Code (IaC). In this guide, we'll create a simple yet illustrative Terraform project, echoing the classic programming tradition with a "Hello World" example.
Create a Directory:
Begin by creating a new directory for your Terraform project. Open your terminal and run:
mkdir terraform_hello_world
cd terraform_hello_world

Initialize Terraform:
In your project directory, run the following command to initialize Terraform:
terraform init
This command sets up the necessary files and downloads any required provider plugins.

Create a file named main. tf in your project directory. This file will contain the Terraform configuration code.
main.tf
provider "null" {
version = "3.1.0"
}
resource "null_resource" "hello_world" {
provisioner "local-exec" {
command = "echo 'Hello, Terraform!'"
}
}

Let's break down what's happening in this simple configuration:
We're using the null provider, a built-in provider that represents no real infrastructure. It's perfect for our "Hello World" example.
The null_resource block creates a null resource, again, for demonstration purposes.
Within the provisioner block, we use the local-exec provisioner to execute a local command. In this case, we're echoing "Hello, Terraform!" to the terminal.
Now that your Terraform configuration is ready, let's apply it:
terraform apply
Terraform will provide an execution plan, outlining the actions it will take. Confirm the plan by typing yes. Terraform will then execute the plan, and you should see the "Hello, Terraform!" message in your terminal.

Once you've had your "Hello, Terraform!" moment, it's essential to clean up resources to avoid unnecessary costs. Run the following command to destroy the resources:
terraform destroy
Confirm the destruction by typing yes when prompted.

Congratulations! You've just created and applied your first Terraform configuration. While the "Hello World" example may seem trivial, it lays the foundation for more complex infrastructure definitions.
In future blog posts, we'll explore advanced Terraform features, work with real cloud providers, and tackle real-world use cases. Stay tuned for an exciting journey into the depths of Infrastructure as Code with Terraform!