Sub topic 6
Calling AWS Services Directly
This chapter assumes you can write a basic state machine in Amazon States Language (ASL), know what an IAM role is, and have written a simple Lambda function. It covers how a Task state calls AWS services without a Lambda function in between. The next chapter covers the three integration patterns.
Why call services directly
A common first design is a Lambda function for every step. One function writes to DynamoDB, another publishes to SNS, another starts a Glue job. Each function is code you must write, package, test, monitor, and pay for. It also adds a failure point and a cold start to the workflow.
A Task state can call many AWS services itself. Step Functions makes the API call with the state machine's execution role and puts the response in the state output. You write no function code, and the workflow shows each call as its own step in the execution history.
Use a Lambda function when you need real logic, such as parsing a file or applying business rules. Use a direct integration when the step only moves data to or from a service.
Two kinds of integration
Step Functions offers two kinds of service integration. Both are selected by the Resource field of a Task state.
- Optimized integrations are built for Step Functions. They exist for a curated set of services, such as Lambda, SNS, SQS, DynamoDB, ECS, Batch, Glue, EventBridge, and nested Step Functions executions. They shape the request and response for you and support extra patterns such as
.sync. - AWS SDK integrations expose the API actions of most AWS services through a single naming scheme. They return the raw SDK response and support the request-response pattern for every service. Some also support
.syncor.waitForTaskToken, as listed in the Step Functions documentation.
The Resource ARNs differ:
| Kind | Resource format | Example |
|---|---|---|
| Optimized | `arn:aws:states:::service:action` | `arn:aws:states:::sns:publish` |
| AWS SDK | `arn:aws:states:::aws-sdk:service:action` | `arn:aws:states:::aws-sdk:s3:listObjectsV2` |
In both formats, service is the lowercase service name. For SDK integrations, action is the API action name with a lowercase first letter.
The optimized Lambda integration
The first code example in this tutorial invokes Lambda with the optimized integration:
{
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {
"FunctionName": "arn:aws:lambda:us-east-1:123456789012:function:ValidateOrder:$LATEST",
"Payload.$": "$"
},
"ResultSelector": {
"validation.$": "$.Payload"
},
"ResultPath": "$.result",
"Next": "IsValid"
}Three points matter here.
Parametersmaps onto the LambdaInvokeAPI.FunctionNameis the function, andPayloadis the event your function receives. The.$suffix onPayload.$tells Step Functions to evaluate the value as a path into the state input instead of using it as a literal string.- The response is not just your function's return value. It is the whole Invoke response, with your return value under
Payload, plus fields such asStatusCodeandExecutedVersion. This is why the example usesResultSelectorto keep only$.Payload. - The older form, where
Resourceis the function ARN itself, still works. It returns only the function's output and takes noParametersblock shaped like the Invoke API. New workflows should usearn:aws:states:::lambda:invoke, which lets you set options such as the qualifier or invocation type.
Pinning a version or alias in the ARN, as the example does with :$LATEST, controls which code runs. In production, point at an alias so a deployment does not change what a running workflow calls.
An optimized integration without Lambda
This state publishes a message to an SNS topic and needs no function:
{
"StartAt": "NotifyCustomer",
"States": {
"NotifyCustomer": {
"Type": "Task",
"Resource": "arn:aws:states:::sns:publish",
"Parameters": {
"TopicArn": "arn:aws:sns:us-east-1:123456789012:order-events",
"Message.$": "States.Format('Order {} was accepted', $.orderId)",
"Subject": "Order update"
},
"ResultPath": null,
"End": true
}
}
}Parameters uses the same field names as the SNS Publish API. States.Format is an intrinsic function that builds a string from the input. Setting ResultPath to null discards the service response, so the state's output equals its input. This is useful when you only care that the call happened.
An AWS SDK integration
The next state reads one item from a DynamoDB table. It uses the SDK integration, so the parameters follow the DynamoDB API exactly, including its typed attribute values:
{
"StartAt": "GetOrder",
"States": {
"GetOrder": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
"Parameters": {
"TableName": "Orders",
"Key": {
"orderId": {
"S.$": "$.orderId"
}
}
},
"ResultSelector": {
"status.$": "$.Item.status.S"
},
"ResultPath": "$.order",
"End": true
}
}
}Here the response keeps DynamoDB's typed format, so the status is at $.Item.status.S and not $.Item.status. The optimized arn:aws:states:::dynamodb:getItem integration returns the same shape. If you want plain JSON values, convert them in a Lambda function or reshape them with ResultSelector.
Naming gotchas with SDK integrations
- Parameter names are PascalCase. SDK integrations use the API's field names, such as
TableNameandKey, whatever naming your SDK language uses. In the Python SDK,boto3calls useTableNameas well, but the Java and JavaScript SDKs use different conventions, so rely on the API reference. - Response names are PascalCase too. Expect
$.Item,$.Contents, and similar keys. - Some actions have different permission names. The IAM action for an SDK call is not always the same as the API action. For example, calls to
aws-sdk:s3:listObjectsV2need thes3:ListBucketpermission. Check the service's IAM documentation when a call is denied. - Binary data is not supported. Task states handle JSON. Blob parameters must be base64 strings where the API accepts them.
Request and response limits
Direct integrations are bound by the quotas of Step Functions and of the target service.
- The input or output of a state is limited to 256 KiB in a Standard or Express workflow. A large service response, such as a long S3 listing, can exceed it and fail the state with
States.DataLimitExceeded. - When a response is large, use
ResultSelectorto keep only the fields you need. For big payloads, store the data in S3 and pass the object key through the workflow. - Each service enforces its own throttling. A direct call that is throttled returns a service error that you can match in
Retry, covered in a later chapter.
Errors from direct integrations
When a service call fails, the state fails with an error name derived from the service error. For SDK integrations, the name has the form ServiceName.ExceptionName, for example DynamoDb.ResourceNotFoundException or S3.NoSuchKeyException. Optimized integrations use the same pattern for most services, with Lambda using names such as Lambda.TooManyRequestsException.
You match these names in ErrorEquals inside Retry and Catch. Use specific names for transient errors and States.ALL as a last-resort catch. The exact error name appears in the execution history, so the fastest way to find it is to trigger the failure once and read the event.
Permissions
A direct integration calls the service with the execution role, not with your own credentials. Each Task state therefore needs a matching permission on that role. For the SNS example above, the role needs sns:Publish on the topic ARN. For the DynamoDB example, it needs dynamodb:GetItem on the table ARN.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sns:Publish",
"Resource": "arn:aws:sns:us-east-1:123456789012:order-events"
},
{
"Effect": "Allow",
"Action": "dynamodb:GetItem",
"Resource": "arn:aws:dynamodb:us-east-1:123456789012:table/Orders"
}
]
}A missing permission produces a failure with an access-denied error at the Task state, not at deployment time. Execution roles get a full treatment in a later chapter.
Choosing between Lambda and a direct call
| Situation | Better choice |
|---|---|
| Put a message on SQS, publish to SNS, or write one DynamoDB item | Direct integration |
| Start an ECS task or Glue job and wait for it | Optimized integration with `.sync` |
| Transform or validate data with custom logic | Lambda |
| Call a service with no SDK integration, or a third-party API | Lambda |
| Reshape a DynamoDB response into plain JSON | Lambda, or `ResultSelector` for simple cases |
A direct call has fewer moving parts, but it also moves logic into JSON, which is harder to unit test than code. If a state needs several Parameters tricks to work, a small Lambda function is often clearer.
How a call flows through the state
Summary of the main points
- A Task state calls AWS services directly through optimized or AWS SDK integrations, with no Lambda function needed.
- Optimized ARNs look like
arn:aws:states:::sns:publish. SDK ARNs look likearn:aws:states:::aws-sdk:s3:listObjectsV2. Parametersfollows the target API's field names. Use.$to read values from the state input.- Responses use the API's shape, so use
ResultSelectorandResultPathto keep only what you need. - The execution role must allow each API call you make.
Exercise
A state uses arn:aws:states:::lambda:invoke and the next state fails because $.validation.valid does not exist. The Lambda function returns {"valid": true}. The Task state has no ResultSelector or ResultPath. Where is the function's return value in the output, and how do you fix the path?
Show answer
The optimized integration returns the full Invoke response, so the function's return value is under $.Payload, and the whole output replaces the state input. The value is at $.Payload.valid. To fix it, either read $.Payload.valid in the next state, or add "ResultSelector": {"validation.$": "$.Payload"} and "ResultPath": "$.result" so the value sits at $.result.validation.valid and the original input is kept.
Exercise
You add an aws-sdk:s3:getObject Task state to a workflow. The execution fails at that state with an access-denied error, although the bucket policy allows your own IAM user. What is the likely cause and fix?
Show answer
The call runs with the state machine's execution role, not with your user's credentials. Add an IAM statement to the execution role that allows s3:GetObject on the object ARNs, such as arn:aws:s3:::my-bucket/*. Also check that the bucket policy does not deny the role.
Exercise
A direct DynamoDB getItem returns {"Item": {"status": {"S": "PAID"}}}. Write a ResultSelector that outputs {"status": "PAID"}.
Show answer
"ResultSelector": {"status.$": "$.Item.status.S"}