Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke an AWS lambda across regions

I have three lambda functions: boss, worker1, worker2. When using boto3.client.invoke I am able to call worker1 from boss. These two are in the same region.
worker2 is in a separate region. When attempting to call worker2 from boss the following error returns:
"An error occurred (ResourceNotFoundException) when calling the Invoke operation: Functions from 'us-east-1' are not reachable in this region ('us-west-2')" . boss has an execution role with the following permission:

{
"Version": "2012-10-17",
"Statement": [
    {
        "Action": [
            "lambda:InvokeFunction"
        ],
        "Effect": "Allow",
        "Resource": "arn:aws:lambda:*:*:*"
    }
]
}

Please help clarify how permissions need to be conveyed for this to work. Thanks

Edit: master and worker1 are in us-west-2 and worker1 is in us-east-1.
Here is the code used to invoke worker from master:

def lambda_handler(event, context):
function_name = "arn:aws:lambda:us-east-1-...:function:worker_2"
lambda_client = boto3.client('lambda')
payload = json.dumps({"body-json": "payload string")
response = lambda_client.invoke(
    FunctionName = function_name,
    Payload = payload
)
response_payload = response['Payload'].read()
response_arr = json.loads(response_payload)
return response_arr['answer']
like image 833
Sawyer Merchant Avatar asked Jan 14 '18 14:01

Sawyer Merchant


People also ask

Can I call Lambda from another region?

Yes, just be sure you set the correct region on the lambda client and have proper invoke permissions on the iam role.

Can Lambda work cross region?

Can I use lambda across regions? AWS Lambda is a regional service. A single Lambda function in a single region can make API calls to AWS services in other regions, but they're remote, of course, so any data transferred between that Lambda function and the destination services or vice-versa takes longer and costs more.

Are AWS Lambda functions region specific?

When working with AWS Lambda functions, the question of region is one of the first you need to answer. As each Lambda function lives in a specific AWS region, and each AWS region has a slightly different set of functionality, you may find yourself having to work with functions in multiple regions on a regular basis.

Can S3 trigger Lambda in different region?

S3 event can't trigger a lambda in a different region. What you can do is for example: Send the S3 event to SNS topic or SQS queue and trigger lambda in your target region from that message. Trigger Lambda in the same region from the S3 event and trigger your target region Lambda from the first Lambda.


1 Answers

Thank you everyone for the input. @Michael-sqlbot's comment about the AWS client library defaulting to sending requests to the local region is what helped me find the solution. For Python, the library is boto3. Having read the docs it was not clear how to set the region. It was this blog post that provided the (simple) answer:

client = boto3.client('lambda', region_name='us-west-2')

You are right Michael that the use case for one lambda to another between regions is convoluted. I'll leave this answer here in case any others who are new to boto3 encounter the same error when trying to get other resources (lambda to ec2, lambda to s3, etc) to work across regions.
Thanks

like image 154
Sawyer Merchant Avatar answered Sep 20 '22 17:09

Sawyer Merchant