title: How to invoke AWS lambda from another lambda within SAM local?
body: <p>I am using AWS SAM with Python. My goal is to have two Lambdas:</p>
  
  <ul>
  <li>Function A: A normal synchronous Lambda which will invoke Function B, then return quickly</li>
  <li>Function B: A long-running asynchronous Event Lambda</li>
  </ul>
  
  <p>There are a couple other SO questions which deal with this scenario, but as far as I can tell none have touched on how to do it when deploying SAM locally.</p>
  
  <p>Here is my SAM template file:</p>
  
  <pre><code># template.yaml
  
  Resources:
    FunctionA:
      # PUT /functions/a, should invoke FunctionB asynchronously
      Type: AWS::Serverless::Function
      Properties:
        CodeUri: api/
        Handler: functions.a
        Runtime: python3.7
        Events:
          FunctionA:
            Type: Api
            Properties:
              Path: /functions/a
              Method: put
  
    FunctionB:
      # Long-running asynchronous function
      Type: AWS::Serverless::Function
      Properties:
        FunctionName: 'FunctionB'
        CodeUri: api/
        Handler: functions.b
        Runtime: python3.7
        EventInvokeConfig:
          MaximumRetryAttempts: 2
          DestinationConfig:
            OnSuccess:
              Type: SQS
            OnFailure:
              Type: SQS
  </code></pre>
  
  <p>And my Python lambda handler logic:</p>
  
  <pre class="lang-py prettyprint-override"><code># functions.py
  
  def a(event, context):
    boto3.client('lambda').invoke(
      FunctionName='FunctionB',
      InvocationType='Event',
      Payload='some_data'.encode('UTF-8')
    )
    return { "statusCode": 200, "body": {} }
  
  def b(data):
    print("SUCCESS!")
  </code></pre>
  
  <p>I deploy it locally:</p>
  
  <pre><code># deploy.sh
  sam build
  sam local start-api
  </code></pre>
  
  <p>All is well until this point. When I call <code>PUT /functions/a</code>, I get the following error indicating that Function B could not be invoked from Function A:</p>
  
  <pre><code>[ERROR] ResourceNotFoundException: An error occurred (ResourceNotFoundException) when calling the Invoke operation: Function not found: arn:aws:lambda:us-east-2:[iam-user-id]:function:FunctionB
  </code></pre>
  
  <p>Has anyone found a fix for this? Here's what I've tried:</p>
  
  <ol>
  <li>Verified that Function B can be invoked successfully via the command line:</li>
  </ol>
  
  <pre><code>sam local invoke FunctionB # works great
  </code></pre>
  
  <ol start="2">
  <li>Attempted to change the <code>InvocationType=Event</code> to <code>InvocationType=RequestResponse</code> and received the same error</li>
  <li>Instantiated the lambda client to reference the local URL</li>
  </ol>
  
  <pre class="lang-py prettyprint-override"><code>boto3.client('lambda', endpoint_url='http://localhost:3000')
  # [ERROR] EndpointConnectionError: Could not connect to the endpoint URL: "http://localhost:3000/2015-03-31/functions/ScheduleShowsAsyncFunction/invocations"
  </code></pre>