Skip to content

fix(apigateway): duplicate CloudWatch role URN on fresh accounts#6911

Open
jasonrowsell wants to merge 2 commits into
anomalyco:devfrom
jasonrowsell:fix-apigateway-account-role-race
Open

fix(apigateway): duplicate CloudWatch role URN on fresh accounts#6911
jasonrowsell wants to merge 2 commits into
anomalyco:devfrom
jasonrowsell:fix-apigateway-account-role-race

Conversation

@jasonrowsell

@jasonrowsell jasonrowsell commented Jun 11, 2026

Copy link
Copy Markdown

Fixes #6199, likely #6677 too. I was also getting this error

On a fresh AWS account cloudwatchRoleArn is unset, so the guard in setupApiGatewayAccount never short-circuits and every gateway registers an iam.Role under the same logical name — two or more gateways and the deploy fails with Duplicate resource URN. It dies before the ARN is written back, so retries hit the same race. Any account that ever completed a single-gateway deploy short-circuits, which is why this has been hard to reproduce.

Commit 1 registers the role once per process, same as the LambdaEncryptionKey dedupe in function.ts. No logical names change, so existing stacks see no churn.

Commit 2: since terraform-provider-aws v6, destroying aws_api_gateway_account resets cloudwatchRoleArn to null. The setup only exists in the program while the ARN is unset, so bootstrap never converges: set, reset on the next deploy, bootstrap again with a fresh orphan role — the pile-up in #6677. retainOnDelete: true keeps the account-wide setting in place and the cycle converges after the first deploy.

Since terraform-provider-aws v6, destroying aws_api_gateway_account resets cloudwatchRoleArn to null. The setup resource only exists in the program while the account has no role configured, so the cycle was: bootstrap sets the ARN, the next deploy drops the setup resource and the destroy resets the ARN, and the deploy after that bootstraps again with a fresh orphan role. Retaining the resource on delete keeps the account settings and converges after the first deploy.
@jasonrowsell jasonrowsell changed the title Fix duplicate URN when multiple API gateways deploy to a fresh account fix(apigateway): duplicate CloudWatch role URN on fresh accounts Jun 11, 2026
cloudwatchRoleArn: useCloudWatchRole(opts).arn,
},
{ provider: opts.provider },
{ retainOnDelete: true, provider: opts.provider },

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Calling this one out since it's the only behavioral change for existing users: since terraform-provider-aws v6, destroying aws_api_gateway_account resets cloudwatchRoleArn to null (account.go), and the setting is account/region-wide. Without retainOnDelete here, removing one app resets the role for every other stack in the account — and it's also what stops the bootstrap from converging (#6677): the setup only exists in the program while the ARN is unset, so it gets destroyed on the deploy after bootstrap, the destroy resets the ARN, and the next deploy creates another orphan role.

The trade-off is that a full teardown now leaves the account setting in place, pointing at the (already retained) role. That seemed strictly better than resetting shared account state on sst remove.

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.

Duplicate resource URN 'urn:pulumi:stage::app::aws:iam/role:Role::APIGatewayPushToCloudWatchLogsRole'

1 participant