Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· OpenAIAi2 WildChat-4.8M (AI2)

I have a lambda in one account and I want to acomplish with cloudformation template, I…

  1. User

    I have a lambda in one account and I want to acomplish with cloudformation template, I want to create a step function that invokes a lambda in another account.

  2. Assistant

    To create a Step Function that invokes a Lambda function in another AWS account using CloudFormation, you need to create a resource-based policy in the target Lambda function and then use the AWS Step Functions service integration with Lambda:

    1. First, ensure that the Lambda function in the target account has a resource-based policy that grants permission to the source account to invoke it. Here's an example of such policy:
    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Principal": {
            "AWS": "arn:aws:iam::SOURCE_ACCOUNT_ID:root"
          },
          "Action": "lambda:InvokeFunction",
          "Resource": "arn:aws:lambda:TARGET_REGION:TARGET_ACCOUNT_ID:function:TARGET_LAMBDA_NAME"
        }
      ]
    }
    

    Replace SOURCE_ACCOUNT_ID, TARGET_REGION, TARGET_ACCOUNT_ID, and TARGET_LAMBDA_NAME with appropriate values. Add this policy to your Lambda function in the target account.

    1. Now create a CloudFormation template in the source account with a Step Function using AWS Step Functions service integration:
    Resources:
      MyStateMachine:
        Type: 'AWS::StepFunctions::StateMachine'
        Properties:
          DefinitionString: !Sub |
            {
              "StartAt": "InvokeLambda",
              "States": {
                "InvokeLambda": {
                  "Type": "Task",
                  "Resource": "arn:aws:states:::lambda:invoke",
                  "Parameters": {
                    "FunctionName": "arn:aws:lambda:${TARGET_REGION}:${TARGET_ACCOUNT_ID}:function:${TARGET_LAMBDA_NAME}",
                    "Payload": {
                      "Input.$": "$"
                    }
                  },
                  "End": true
                }
              }
            }
          RoleArn: !GetAtt MyStateMachineRole.Arn
    
      MyStateMachineRole:
        Type: 'AWS::IAM::Role'
        Properties:
          AssumeRolePolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Principal:
                  Service: 'states.amazonaws.com'
                Action: 'sts:AssumeRole'
          Path: '/'
          ManagedPolicyArns:
            - 'arn:aws:iam::aws:policy/service-role/AWSLambdaRole'
          Policies:
            - PolicyName: MyStateMachinePolicy
              PolicyDocument:
                Version: '2012-10-17'
                Statement:
                  - Effect: Allow
                    Action:
                      - 'lambda:InvokeFunction'
                    Resource: 'arn:aws:lambda:${TARGET_REGION}:${TARGET_ACCOUNT_ID}:function:${TARGET_LAMBDA_NAME}'
    

    Replace ${TARGET_REGION}, ${TARGET_ACCOUNT_ID}, and ${TARGET_LAMBDA_NAME} with appropriate values. This CloudFormation template will create a Step Function and a corresponding IAM role for the Step Function.

    1. Finally, deploy your CloudFormation stack using the AWS Console, AWS CLI, or SDKs.

    Note: If your Lambda functions are in different regions, you may also need to enable Lambda cross-region invocation by following this guide: https://aws.amazon.com/premiumsupport/knowledge-center/lambda-cross-account-cross-region/

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.