If you don't want to update your lambda function, just simulate APIGateway event object by boto3 client:
If your api looks like (ex: https://linktolambda/{id})https://linktolambda/123456
You will invoke with this code:
payload = { "pathParameters": { "id":"123456" } }
result = client.invoke(FunctionName=conf.lambda_function_name,
InvocationType='RequestResponse',
Payload=json.dumps(payload))
Or your API look like https://linktolambda?id=123456
payload = { "queryStringParameters": { "id":"123456" } }
result = client.invoke(FunctionName=conf.lambda_function_name,
InvocationType='RequestResponse',
Payload=json.dumps(payload))
lets say your is in invoke_lambda file. You want to use the patch annotation to mock the lambda_client, not the response. You can then set the my_lambda.py of the return_value to mocked_lambda_client.mocked_response
# my_lambda.py
def get_attachment(my_id):
payload = {"myId": my_id}
response = lambda_client.invoke(
FunctionName=os.environ["MY_LAMBDA"],
Payload=json.dumps(payload),
)
return response.status_code
# test_my_lambda.py
@mock.patch("my_lambda.lambda_client")
def test_duck_response_200(mock_lambda_client):
mocked_response = mock.Mock()
mocked_response.status_code = 200
mock_lambda_client.invoke.return_value = mocked_response
response = get_attachment('some_id')
assert response == 200
It appears that you are saying that Lambda function A is timing out when invoking Lambda function B.
A common scenario is that the timeout is caused by Lambda function A not having access to the Internet. The AWS API endpoint is on the Internet and Lambda function A requires Internet access to invoke Lambda function B.
To have Internet access, the Lambda function should either:
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