0
votes

When running the below file with Terraform I get the following error:

Resource 'aws_instance.nodes-opt-us-k8s' not found for variable 'aws_instance.nodes-opt.us1-k8s.id'.

Do I need to include the provisioner twice because my 'count' variable is creating two? When I just include one for 'count' variable I get the error my Ansible playbook needs to run playbook files, which makes since because it is empty until I figure this error out.

I am in the early stages with Terraform and Linux so pardon my ignorance

#-----------------------------Kubernetes Master & Worker Node Server Creations----------------------------

#-----key pair for Workernodes-----

resource "aws_key_pair" "k8s-node_auth" {
  key_name   = "${var.key_name2}"
  public_key = "${file(var.public_key_path2)}"
}

#-----Workernodes-----

resource "aws_instance" "nodes-opt-us1-k8s" {
  instance_type = "${var.k8s-node_instance_type}"
  ami           = "${var.k8s-node_ami}"
  count         = "${var.NodeCount}"

  tags {
    Name = "nodes-opt-us1-k8s"
  }

  key_name               = "${aws_key_pair.k8s-node_auth.id}"
  vpc_security_group_ids = ["${aws_security_group.opt-us1-k8s_sg.id}"]
  subnet_id              = "${aws_subnet.opt-us1-k8s.id}"

  #-----Link Terraform worker nodes to Ansible playbooks-----

  provisioner "local-exec" {
    command = <<EOD
cat <<EOF >> workers
[workers]
${self.public_ip}
EOF
EOD
  }
  provisioner "local-exec" {
    command = "aws ec2 wait instance-status-ok --instance-ids ${aws_instance.nodes-opt-us1-k8s.id} --profile Terraform && ansible-playbook -i workers Kubernetes-Nodes.yml"
  }
}
2
You just want self.id hereydaetskcoR
Thanks...that did the trick.Chase Mitchell

2 Answers

0
votes

Terraform 0.12.26 resolved similar issue for me (when using multiple file provisioners when deploying multiple VMs to Azure)

Hope this helps you: https://github.com/hashicorp/terraform/issues/22006

0
votes

When using a provisioner and referring to the resource the provisioner is attached to you need to use the self keyword as you've already spotted with what you are writing to the file.

So in your case you want to use the following provisioner block:

...

  provisioner "local-exec" {
    command = <<EOD
cat <<EOF >> workers
[workers]
${self.public_ip}
EOF
EOD
  }
  provisioner "local-exec" {
    command = "aws ec2 wait instance-status-ok --instance-ids ${self.id} --profile Terraform && ansible-playbook -i workers Kubernetes-Nodes.yml"
  }