Skip to content

fix(iam): resolve local Lambda Ref to static ARN (#470)#746

Merged
zirkelc merged 1 commit intomasterfrom
fix-470-circular-dependency
Apr 4, 2026
Merged

fix(iam): resolve local Lambda Ref to static ARN (#470)#746
zirkelc merged 1 commit intomasterfrom
fix-470-circular-dependency

Conversation

@VirtueMe
Copy link
Copy Markdown
Collaborator

@VirtueMe VirtueMe commented Apr 2, 2026

Summary

Fixes two bugs triggered when a task state uses Resource: !Ref localFunction:

  • MalformedPolicyDocument — the IAM role's policy resource contained { Ref: ParseCSVLambdaFunction }, which CloudFormation resolves to the function name (e.g. my-service-dev-parseCSV), not its ARN. IAM rejects non-ARN resources.
  • InvalidDefinition — the state machine definition received the same function name string as its Resource. Step Functions rejects anything that isn't an ARN.

Both bugs also introduce a CloudFormation resource dependency (StateMachineRole → Lambda) that causes the circular dependency error reported in #470 when the Lambda has an env var referencing the state machine ARN.

Fix

Adds resolveLambdaFunctionName to lib/utils/aws.js. When a Ref points to a local function whose deployed name is known at compile time (via serverless.service.functions[key].name), a static Fn::Sub ARN is emitted instead — no CloudFormation resource reference, no dependency, no cycle.

Two call sites fixed:

File Path
iamStrategies/lambda.js getFallbackPermissions (direct !Ref resource) and getPermissions (FunctionName: !Ref SDK integration)
compileStateMachines.js Definition string Fn::Sub params — local Lambda Refs are inlined as static ARNs directly in the definition string

Integration test

Adds a circular-dependency fixture reproducing the exact scenario from #470. LocalStack validates both bugs:

  • Without fix: CircularMachineRole fails with MalformedPolicyDocument, then CircularMachine fails with InvalidDefinition
  • With fix: full stack deploys cleanly

Test plan

  • npm test — 534 passing
  • npx osls circular-dependency:deploy --stage test deploys successfully against LocalStack

Closes #470

🤖 Generated with Claude Code

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 2, 2026

Open in StackBlitz

npm i https://pkg.pr.new/serverless-operations/serverless-step-functions@746

commit: 389288b

…PolicyDocument

When a task state uses `Resource: !Ref localFunction`, two bugs occur:

1. The IAM role's policy resource contains `{ Ref: ParseCSVLambdaFunction }`.
   CloudFormation resolves a Lambda function Ref to the function *name*
   (e.g. `my-service-dev-parseCSV`), not its ARN — IAM rejects this with
   MalformedPolicyDocument.

2. The state machine definition also receives the function name string as
   its Resource. Step Functions rejects this with InvalidDefinition.

In both cases the Ref also introduces a CloudFormation resource dependency
that can cause the circular dependency error reported in #470 when the
referenced Lambda has an env var pointing back at the state machine.

Fix: add `resolveLambdaFunctionName` to `lib/utils/aws.js`. When a Ref
points to a local function whose deployed name is known at compile time
(via `serverless.service.functions[key].name`), callers emit a static
`Fn::Sub` ARN string instead of the Ref. Two call sites are fixed:

- `iamStrategies/lambda.js` — IAM policy resource for both the direct
  lambda ARN path (getFallbackPermissions) and the lambda:invoke SDK
  integration path (getPermissions)
- `compileStateMachines.js` — definition string Fn::Sub params: local
  Lambda Refs are inlined as static ARNs directly in the definition
  string, eliminating both the CF dependency and the name-not-ARN error

Adds a `circular-dependency` integration fixture reproducing #470.
LocalStack confirms: without the fix the IAM role fails with
MalformedPolicyDocument and the state machine fails with InvalidDefinition;
with the fix the full stack deploys cleanly.

Closes #470

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@VirtueMe VirtueMe force-pushed the fix-470-circular-dependency branch from d954524 to 389288b Compare April 2, 2026 08:08
@zirkelc zirkelc merged commit aaf9dec into master Apr 4, 2026
4 checks passed
@zirkelc zirkelc deleted the fix-470-circular-dependency branch April 4, 2026 04:35
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.

Circular dependency calling intrinsic function

2 participants