Complete the code to run a script on a resource using a provisioner.
resource "aws_instance" "example" { ami = "ami-123456" instance_type = "t2.micro" provisioner "remote-exec" { inline = ["[1]"] } }
The inline block in the remote-exec provisioner runs shell commands on the resource. "echo Hello World" is a valid simple command.
Complete the code to specify the connection details for the provisioner.
resource "aws_instance" "example" { ami = "ami-123456" instance_type = "t2.micro" connection { type = "ssh" user = "[1]" private_key = file("~/.ssh/id_rsa") host = self.public_ip } }
root which is often disabled for SSH.For Amazon Linux instances, the default SSH user is ec2-user. This user is needed for the provisioner to connect.
Fix the error in the provisioner block to run a script file on the resource.
resource "aws_instance" "example" { ami = "ami-123456" instance_type = "t2.micro" provisioner "file" { source = "./setup.sh" destination = "/tmp/[1]" } }
The destination path should match the file name being copied. Since the source is setup.sh, the destination should be /tmp/setup.sh.
Fill both blanks to run a script after copying it to the resource.
resource "aws_instance" "example" { ami = "ami-123456" instance_type = "t2.micro" provisioner "file" { source = "./install.sh" destination = "/tmp/[1]" } provisioner "remote-exec" { inline = ["chmod +x /tmp/[2]", "/tmp/install.sh"] } }
Both the file and remote-exec provisioners must refer to the same script file name install.sh to copy and then execute it.
Fill all three blanks to provision a resource, copy a script, and run it with correct connection details.
resource "aws_instance" "example" { ami = "ami-123456" instance_type = "t2.micro" connection { type = "ssh" user = "[1]" private_key = file("~/.ssh/id_rsa") host = self.public_ip } provisioner "file" { source = "./setup.sh" destination = "/tmp/[2]" } provisioner "remote-exec" { inline = ["chmod +x /tmp/[3]", "/tmp/setup.sh"] } }
The connection user for AWS Linux is ec2-user. The script file name setup.sh must be consistent in both the file and remote-exec provisioners.