fix: keep literal lambda ARNs account/region-portable (#170)#180
Merged
awsandy merged 1 commit intoJul 15, 2026
Merged
Conversation
globals_replace keeps some lambda ARNs literal instead of de-referencing them to aws_lambda_function.<name>.arn: an aws:SourceArn policy condition or a policy Resource naming the function that owns the role (de-referencing those cycles - aws-samples#170/aws-samples#172), a wildcard, a cross-region function, or a function not collected in this run. Those literals were emitted with the account id and region hard-coded inline. Route them through a new sub_acct_region() helper that substitutes this account's id/region with data.aws_caller_identity / data.aws_region - the same substitution other ARN types already get later in globals_replace. The function name stays literal, so the aws-samples#172 cycle fix is preserved; an ARN in a different account (this account's id absent) is left fully literal.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to the caveat noted on #172 (which fixed #170) — awsandy asked to fix it if I got the chance.
Problem
globals_replacekeeps certain lambda ARNs literal rather than de-referencing them toaws_lambda_function.<name>.arn:aws:SourceArnpolicy condition naming the function that owns the role (de-referencing it cycles — the Lambda execution role trust policy with aws:SourceArn creates a terraform cycle - run aborts at exit 020 #170 / fix: keep literal arn in aws:SourceArn policy conditions - terraform cycle (#170) #172 case),Resourceelement,function:*),In every one of those the ARN was emitted with the account id and region hard-coded inline:
This is the caveat #172 called out: the lambda branch returns before reaching the account/region
format(...)substitution that other ARN types get, so these ARNs are not account/region-portable.Fix
Route those literals through a new
sub_acct_region()helper that substitutes this account's id/region withdata.aws_caller_identity.current.account_id/data.aws_region.current.region— the same substitution the tail ofglobals_replacealready applies to other ARN types:The function name stays literal, so the #172 cycle fix is preserved (the role still does not depend on the lambda). An ARN in a different account (this account's id not present) is left fully literal — de-referencing it to
data.aws_caller_identitywould be wrong.Behaviour change beyond the SourceArn case
#172 fixed only the
aws:SourceArncycle. This change also makes the policyResource, wildcard and cross-region lambda literals account/region-portable — cases that previously kept account/region inline and worked, but were not portable. They now render asformat(...)with data sources. The value produced at apply time is identical, soterraform planshows no diff; flagging it because it is a behaviour change on paths that already worked.Testing
Environment: python 3.12, AWS provider 6.53.0.
globals_replaceexercised standalone against 7 line shapes:aws:SourceArn/ policyResource/ wildcard / cross-region / uncollected-function all become portableformat(...)literals with the function name kept literal; a different-account ARN stays fully literal; and an ordinary attribute for a collected function still de-references toaws_lambda_function.<name>.arn(unchanged). All 7 pass.aws:SourceArncondition (the exact Lambda execution role trust policy with aws:SourceArn creates a terraform cycle - run aborts at exit 020 #170 shape). Imported the function and its role:terraform validatepasses (no cycle), plan is0 to add, 0 to change, 0 to destroy, and the generated trust policy renders the condition as"aws:SourceArn" = format("arn:aws:lambda:%s:%s:function:<name>", data.aws_region.current.region, data.aws_caller_identity.current.account_id)—account/region portable, function name literal.