0
0
Terraformcloud~30 mins

Connection blocks for SSH in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
Connection blocks for SSH
📖 Scenario: You are setting up a Terraform configuration to manage a virtual machine in the cloud. To apply changes remotely, you need to configure SSH connection details so Terraform can connect to the VM securely.
🎯 Goal: Build a Terraform configuration that defines an SSH connection block with the correct parameters to connect to a remote server.
📋 What You'll Learn
Create a resource block for a virtual machine named example_vm.
Add a connection block inside the resource with SSH details.
Use the exact variable names and values as specified.
Ensure the connection block includes type, host, user, and private_key.
💡 Why This Matters
🌍 Real World
Terraform uses connection blocks to securely connect to remote servers for provisioning and configuration management.
💼 Career
Understanding connection blocks is essential for infrastructure engineers and DevOps professionals managing cloud resources with Terraform.
Progress0 / 4 steps
1
Create a Terraform resource block for a virtual machine
Write a Terraform resource block named example_vm of type aws_instance with the ami set to "ami-12345678" and instance_type set to "t2.micro".
Terraform
Need a hint?

Use the resource keyword followed by the resource type and name. Set the ami and instance_type attributes exactly as shown.

2
Add a connection block with SSH type
Inside the example_vm resource block, add a connection block with type set to "ssh".
Terraform
Need a hint?

The connection block goes inside the resource block. Set type = "ssh" exactly.

3
Add host, user, and private_key to the connection block
Inside the connection block, add host set to "${self.public_ip}", user set to "ubuntu", and private_key set to file("~/.ssh/id_rsa").
Terraform
Need a hint?

Use host = "${self.public_ip}" to refer to the instance's public IP. Set user and private_key exactly as shown.

4
Add a provisioner block using the connection
Add a provisioner block of type remote-exec inside the example_vm resource. Use the existing connection block and add a inline command to run echo Hello, Terraform!.
Terraform
Need a hint?

The provisioner block runs commands on the remote machine. Use inline with the exact echo command.