Sub topic 11
Execution roles and least-privilege permissions
This chapter assumes you can read IAM policy JSON and know how a role's trust policy differs from its permissions policy. It also assumes you have written the Task, Retry, and Catch states from earlier chapters. Here you will write the role that decides what your state machine is allowed to do.
Two sets of permissions
Step Functions involves two separate permission questions.
- Who may use the state machine. A person, application, or EventBridge rule needs IAM permission to call
states:StartExecutionand similar actions. This is ordinary identity-based IAM on the caller. - What the state machine may do. Every state machine has an execution role. When an execution runs, Step Functions assumes that role and makes each Task state's API call with it. The caller's permissions are not passed along.
Because of this split, a user who can start an execution does not need lambda:InvokeFunction. Only the execution role needs it. A user with broad access also cannot make a workflow do more than its role allows.
The trust policy
The trust policy names which principal may assume the role. For Step Functions that principal is the service states.amazonaws.com. Add conditions so that only your state machine can use the role. This prevents the confused deputy problem, where another account tricks the service into acting with your role.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "states.amazonaws.com" },
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": { "aws:SourceAccount": "123456789012" },
"ArnLike": {
"aws:SourceArn": "arn:aws:states:us-east-1:123456789012:stateMachine:OrderWorkflow"
}
}
}
]
}The state machine ARN is predictable from its name, so you can write the condition before you create the state machine.
A permissions policy for the order workflow
Take the OrderWorkflow from the earlier chapters. It invokes ValidateOrder, and its approval variant sends a message to an SQS queue. Add CloudWatch Logs and X-Ray, and the permissions policy looks like this.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "InvokeValidateOrder",
"Effect": "Allow",
"Action": "lambda:InvokeFunction",
"Resource": [
"arn:aws:lambda:us-east-1:123456789012:function:ValidateOrder",
"arn:aws:lambda:us-east-1:123456789012:function:ValidateOrder:*"
]
},
{
"Sid": "SendApprovalMessage",
"Effect": "Allow",
"Action": "sqs:SendMessage",
"Resource": "arn:aws:sqs:us-east-1:123456789012:approvals"
},
{
"Sid": "DeliverExecutionLogs",
"Effect": "Allow",
"Action": [
"logs:CreateLogDelivery",
"logs:GetLogDelivery",
"logs:UpdateLogDelivery",
"logs:DeleteLogDelivery",
"logs:ListLogDeliveries",
"logs:PutResourcePolicy",
"logs:DescribeResourcePolicies",
"logs:DescribeLogGroups"
],
"Resource": "*"
},
{
"Sid": "Tracing",
"Effect": "Allow",
"Action": [
"xray:PutTraceSegments",
"xray:PutTelemetrySegments",
"xray:GetSamplingRules",
"xray:GetSamplingTargets"
],
"Resource": "*"
}
]
}Three details matter here.
- Scope each statement to the exact resource. Lambda and SQS actions name the specific function and queue, never
*. - List both the unqualified and the qualified Lambda ARN. The Task state in this course calls
ValidateOrder:$LATEST, which is a qualified ARN. A policy that lists only the unqualified ARN does not cover it, so the:*entry is required. - Use
*only where the service requires it. The log delivery and X-Ray actions do not support resource-level restrictions, soResource: "*"is the correct scope for those two statements.
Create the role and the state machine
aws iam create-role \
--role-name OrderWorkflowRole \
--assume-role-policy-document file://trust-policy.json
aws iam put-role-policy \
--role-name OrderWorkflowRole \
--policy-name OrderWorkflowPermissions \
--policy-document file://permissions-policy.json
aws stepfunctions create-state-machine \
--name OrderWorkflow \
--definition file://definition.json \
--role-arn arn:aws:iam::123456789012:role/OrderWorkflowRoleThe identity that runs create-state-machine also needs iam:PassRole on OrderWorkflowRole. Restrict it with the condition iam:PassedToService set to states.amazonaws.com. This stops a developer from attaching a powerful role to a state machine they control. Without the restriction, creating a state machine becomes a way to escalate privileges.
Permissions by integration pattern
The integration pattern you choose changes what the role needs. The .sync pattern makes Step Functions poll or listen for completion, and that requires extra actions.
| Task resource | Actions the role needs |
|---|---|
| `lambda:invoke` | `lambda:InvokeFunction` on the function ARN and `:*` |
| `sqs:sendMessage` (and `.waitForTaskToken`) | `sqs:SendMessage` on the queue ARN |
| `states:startExecution.sync:2` | `states:StartExecution`, `states:DescribeExecution`, `states:StopExecution`, plus `events:PutTargets`, `events:PutRule`, `events:DescribeRule` |
| Distributed Map reading and writing S3 | `s3:ListBucket` and `s3:GetObject` on the input, `s3:PutObject` on the results prefix, and `states:StartExecution` for the child workflow |
The .sync row is the one people miss. Step Functions creates a managed EventBridge rule to learn when the nested execution finishes, and the role needs the events: actions to manage it. Other .sync integrations, such as ECS runTask.sync, have their own action lists. Check the integration's documentation page when you add one.
The .waitForTaskToken pattern adds a permission to a different identity. The worker that finishes the work calls SendTaskSuccess, SendTaskFailure, or SendTaskHeartbeat using its own credentials, as in the earlier approve function. So grant states:SendTaskSuccess and states:SendTaskFailure to the worker's role, not the execution role. These task-token actions do not support resource-level scoping, so Resource: "*" is expected.
Callers and events
For a caller that only starts one workflow, grant exactly that.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "states:StartExecution",
"Resource": "arn:aws:states:us-east-1:123456789012:stateMachine:OrderWorkflow"
}
]
}Express workflows called synchronously need states:StartSyncExecution instead. When an EventBridge rule or a Pipe starts the workflow, the permission goes on that service's own role, and the same rule applies: one action, one state machine ARN.
Diagnosing permission failures
A missing permission appears as a failed Task state. In the execution history, the TaskFailed event carries a cause such as "User ... assumed-role/OrderWorkflowRole ... is not authorized to perform: lambda:InvokeFunction on resource ...". Read the three parts of that message: the role, the denied action, and the resource ARN. The resource ARN is usually the clue. If it ends in :$LATEST and your policy lacks :*, that is the bug.
Step Functions reports some of these failures as the error name States.Permissions. It does not retry them usefully, because retrying cannot add a permission. Your Catch with States.ALL already routes them to OrderFailed, so the cause string is stored under $.error for later inspection.
Common mistakes
- Using
AWSLambdaRoleorAdministratorAccessas the execution role. These grant access to every function or every service in the account. - Forgetting
iam:PassRole, which makescreate-state-machineorupdate-state-machinefail with an access denied error. - Omitting the logging and X-Ray statements, so that enabling logging on the state machine fails or produces no logs.
- Sharing one role among many state machines. A shared role accumulates the union of every workflow's permissions, so use one role per state machine.
- Granting a wildcard such as
lambda:*to avoid looking up the exact action name.
Exercise
Your state machine calls arn:aws:lambda:us-east-1:123456789012:function:Charge:live through the lambda:invoke integration. The execution role's policy allows lambda:InvokeFunction on arn:aws:lambda:us-east-1:123456789012:function:Charge, and the Task fails with an authorization error. What do you change, and why?
Show answer
Add the qualified ARN to the policy's Resource list, either arn:aws:lambda:us-east-1:123456789012:function:Charge:live or arn:aws:lambda:us-east-1:123456789012:function:Charge:*. A policy that lists only the unqualified function ARN does not match an invocation that names an alias or version.
Exercise
A developer with states:StartExecution on OrderWorkflow complains that the workflow cannot send messages to an SQS queue. Should you add sqs:SendMessage to the developer's permissions? Which identity needs it?
Show answer
No. The Task state calls SQS with the execution role, not the caller's identity. Add sqs:SendMessage, scoped to the queue ARN, to the execution role. Also check the queue's resource policy and any permissions boundary or service control policy if the denial persists.