1

ERROR: ╷ │ Error: Unsupported attribute │ │ on spot_inst.tf line 82, in resource "null_resource" "spot_inst_ssh": │ 82: host = element(aws_spot_fleet_request.spot_inst..public_ip,"${each.value.IMO}") │ │ This object does not have an attribute named "public_ip". ╵ ╷ │ Error: Unsupported attribute │ │ on spot_inst.tf line 82, in resource "null_resource" "spot_inst_ssh": │ 82: host = element(aws_spot_fleet_request.spot_inst..public_ip,"${each.value.IMO}") │ │ This object does not have an attribute named "public_ip". ╵


Below is the main.tf file


locals {
      csvdata = csvdecode(file("C:\\Users\\myhome\\Desktop\\test_terraform\\IMO_TOKEN.csv"))
}
resource "aws_spot_fleet_request" "spot_inst" {
      iam_fleet_role  = "arn:aws:iam::123456789:role/aws-ec2-spot-fleet-tagging-role"
      for_each = { for inst in local.csvdata : inst.IMO => inst }
      spot_price      = "0.5"
      target_capacity = 1
      wait_for_fulfillment = true        
      launch_specification {
        instance_type     = "m5a.xlarge"
        ami               = "ami-07dbhghghhfgfg"
        associate_public_ip_address = true
        key_name          = "dryrun"
        subnet_id = "subnet-059107999db020b76"
        vpc_security_group_ids = ["sg-0511bb4914501d1ce"]
    
        root_block_device{
          volume_size = 20
          volume_type = "gp3"
          iops = "3000"
        }
    
        tags = {
          Name = "CloudInst${each.value.TOKEN}"
          Environment = "dev"
          Group = "OPS"
        }
      }
    
      launch_specification {
        instance_type     = "m5d.xlarge"
        ami               = "ami-07dbhghghhfgfg"
        associate_public_ip_address = true
        key_name          = "dryrun"
        subnet_id = "subnet-059107999db020b76"
        vpc_security_group_ids = ["sg-0511bb4914501d1ce"]
        root_block_device{
          volume_size = 20
          volume_type = "gp3"
          iops = "3000"
        }
        tags = {
          Name = "CloudInst${each.value.TOKEN}"
          Environment = "dev"
          Group = "OPS"
        }
      }
    
    /*
      connection {
          type        = "ssh"
          host        = "${self.public_ip}"
          user        = "centos"
          private_key = "${file("C:\\Users\\myhome\\Desktop\\test_terraform\\dryrun.pem")}"
          timeout     = "5m"
          agent       = true
        }
    
    
      provisioner "remote-exec" {
          inline = ["/usr/bin/nohup /usr/bin/sudo /usr/bin/sh /home/centos/new_cloudinst.sh ${each.value.IMO} ${each.value.TOKEN}"]
      }
    */
}
    

resource "null_resource" "spot_inst_ssh" {
      depends_on = [aws_spot_fleet_request.spot_inst]
      for_each = { for inst in local.csvdata : inst.IMO => inst }
      connection {
        type        = "ssh"
        host = element(aws_spot_fleet_request.spot_inst.*.public_ip,"${each.value.IMO}")
        user        = "centos"
        private_key = "${file("C:\\Users\\myhome\\Desktop\\test_terraform\\dryrun.pem")}"
        timeout     = "5m"
        agent       = true
      }
      provisioner "remote-exec" {
        inline = ["/usr/bin/nohup /usr/bin/sudo /usr/bin/sh /home/centos/new_cloudinst.sh ${each.value.IMO} ${each.value.TOKEN}"]
      }
}
   
Sumanth M
  • 11
  • 2
  • CSV file contents are below IMO_TOKEN.csv ____________________ IMO,TOKEN 5004005,10494DA3 5123476,7B358279 Error I get : __________________________________________ ╷ │ Error: Unsupported attribute │ │ on spot_inst.tf line 85, in resource "null_resource" "spot_inst_ssh": │ 85: host = element(aws_spot_fleet_request.spot_inst.*.public_ip,0) │ │ This object does not have an attribute named "public_ip". – Sumanth M Jul 25 '22 at 07:34
  • Please correctly format the code and provide **full** error message. – Marcin Jul 25 '22 at 07:34
  • Edited the question with error and proper code – Sumanth M Jul 25 '22 at 07:44

1 Answers1

0

It should be:

host = aws_spot_fleet_request.spot_inst[each.value.IMO].public_ip
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • I am getting below error after changing "host = aws_spot_fleet_request.spot_inst[each.value.IMO].public_ip"Error: Unsupported attribute │ │ on spot_inst.tf line 85, in resource "null_resource" "spot_inst_ssh": │ 85: host = aws_spot_fleet_request.spot_inst[each.value.IMO].public_ip │ ├──────────────── │ │ aws_spot_fleet_request.spot_inst is object with 2 attributes │ │ each.value.IMO is "5004375" │ │ This object does not have an attribute named "public_ip". – Sumanth M Jul 26 '22 at 06:34