How to set a default value for a variable in a Terraform template?

Modules can also have variables with default values.

variable "first_name" {
  type = string
  default = "there"
}

resource "local_file" "foo" {
    content     = templatefile(format("%s/hello.tpl", path.module), { first_name = var.first_name })
    filename = format("%s/hello.txt", path.module)
}

content of hello.tpl

Hello ${first_name}

init

$ terraform init

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/local...
- Installing hashicorp/local v2.1.0...
- Installed hashicorp/local v2.1.0 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

apply with default value

$ terraform apply -auto-approve
local_file.foo: Refreshing state... [id=42329929896ff1f958cb9e4013d610eadf8b6fde]
local_file.foo: Destroying... [id=42329929896ff1f958cb9e4013d610eadf8b6fde]
local_file.foo: Destruction complete after 0s
local_file.foo: Creating...
local_file.foo: Creation complete after 0s [id=cf77cea2580f552986703576b260376c818e37f0]

Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
$ cat .\hello.txt
Hello there

apply with a value set (via CLI here, but could be a .tfvars file or a variable set in the workspace when using TF cloud

$ terraform apply -auto-approve -var 'first_name=Kitty'
local_file.foo: Refreshing state... [id=cf77cea2580f552986703576b260376c818e37f0]
local_file.foo: Destroying... [id=cf77cea2580f552986703576b260376c818e37f0]
local_file.foo: Destruction complete after 0s
local_file.foo: Creating...
local_file.foo: Creation complete after 0s [id=fd9bc4b115e5c567fe0f5b18dd7fff847a10bc36]

Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
$ cat .\hello.txt
Hello Kitty