File provisioner error

vars

variable “REGION” {

default = “us-east-1”

}

variable “ZONE2” {

default = “us-east-1a”

}

variable “AMIS” {

type = map(any)

default = {

us-east-1 = "ami-053b0d53c279acc90"

us-east-2 = "ami-024e6efaf93d85776"

}

}

variable “USERS” {

default = “ec2-user”

}

Providers

provider “aws” {

region = var.REGION

}

Instance

resource “aws_key_pair” “terrakey” {

key_name = “tryingTerra”

public_key = file(“newinstanceforterraform.pub”)

}

resource “aws_instance” “forTerrIntro” {

ami = var.AMIS[var.REGION]

availability_zone = var.ZONE2

instance_type = “t2.micro”

key_name = aws_key_pair.terrakey.key_name

vpc_security_group_ids = [“sg-0f3d7e7163ace6147”]

tags = {

Name = "AmritTryingForTerra"

}

provisioner “file” {

source      = "local_bash.sh"

destination = "/tmp/local_bash.sh"

}

provisioner “remote-exec” {

inline = [

  "chmod +x /tmp/local_bash.sh",

  "sudo /tmp/local_bash.sh"

]

}

connection {

user        = var.USERS

private_key = file("newinstanceforterraform")

host        = self.public_ip

}

}

local_bash

#!/bin/bash

yum install wget unzip httpd -y

systemctl start httpd

systemctl enable httpd

wget https://www.tooplate.com/zip-templates/2132_clean_work.zip

unzip -o 2132_clean_work.zip

cp -r 2132_clean_work.zip/* /var/www/html/

systemctl restart httpd

HERE’S THE ERROR

Do you want to perform these actions?

Terraform will perform the actions described above.

Only ‘yes’ will be accepted to approve.

Enter a value: yes

aws_key_pair.terrakey: Creating…

aws_key_pair.terrakey: Creation complete after 2s [id=tryingTerra]

aws_instance.forTerrIntro: Creating…

aws_instance.forTerrIntro: Still creating… [10s elapsed]

aws_instance.forTerrIntro: Still creating… [21s elapsed]

aws_instance.forTerrIntro: Still creating… [31s elapsed]

aws_instance.forTerrIntro: Provisioning with ‘file’…

aws_instance.forTerrIntro: Still creating… [41s elapsed]

aws_instance.forTerrIntro: Still creating… [51s elapsed]

aws_instance.forTerrIntro: Still creating… [1m1s elapsed]

aws_instance.forTerrIntro: Still creating… [1m11s elapsed]

aws_instance.forTerrIntro: Still creating… [1m21s elapsed]

aws_instance.forTerrIntro: Still creating… [1m31s elapsed]

aws_instance.forTerrIntro: Still creating… [1m41s elapsed]

aws_instance.forTerrIntro: Still creating… [1m51s elapsed]

aws_instance.forTerrIntro: Still creating… [2m1s elapsed]

aws_instance.forTerrIntro: Still creating… [2m11s elapsed]

aws_instance.forTerrIntro: Still creating… [2m21s elapsed]

aws_instance.forTerrIntro: Still creating… [2m31s elapsed]

aws_instance.forTerrIntro: Still creating… [2m41s elapsed]

aws_instance.forTerrIntro: Still creating… [2m51s elapsed]

aws_instance.forTerrIntro: Still creating… [3m1s elapsed]

aws_instance.forTerrIntro: Still creating… [3m11s elapsed]

aws_instance.forTerrIntro: Still creating… [3m21s elapsed]

aws_instance.forTerrIntro: Still creating… [3m31s elapsed]

aws_instance.forTerrIntro: Still creating… [3m41s elapsed]

aws_instance.forTerrIntro: Still creating… [3m51s elapsed]

aws_instance.forTerrIntro: Still creating… [4m1s elapsed]

aws_instance.forTerrIntro: Still creating… [4m11s elapsed]

aws_instance.forTerrIntro: Still creating… [4m21s elapsed]

aws_instance.forTerrIntro: Still creating… [4m31s elapsed]

aws_instance.forTerrIntro: Still creating… [4m41s elapsed]

aws_instance.forTerrIntro: Still creating… [4m51s elapsed]

aws_instance.forTerrIntro: Still creating… [5m1s elapsed]

aws_instance.forTerrIntro: Still creating… [5m11s elapsed]

aws_instance.forTerrIntro: Still creating… [5m21s elapsed]

aws_instance.forTerrIntro: Still creating… [5m31s elapsed]

│ Error: file provisioner error

│ with aws_instance.forTerrIntro,

│ on Instance.tf line 15, in resource “aws_instance” “forTerrIntro”:

│ 15: provisioner “file” {

│ timeout - last error: SSH authentication failed

│ (ec2-user@54.91.212.209:22): ssh: handshake failed: ssh: unable to

│ authenticate, attempted methods [none publickey], no supported methods

│ remain

Hi @Amrit-boss,

Provisioners that run commands remotely are difficult to configure correctly because you must ensure both that the computer where you are running Terraform can directly connect to the SSH server of each individual server and that you have correctly configured some authentication credentials to use to connect. This complexity is one of the reasons why provisioners are a last resort.

You are using provisioners here only to upload a script to the remote system and then execute it, which is an unnecessary use of provisioners.

Instead, use cloud-init for your first-boot system configuration, as described in this tutorial:

Cloud-init uses configuration sent indirectly via the EC2 API, so your computer running Terraform will not need to connect to the target server over SSH. Therefore you will not have authentication-related problems as you saw here.