{"action":"created","issue":{"url":"https://api.github.com/repos/hashicorp/terraform/issues/3975","labels_url":"https://api.github.com/repos/hashicorp/terraform/issues/3975/labels{/name}","comments_url":"https://api.github.com/repos/hashicorp/terraform/issues/3975/comments","events_url":"https://api.github.com/repos/hashicorp/terraform/issues/3975/events","html_url":"https://github.com/hashicorp/terraform/issues/3975","id":117715738,"number":3975,"title":"How to manage individual resources when using count","user":{"login":"mschurenko","id":4079843,"avatar_url":"https://avatars.githubusercontent.com/u/4079843?v=3","gravatar_id":"","url":"https://api.github.com/users/mschurenko","html_url":"https://github.com/mschurenko","followers_url":"https://api.github.com/users/mschurenko/followers","following_url":"https://api.github.com/users/mschurenko/following{/other_user}","gists_url":"https://api.github.com/users/mschurenko/gists{/gist_id}","starred_url":"https://api.github.com/users/mschurenko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mschurenko/subscriptions","organizations_url":"https://api.github.com/users/mschurenko/orgs","repos_url":"https://api.github.com/users/mschurenko/repos","events_url":"https://api.github.com/users/mschurenko/events{/privacy}","received_events_url":"https://api.github.com/users/mschurenko/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/hashicorp/terraform/labels/core","name":"core","color":"cccccc"},{"url":"https://api.github.com/repos/hashicorp/terraform/labels/question","name":"question","color":"cc317c"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-11-19T00:48:52Z","updated_at":"2015-11-22T20:36:05Z","closed_at":null,"body":"I'm not sure if this should be an issue or not so excuse me if it's in the wrong place.\r\n\r\nI am using the aws_iam_user resource to manage users like this:\r\n\r\n```\r\nresource \"aws_iam_user\" \"admin_users\" {\r\n  count = \"${length(split(\",\", var.admin_users))}\"\r\n  name = \"${element(split(\",\", var.admin_users), count.index)}\"\r\n}\r\n```\r\n\r\nThe users are defined as a comma delimited string like this:\r\n\r\n```\r\nvariable \"admin_users\" {\r\n  default = \"user1,user2\"\r\n}\r\n```\r\n\r\nAfter I've run \"terraform apply\" and created the users I now want to remove user1. So I remove user1 from the string so it's like this:\r\n\r\n```\r\nvariable \"admin_users\" {\r\n  default = \"user2\"\r\n}\r\n```\r\n\r\nWhen I run \"terraform plan\" I get this:\r\n\r\n```bash\r\nThe Terraform execution plan has been generated and is shown below.\r\nResources are shown in alphabetical order for quick scanning. Green resources\r\nwill be created (or destroyed and then created if an existing resource\r\nexists), yellow resources are being changed in-place, and red resources\r\nwill be destroyed.\r\n\r\nNote: You didn't specify an \"-out\" parameter to save this plan, so when\r\n\"apply\" is called, Terraform can't guarantee this is what will execute.\r\n\r\n~ module.users.aws_iam_group_membership.admin_members\r\n    users.#:          \"2\" => \"1\"\r\n    users.2354152789: \"user1\" => \"\"\r\n    users.358143215:  \"user2\" => \"user2\"\r\n\r\n-/+ module.users.aws_iam_user.admin_users\r\n    arn:       \"arn:aws:iam::094598394518:user/user1\" => \"<computed>\"\r\n    name:      \"user1\" => \"user2\" (forces new resource)\r\n    path:      \"/\" => \"/\"\r\n    unique_id: \"AIDAIRHPYR3R46Z37JT3A\" => \"<computed>\"\r\n\r\n- module.users.aws_iam_user.admin_users.1\r\n\r\n\r\nPlan: 1 to add, 1 to change, 2 to destroy.\r\n```\r\n\r\nSo not only is user1 being deleted but user2 is being destroyed and created again. How can we reliably manage the individual resources when using count? Is the solution just not to use count? If so then this makes templates less reusable. "},"comment":{"url":"https://api.github.com/repos/hashicorp/terraform/issues/comments/158795863","html_url":"https://github.com/hashicorp/terraform/issues/3975#issuecomment-158795863","issue_url":"https://api.github.com/repos/hashicorp/terraform/issues/3975","id":158795863,"user":{"login":"thegedge","id":833576,"avatar_url":"https://avatars.githubusercontent.com/u/833576?v=3","gravatar_id":"","url":"https://api.github.com/users/thegedge","html_url":"https://github.com/thegedge","followers_url":"https://api.github.com/users/thegedge/followers","following_url":"https://api.github.com/users/thegedge/following{/other_user}","gists_url":"https://api.github.com/users/thegedge/gists{/gist_id}","starred_url":"https://api.github.com/users/thegedge/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thegedge/subscriptions","organizations_url":"https://api.github.com/users/thegedge/orgs","repos_url":"https://api.github.com/users/thegedge/repos","events_url":"https://api.github.com/users/thegedge/events{/privacy}","received_events_url":"https://api.github.com/users/thegedge/received_events","type":"User","site_admin":false},"created_at":"2015-11-22T20:36:05Z","updated_at":"2015-11-22T20:36:05Z","body":"Was just putting together an example and about to come in and create this same issue when I found this one :smile: \r\n\r\nI'll provide the example I put together:\r\n```\r\nvariable \"count\" {}\r\n\r\nresource \"template_file\" \"foo\" {\r\n  count = \"${var.count}\"\r\n  template = \"foo.tpl\"\r\n  vars { index = \"${count.index}\" }\r\n}\r\n\r\nresource \"null_resource\" \"bar\" {\r\n  count = \"${var.count}\"\r\n  triggers {\r\n    foo = \"${element(template_file.foo.*.rendered, count.index)}\"\r\n  }\r\n}\r\n```\r\nAnd the following shell session:\r\n```\r\n$> terraform apply -var 'count=1'\r\n…\r\n$> terraform plan --var 'count=2'\r\nRefreshing Terraform state prior to plan...\r\n\r\ntemplate_file.foo.0: Refreshing state... (ID: 9a271f2a916b0b6ee6cecb2426f0b3206ef074578be55d9bc94f6f3fe3ab86aa)\r\nnull_resource.bar.0: Refreshing state... (ID: 7248997055001344561)\r\n…\r\n\r\n-/+ null_resource.bar.0\r\n    triggers.#:   \"1\" => \"<computed>\" (forces new resource)\r\n    triggers.foo: \"0\\n\" => \"\"\r\n\r\n+ null_resource.bar.1\r\n    triggers.#: \"\" => \"<computed>\"\r\n\r\n+ template_file.foo.1\r\n    rendered:   \"\" => \"<computed>\"\r\n    template:   \"\" => \"foo.tpl\"\r\n    vars.#:     \"\" => \"1\"\r\n    vars.index: \"\" => \"1\" \r\n```\r\nSo all I'm doing is increasing the count and it wants to recreate the initial resource. What's somewhat surprising is that decreasing the count from 2 to 1 doesn't want to do the same (i.e., it will only destroy the second resource, leaving the first untouched).\r\n\r\nI can see removing things from the middle of a `count`-ed resource being a little more complex, but I think the UX of terraform would greatly benefit from detecting these changes and giving a minimal plan."}}