Sub topic 11
Execution roles and least-privilege permissions
This chapter assumes you can read IAM policy JSON and know the difference between a trust policy and a permissions policy. It also assumes you have seen the Task states from earlier chapters.
Two identities, two jobs
Two different identities are involved when a workflow runs.
- The caller starts the execution. Calling
states:StartExecutionneeds the caller's own IAM permissions. - The execution role is the role Step Functions assumes while the state machine runs. Every AWS call made by a Task state uses this role, not the caller's permissions.
This split means a caller does not need access to Lambda or SQS to start a workflow that uses them. The execution role holds that access.
The trust policy
The role must trust the Step Functions service. Without this, the state machine cannot assume it.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "states.amazonaws.com" },
"Action": "sts:AssumeRole"
}
]
}A permissions policy that matches the state machine
Write the permissions policy by reading your definition and listing each action it performs. Take the order workflow from earlier chapters. It invokes the ValidateOrder Lambda function. A second version sends an approval message to SQS. The matching 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": "SendApprovalRequest",
"Effect": "Allow",
"Action": "sqs:SendMessage",
"Resource": "arn:aws:sqs:us-east-1:123456789012:approvals"
}
]
}Three details matter here:
- Each statement names a specific resource. Avoid
"Resource": "*"for service actions. - Qualified ARNs need their own entry. The first example used
ValidateOrder:$LATEST. A policy that lists only the unqualified ARN does not cover that call, so the:*entry is needed. - The action name follows the API call, not the state type. The
sqs:sendMessageintegration requiressqs:SendMessage.
Permissions for observability
Logging and tracing add permissions that the Task states do not need. If you enable CloudWatch Logs or X-Ray on the state machine, add them to the execution role.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogDelivery",
"logs:GetLogDelivery",
"logs:UpdateLogDelivery",
"logs:DeleteLogDelivery",
"logs:ListLogDeliveries",
"logs:PutResourcePolicy",
"logs:DescribeResourcePolicies",
"logs:DescribeLogGroups"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"xray:PutTraceSegments",
"xray:PutTelemetryRecords",
"xray:GetSamplingRules",
"xray:GetSamplingTargets"
],
"Resource": "*"
}
]
}These actions do not support resource-level restrictions, so * is the correct value here.
Common errors
AccessDeniedExceptionon a Task state. The execution history shows the denied action and resource. Compare them with your policy. A missing:*on a Lambda ARN is a frequent cause.- Cannot create the state machine. The role's trust policy does not name
states.amazonaws.com, or you lackiam:PassRoleon the role. - Callback never resumes. With
.waitForTaskToken, the worker callsSendTaskSuccessusing its own identity. It needsstates:SendTaskSuccess, andstates:SendTaskFailurefor errors. The execution role is not involved. - Shared roles grow too broad. One role for every workflow gathers all their permissions. Create one role per state machine so a change in one workflow cannot widen access for another.
Exercise
A state machine calls lambda:invoke on arn:aws:lambda:us-east-1:123456789012:function:ValidateOrder:$LATEST. The role's policy allows lambda:InvokeFunction only on arn:aws:lambda:us-east-1:123456789012:function:ValidateOrder. The Task state fails with an access error. What do you change, and why?
Show answer
Add arn:aws:lambda:us-east-1:123456789012:function:ValidateOrder:* to the Resource list. The call uses a qualified ARN (with a version or alias), and the unqualified ARN in the policy does not match it. Keep the resource scoped to this one function instead of switching to *.