0
0
Terraformcloud~30 mins

Remote-exec provisioner in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Terraform Remote-exec Provisioner
📖 Scenario: You are setting up a cloud server and want to run a simple command on it right after it is created. This helps you install software or configure the server automatically.
🎯 Goal: Build a Terraform configuration that creates a virtual machine and uses the remote-exec provisioner to run a command on the server after creation.
📋 What You'll Learn
Create an AWS EC2 instance resource named aws_instance.example
Add a remote-exec provisioner inside the instance resource
Use the provisioner to run the command echo Hello, Terraform! on the instance
Configure connection details for the provisioner using SSH
💡 Why This Matters
🌍 Real World
Automating server setup by running commands immediately after the server is created saves time and reduces manual errors.
💼 Career
Cloud engineers and DevOps professionals use remote-exec provisioners to automate configuration and deployment tasks on cloud instances.
Progress0 / 4 steps
1
Create AWS EC2 instance resource
Create a resource block named aws_instance.example with ami set to "ami-0c55b159cbfafe1f0" and instance_type set to "t2.micro".
Terraform
Need a hint?

The resource block defines the EC2 instance with the given AMI and instance type.

2
Add SSH connection configuration
Add a connection block inside aws_instance.example with type set to "ssh", user set to "ec2-user", and private_key set to file("~/.ssh/id_rsa").
Terraform
Need a hint?

The connection block tells Terraform how to connect to the instance via SSH.

3
Add remote-exec provisioner to run a command
Inside aws_instance.example, add a provisioner "remote-exec" block that runs the command echo Hello, Terraform! using the inline argument.
Terraform
Need a hint?

The remote-exec provisioner runs commands on the remote instance after creation.

4
Add public IP to connect to the instance
Add associate_public_ip_address = true inside the aws_instance.example resource to ensure the instance gets a public IP for SSH connection.
Terraform
Need a hint?

Setting associate_public_ip_address to true allows SSH access to the instance.