0
0
Terraformcloud~5 mins

File provisioner in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to copy files from your computer to a server when creating infrastructure. The file provisioner in Terraform helps you do this automatically during resource creation.
When you want to copy a configuration file to a new virtual machine during setup.
When you need to upload a script to a server to run later.
When you want to place a certificate or key file on a remote machine securely.
When you want to automate copying files instead of doing it manually after deployment.
When you want to ensure files are present on a server as part of your infrastructure code.
Config File - main.tf
main.tf
provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  connection {
    type        = "ssh"
    user        = "ec2-user"
    private_key = file("~/.ssh/id_rsa")
    host        = self.public_ip
  }

  provisioner "file" {
    source      = "app.conf"
    destination = "/tmp/app.conf"
  }

  provisioner "remote-exec" {
    inline = ["cat /tmp/app.conf"]
  }
}

This Terraform file creates an AWS EC2 instance.

The file provisioner copies a local file named app.conf to the remote instance at /tmp/app.conf.

The remote-exec provisioner runs a command on the instance to show the copied file content.

The connection block defines how Terraform connects to the instance using SSH.

Commands
This command initializes Terraform in the current directory. It downloads the AWS provider plugin needed to create resources.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes required for your infrastructure.
This command creates the AWS EC2 instance and copies the file using the file provisioner automatically without asking for confirmation.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_instance.example: Creating... aws_instance.example: Still creating... [10s elapsed] aws_instance.example: Creation complete after 20s [id=i-0abcd1234efgh5678] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skips the interactive approval prompt to apply changes immediately.
This command removes the created EC2 instance and cleans up all resources automatically.
Terminal
terraform destroy -auto-approve
Expected OutputExpected
aws_instance.example: Refreshing state... [id=i-0abcd1234efgh5678] aws_instance.example: Destroying... [id=i-0abcd1234efgh5678] aws_instance.example: Destruction complete after 10s Destroy complete! Resources: 1 destroyed.
-auto-approve - Skips the confirmation prompt to destroy resources immediately.
Key Concept

If you remember nothing else from this pattern, remember: the file provisioner copies files from your computer to a remote machine during resource creation to automate setup.

Common Mistakes
Not defining the connection block when using the file provisioner.
Terraform needs connection details like SSH user and key to copy files; without it, the provisioner fails.
Always include a connection block with correct SSH details when using file or remote-exec provisioners.
Using a local file path that does not exist or is incorrect in the source field.
Terraform cannot find the file to copy, causing the apply to fail.
Ensure the source file path is correct and the file exists before running terraform apply.
Expecting the file provisioner to update files on existing resources without recreating them.
Provisioners run only during resource creation or replacement, so changes to files won't apply on existing resources.
To update files, recreate the resource or use configuration management tools after deployment.
Summary
Use terraform init to prepare Terraform and download providers.
Use the file provisioner inside a resource to copy files from local to remote during creation.
Use terraform apply to create resources and copy files automatically.
Always define connection details for SSH access when using file provisioner.
Use terraform destroy to clean up resources when done.