Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Change count for for_each on ram_principal_association due resource recreation #95

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ebarros29
Copy link

@ebarros29 ebarros29 commented Dec 23, 2022

Description

Change count for for_each on aws_ram_principal_association due resource recreation.

Motivation and Context

In order to avoid recreation of the resource aws_ram_principal_association everytime that a principal is removed or inserted in the middle of the list, it's necessary to change count for for_each loop.

Breaking Changes

The meta-argument count use indices to organize the list, for instance aws_ram_principal_association.this["0"].
The meta-argument for_eachuses "key, value" to organize it, for instance aws_ram_principal_association.this["01234567890123"] where 01234567890123 will be the principal account ID for this case.
So due these differences it will cause a recreation of all existing aws_ram_principal_association resources and the people who use RAM for share the TGW will need to run terraform state mv in order to avoid this recreation.

How Has This Been Tested?

It has been tested using the module with input share_tgw = true by analysing the plan and outputs after apply.

  • I have updated at least one of the examples/* to demonstrate and validate my change(s)
  • I have tested and validated these changes using one or more of the provided examples/* projects
  • I have executed pre-commit run -a on my pull request

@ebarros29 ebarros29 changed the title fix: change count for for_each on ram_principal_association due resource recreation fix: Change count for for_each on ram_principal_association due resource recreation Dec 23, 2022
@github-actions
Copy link

This PR has been automatically marked as stale because it has been open 30 days
with no activity. Remove stale label or comment or this PR will be closed in 10 days

@github-actions github-actions bot added the stale label Jan 27, 2023
…e and adjust the output related to this resource
@ebarros29 ebarros29 force-pushed the fix/avoid-recreate-ram-principal-association branch from e56038e to f4660da Compare January 28, 2023 17:18
@ebarros29
Copy link
Author

Folks!
could you review this PR? I think it's a good improvement...

@ebarros29
Copy link
Author

can we remove the stale label?

@ebarros29
Copy link
Author

@antonbabenko @bryantbiggs
Please, could you review this PR?

Copy link
Member

@bryantbiggs bryantbiggs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change and it has some fundamental issues with regards to the use of for_each

@github-actions github-actions bot removed the stale label Jan 29, 2023
@ebarros29
Copy link
Author

ebarros29 commented Feb 5, 2023

This is a breaking change and it has some fundamental issues with regards to the use of for_each

Yes, it is!
I described this breaking change into the PR description and I've tested this change...

What kind of issues we can expect different from what has been described into PR?

main.tf Outdated
@@ -161,9 +161,9 @@ resource "aws_ram_resource_association" "this" {
}

resource "aws_ram_principal_association" "this" {
count = var.create_tgw && var.share_tgw ? length(var.ram_principals) : 0
for_each = var.create_tgw && var.share_tgw ? toset(var.ram_principals) : []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if any values provided in var.ram_principals are computed, this will fail. in addition, the left side of the conditional is of type toset() and the right is tolist()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if any values provided in var.ram_principals are computed, this will fail.

You have a good point! I will fix it by using a local value.

in addition, the left side of the conditional is of type toset() and the right is tolist()

Why tolist()? The variable ram_principals is already a list, moreover for_each cannot iterate over lists as you know and that was the reason I used toset()... Did I miss something?

@@ -1,4 +1,7 @@
locals {
# Store the list of principals and convert to set
ram_principals = var.create_tgw && var.share_tgw ? toset(var.ram_principals) : []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simply moving this logic to a local does not correct its flaws

again, lets look at the conditional where its generally of the format conditional ? true case : false case

  • When the conditional is true, the type is of toset()
  • When the conditional is false, the type is of list since its just empty brackets

This is an issue because the types are now mis-matched (toset() vs list).

In addition, because you are using toset() on a list, any of the values in that list cannot be computed or else you will face the well known issue of not being able to use for_each on a value that is not yet known.

Lastly, changing from count to for_each is a breaking change

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess what @bryantbiggs is trying to say is since the left side of the conditional is a set you need the right one to be set too, i.e. toset([]) instead of [] which is of type list.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simply moving this logic to a local does not correct its flaws

again, lets look at the conditional where its generally of the format conditional ? true case : false case

  • When the conditional is true, the type is of toset()
  • When the conditional is false, the type is of list since its just empty brackets

This is an issue because the types are now mis-matched (toset() vs list).

In addition, because you are using toset() on a list, any of the values in that list cannot be computed or else you will face the well known issue of not being able to use for_each on a value that is not yet known.

Lastly, changing from count to for_each is a breaking change

Sorry, Bryan!

I thought you were asking to use tolist() function insted toset() 😅

There was a misunderstanding on my part and now I got it!

Indeed, for_each cannot predict these values (when computed) while using toset() function , neather iterate over a list.

I agree with u, it’s a breaking change and maybe it’s better keep this code as is, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess what @bryantbiggs is trying to say is since the left side of the conditional is a set you need the right one to be set too, i.e. toset([]) instead of [] which is of type list.

Exactly, Igor! Thanks 🙇

However, even solving this part (making the sides equal) we will face that for_each known issue mentioned by Bryan... 😢

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was take a looking on main.tf again and currently it uses count on length(var.ram_principals) here:

count = var.create_tgw && var.share_tgw ? length(var.ram_principals) : 0

How count will know the length of the list for computed values? In this case we wil face the same problem (unknown value), right?

@github-actions
Copy link

This PR has been automatically marked as stale because it has been open 30 days
with no activity. Remove stale label or comment or this PR will be closed in 10 days

@github-actions github-actions bot added the stale label Apr 29, 2023
@ebarros29
Copy link
Author

Just to remove stale label...

@github-actions github-actions bot removed the stale label May 9, 2023
@drAlberT
Copy link

Maybe an approach could be to write a local module for the association and then use for_each to instantiate many instances of the module?

@github-actions
Copy link

This PR has been automatically marked as stale because it has been open 30 days
with no activity. Remove stale label or comment or this PR will be closed in 10 days

@github-actions github-actions bot added the stale label Jun 24, 2023
@Jarodiv
Copy link
Contributor

Jarodiv commented Jun 24, 2023

keep alive

@github-actions github-actions bot removed the stale label Jun 25, 2023
@github-actions
Copy link

This PR has been automatically marked as stale because it has been open 30 days
with no activity. Remove stale label or comment or this PR will be closed in 10 days

@github-actions github-actions bot added the stale label Jul 26, 2023
@drAlberT
Copy link

drAlberT commented Aug 2, 2023

Any chance to have this done? (keep alive)

@github-actions github-actions bot removed the stale label Aug 3, 2023
@github-actions
Copy link

This PR has been automatically marked as stale because it has been open 30 days
with no activity. Remove stale label or comment or this PR will be closed in 10 days

@github-actions github-actions bot added the stale label Oct 12, 2023
@drAlberT
Copy link

ka

@github-actions github-actions bot removed the stale label Oct 13, 2023
Copy link

This PR has been automatically marked as stale because it has been open 30 days
with no activity. Remove stale label or comment or this PR will be closed in 10 days

@github-actions github-actions bot added the stale label Nov 12, 2023
@Jarodiv

This comment was marked as off-topic.

@github-actions github-actions bot removed the stale label Nov 14, 2023
Copy link

This PR has been automatically marked as stale because it has been open 30 days
with no activity. Remove stale label or comment or this PR will be closed in 10 days

@github-actions github-actions bot added the stale label Jan 11, 2024
@ebarros29
Copy link
Author

keep alive

@github-actions github-actions bot removed the stale label Jan 19, 2024
@ebarros29
Copy link
Author

Sorry guys, It has been busy days since my last comment 😫

Actually I don't know if it's worth to go ahead with this PR, since we have this huge refactor #113 already marked as wip...

@drAlberT
Copy link

Sorry guys, It has been busy days since my last comment 😫

Actually I don't know if it's worth to go ahead with this PR, since we have this huge refactor #113 already marked as wip...

If it manages this issue then it should be ok to close this PR. Otherwise I'd go ahead and the refactor will pick the fix from this PR as well.

Copy link

This PR has been automatically marked as stale because it has been open 30 days
with no activity. Remove stale label or comment or this PR will be closed in 10 days

@github-actions github-actions bot added the stale label Feb 19, 2024
@Jarodiv
Copy link
Contributor

Jarodiv commented Feb 20, 2024

"Keep alive" post

@github-actions github-actions bot removed the stale label Feb 21, 2024
Copy link

This PR has been automatically marked as stale because it has been open 30 days
with no activity. Remove stale label or comment or this PR will be closed in 10 days

@github-actions github-actions bot added the stale label Mar 23, 2024
@Jarodiv
Copy link
Contributor

Jarodiv commented Mar 23, 2024

"Keep alive" post

@github-actions github-actions bot removed the stale label Mar 24, 2024
Copy link

This PR has been automatically marked as stale because it has been open 30 days
with no activity. Remove stale label or comment or this PR will be closed in 10 days

@github-actions github-actions bot added the stale label Apr 24, 2024
@Jarodiv
Copy link
Contributor

Jarodiv commented Apr 24, 2024

"Keep alive" post

@github-actions github-actions bot removed the stale label Apr 25, 2024
Copy link

This PR has been automatically marked as stale because it has been open 30 days
with no activity. Remove stale label or comment or this PR will be closed in 10 days

@github-actions github-actions bot added the stale label May 26, 2024
@Jarodiv
Copy link
Contributor

Jarodiv commented May 27, 2024

"Keep alive" post

@github-actions github-actions bot removed the stale label May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants