Get started with Terraform in minutes by creating and managing your first infrastructure.
This quickstart guide will walk you through installing Terraform and creating your first infrastructure. You’ll learn the basic Terraform workflow: write, initialize, plan, and apply.
This guide uses the local provider to create a file on your local system. This is a simple example to demonstrate Terraform’s workflow without requiring cloud credentials.
Create a new directory for your Terraform project and navigate to it:
mkdir terraform-democd terraform-demo
Create a file named main.tf with the following content:
main.tf
terraform { required_providers { local = { source = "hashicorp/local" version = "~> 2.4" } }}provider "local" {}resource "local_file" "hello" { filename = "${path.module}/hello.txt" content = "Hello, Terraform!\n"}resource "local_file" "config" { filename = "${path.module}/config.json" content = jsonencode({ message = "Welcome to Terraform" environment = "development" timestamp = "2024-03-15" })}output "hello_file_path" { description = "Path to the hello.txt file" value = local_file.hello.filename}output "config_content" { description = "Content of the config file" value = jsondecode(local_file.config.content)}
This configuration uses the local provider to create two files on your local filesystem. The terraform block specifies the required provider, and the resource blocks define the infrastructure.
Before creating any infrastructure, it’s a good practice to preview what Terraform will do:
terraform plan
Terraform will show you the execution plan:
Terraform will perform the following actions: # local_file.config will be created + resource "local_file" "config" { + content = jsonencode( { + environment = "development" + message = "Welcome to Terraform" + timestamp = "2024-03-15" } ) + filename = "./config.json" + id = (known after apply) } # local_file.hello will be created + resource "local_file" "hello" { + content = "Hello, Terraform!\n" + filename = "./hello.txt" + id = (known after apply) }Plan: 2 to add, 0 to change, 0 to destroy.
The + symbol indicates that Terraform will create these resources.
When you’re done experimenting, you can destroy the infrastructure:
terraform destroy
Terraform will show you what it plans to destroy and ask for confirmation. Type yes to proceed:
Plan: 0 to add, 0 to change, 2 to destroy.Do you really want to destroy all resources? Terraform will destroy all your managed infrastructure, as shown above. There is no undo. Only 'yes' will be accepted to confirm. Enter a value: yes
The terraform destroy command permanently deletes all resources managed by your Terraform configuration. Always double-check before confirming.