AWS Compute Blog
Introducing cross-account access capabilities for AWS Step Functions
This post is written by Siarhei Kazhura, Senior Solutions Architect, Serverless.
AWS Step Functions allows you to integrate with more than 220 AWS services by using optimized integrations (for services such as AWS Lambda), and AWS SDK integrations. These capabilities provide the ability to build robust solutions using AWS Step Functions as the engine behind the solution.
Many customers are using multiple AWS accounts for application development. Until today, customers had to rely on resource-based policies to make cross-account access for Step Functions possible. With resource-based policies, you can specify who has access to the resource and what actions they can perform on it.
Not all AWS services support resource-based policies. For example, it is possible to enable cross-account access via resource-based policies with services like AWS Lambda, Amazon SQS, or Amazon SNS. However, services such as Amazon DynamoDB do not support resource-based policies, so your workflows can only use Step Functions’ direct integration if it belongs to the same account.
Now, customers can take advantage of identity-based policies in Step Functions so your workflow can directly invoke resources in other AWS accounts, thus allowing cross-account service API integrations.
Overview
This example demonstrates how to use cross-account capability using two AWS accounts:
- A trusted AWS account (account ID 111111111111) with a Step Functions workflow named SecretCacheConsumerWfw, and an IAM role named TrustedAccountRl.
- A trusting AWS account (account ID 222222222222) with a Step Functions workflow named SecretCacheWfw, and two IAM roles named TrustingAccountRl, and SecretCacheWfwRl.
At a high level:
- The SecretCacheConsumerWfw workflow runs under TrustedAccountRl role in the account 111111111111. The TrustedAccountRl role has permissions to assume the TrustingAccountRl role from the account 222222222222.
- The FetchConfiguration Step Functions task fetches the TrustingAccountRl role ARN, the SecretCacheWfw workflow ARN, and the secret ARN (all these resources belong to the Trusting AWS account).
- The GetSecretCrossAccount Step Functions task has a Credentials field with the TrustingAccountRl role ARN specified (fetched in the step 2).
- The GetSecretCrossAccount task assumes the TrustingAccountRl role during the SecretCacheConsumerWfw workflow execution.
- The SecretCacheWfw workflow (that belongs to the account 222222222222) is invoked by the SecretCacheConsumerWfw workflow under the TrustingAccountRl role.
- The results are returned to the SecretCacheConsumerWfw workflow that belongs to the account 111111111111.
The SecretCacheConsumerWfw workflow definition specifies the Credentials field and the RoleArn. This allows the GetSecretCrossAccount step to assume an IAM role that belongs to a separate AWS account:
{
"StartAt": "FetchConfiguration",
"States": {
"FetchConfiguration": {
"Type": "Task",
"Next": "GetSecretCrossAccount",
"Parameters": {
"Name": "<ConfigurationParameterName>"
},
"Resource": "arn:aws:states:::aws-sdk:ssm:getParameter",
"ResultPath": "$.Configuration",
"ResultSelector": {
"Params.$": "States.StringToJson($.Parameter.Value)"
}
},
"GetSecretCrossAccount": {
"End": true,
"Type": "Task",
"ResultSelector": {
"Secret.$": "States.StringToJson($.Output)"
},
"Resource": "arn:aws:states:::aws-sdk:sfn:startSyncExecution",
"Credentials": {
"RoleArn.$": "$.Configuration.Params.trustingAccountRoleArn"
},
"Parameters": {
"Input.$": "$.Configuration.Params.secret",
"StateMachineArn.$": "$.Configuration.Params.trustingAccountWorkflowArn"
}
}
}
}
Permissions
At a high level:
- The TrustedAccountRl role belongs to the account 111111111111.
- The TrustingAccountRl role belongs to the account 222222222222.
- A trust relationship setup between the TrustedAccountRl and the TrustingAccountRl role.
- The SecretCacheConsumerWfw workflow is executed under the TrustedAccountRl role in the account 111111111111.
- The SecretCacheWfw is executed under the SecretCacheWfwRl role in the account 222222222222.
The TrustedAccountRl role (1) has the following trust policy setup that allows the SecretCacheConsumerWfw workflow to assume (4) the role.
{
"RoleName": "<TRUSTED_ACCOUNT_ROLE_NAME>",
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "states.<REGION>.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
}
The TrustedAccountRl role (1) has the following permissions configured that allow it to assume (3) the TrustingAccountRl role (2).
{
"RoleName": "<TRUSTED_ACCOUNT_ROLE_NAME>",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::<TRUSTING_ACCOUNT>:role/<TRUSTING_ACCOUNT_ROLE_NAME>",
"Effect": "Allow"
}
]
}
}
The TrustedAccountRl role (1) has the following permissions setup that allow it to access Parameter Store, a capability of AWS Systems Manager, and fetch the required configuration.
{
"RoleName": "<TRUSTED_ACCOUNT_ROLE_NAME>",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"ssm:DescribeParameters",
"ssm:GetParameter",
"ssm:GetParameterHistory",
"ssm:GetParameters"
],
"Resource": "arn:aws:ssm:<REGION>:<TRUSTED_ACCOUNT>:parameter/<CONFIGURATION_PARAM_NAME>",
"Effect": "Allow"
}
]
}
}
The TrustingAccountRl role (2) has the following trust policy that allows it to be assumed (3) by the TrustedAccountRl role (1). Notice the Condition field setup. This field allows us to further control which account and state machine can assume the TrustingAccountRl role, preventing the confused deputy problem.
{
"RoleName": "<TRUSTING_ACCOUNT_ROLE_NAME>",
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::<TRUSTED_ACCOUNT>:role/<TRUSTED_ACCOUNT_ROLE_NAME>"
},
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": {
"sts:ExternalId": "arn:aws:states:<REGION>:<TRUSTED_ACCOUNT>:stateMachine:<CACHE_CONSUMER_WORKFLOW_NAME>"
}
}
}
]
}
}
The TrustingAccountRl role (2) has the following permissions configured that allow it to start Step Functions Express Workflows execution synchronously. This capability is needed because the SecretCacheWfw workflow is invoked by the SecretCacheConsumerWfw workflow under the TrustingAccountRl role via a StartSyncExecution API call.
{
"RoleName": "<TRUSTING_ACCOUNT_ROLE_NAME>",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": "states:StartSyncExecution",
"Resource": "arn:aws:states:<REGION>:<TRUSTING_ACCOUNT>:stateMachine:<SECRET_CACHE_WORKFLOW_NAME>",
"Effect": "Allow"
}
]
}
}
The SecretCacheWfw workflow is running under a separate identity – the SecretCacheWfwRl role. This role has the permissions that allow it to get secrets from AWS Secrets Manager, read/write to DynamoDB table, and invoke Lambda functions.
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"secretsmanager:getSecretValue",
],
"Resource": "arn:aws:secretsmanager:<REGION>:<TRUSTING_ACCOUNT>:secret:*",
"Effect": "Allow"
},
{
"Action": "dynamodb:GetItem",
"Resource": "arn:aws:dynamodb:<REGION>:<TRUSTING_ACCOUNT>:table/<SECRET_CACHE_DDB_TABLE_NAME>",
"Effect": "Allow"
},
{
"Action": "lambda:InvokeFunction",
"Resource": [
"arn:aws:lambda:<REGION>:<TRUSTING_ACCOUNT>:function:<CACHE_SECRET_FUNCTION_NAME>",
"arn:aws:lambda:<REGION>:<TRUSTING_ACCOUNT>:function:<CACHE_SECRET_FUNCTION_NAME>:*"
],
"Effect": "Allow"
}
]
}
Comparing with resource-based policies
To implement the solution above using resource-based policies, you must front the SecretCacheWfw with a resource that supports resource base policies. You can use Lambda for this purpose. A Lambda function has a resource permissions policy that allows for the access by SecretCacheConsumerWfw workflow.
The function proxies the call to the SecretCacheWfw, waits for the workflow to finish (synchronous call), and yields the result back to the SecretCacheConsumerWfw. However, this approach has a few disadvantages:
- Extra cost: With Lambda you are charged based on the number of requests for your function, and the duration it takes for your code to run.
- Additional code to maintain: The code must take the payload from the SecretCacheConsumerWfw workflow and pass it to the SecretCacheWfw workflow.
- No out-of-the-box error handling: The code must handle errors correctly, retry the request in case of a transient error, provide the ability to do exponential backoff, and provide a circuit breaker in case of persistent errors. Error handling capabilities are provided natively by Step Functions.
The identity-based policy permission solution provides multiple advantages over the resource-based policy permission solution in this case.
However, resource-based policy permissions provide some advantages and can be used in conjunction with identity-based policies. Identity-based policies and resource-based policies are both permissions policies and are evaluated together:
- Single point of entry: Resource-based policies are attached to a resource. With resource-based permissions policies, you control what identities that do not belong to your AWS account have access to the resource at the resource level. This allows for easier reasoning about what identity has access to the resource. AWS Identity and Access Management Access Analyzer can help with the identity-based policies, providing an ability to identify resources that are shared with an external identity.
- The principal that accesses a resource via a resource-based policy still works in the trusted account and does not have to give its permissions to receive the cross-account role permissions. In this example, SecretCacheConsumerWfw still runs under TrustedAccountRl role, and does not need to assume an IAM role in the Trusting AWS account to access the Lambda function.
Refer to the how IAM roles differ from resource-based policies article for more information.
Solution walkthrough
To follow the solution walkthrough, visit the solution repository. The walkthrough explains:
- Prerequisites required.
- Detailed solution deployment walkthrough.
- Solution testing.
- Cleanup process.
- Cost considerations.
Conclusion
This post demonstrates how to create a Step Functions Express Workflow in one account and call it from a Step Functions Standard Workflow in another account using a new credentials capability of AWS Step Functions. It provides an example of a cross-account IAM roles setup that allows for the access. It also provides a walk-through on how to use AWS CDK for TypeScript to deploy the example.
For more serverless learning resources, visit Serverless Land.