terraform-provider-alicloud icon indicating copy to clipboard operation
terraform-provider-alicloud copied to clipboard

alicloud_ecs_key_pair_attachment rewrites in for_each loop

Open igorfraa opened this issue 1 year ago • 5 comments

Terraform Version

Terraform v1.3.7
on darwin_arm64
+ provider registry.terraform.io/aliyun/alicloud v1.215.0

(also other versions)

Affected Resource(s)

  • alicloud_ecs_key_pair_attachment
  • alicloud_instance (as a target)

Expected Behavior

Multiple ssh keys attached (binded in Alicloud terms) to the ECS instance.

Actual Behavior

One or more keys got binded but never all at once. Note: It seems the longer the list, the more keys got attached (up to 3-5 keys from list of 20). Attachments are random.

With force=true only the last key attaches through all the cases. Though this can be explained throughout the docs.

Terraform Configuration Files

variable "ssh_keys" {
  type        = map(string)
  description = "Ssh keys"
  default = {
  user1 = "ssh-rsa AAAAB3 ..."
  user2 = "ssh-rsa AAAAB3 ..."
  user3 = "ssh-rsa AAAAB3 ..."
  user4 = "ssh-rsa AAAAB3 ..."
  }
}
resource "alicloud_ecs_key_pair" "ssh_keys" {
  for_each      = var.ssh_keys
  key_pair_name = each.key
  public_key    = each.value
}

resource "alicloud_ecs_key_pair_attachment" "ssh_keys" {
  for_each      = var.ssh_keys
  key_pair_name = alicloud_ecs_key_pair.ssh_keys[each.key].key_pair_name
  instance_ids  = var.instance_ids
  force         = false
}

Steps to Reproduce

  1. terraform apply

igorfraa avatar Feb 01 '24 14:02 igorfraa

please note if any more information needed

igorfraa avatar Feb 01 '24 14:02 igorfraa

carefully reading corresponding resource I didn't get how is this supposed to go.

Dear Developer He Guimin (@xiaozhu36), would you have a chance to look into?

source link

igorfraa avatar Feb 01 '24 15:02 igorfraa

Hi @igorfraa I am sorry for late reply, what do you mean "rewrites"? Do the multi key pairs can not be attached multi ecs instances accuratly?

xiaozhu36 avatar May 06 '24 09:05 xiaozhu36

Hi @xiaozhu36 yes - multiple key pairs are not attached correctly.

If I recall correctly, in some tries I've seen more then one key pair attached, up to 3 or 5 random keys out of the 20 in list.

igorfraa avatar May 07 '24 11:05 igorfraa

HI @igorfraa Based on your tf template, I made a test with 5 instance ids, and it can works well and did not happen your issue. Could please share me more details? like tf command outputs and diff logs after running it.

xiaozhu36 avatar May 09 '24 01:05 xiaozhu36