moto icon indicating copy to clipboard operation
moto copied to clipboard

What am I missing with this Lambda Test?

Open chadmiracle opened this issue 1 year ago • 6 comments

In the testing script, I have @mock_aws decorator and nothing returns. I remove the decorator and it works fine. At this point, I'm steering for my team and I to use AWS Powertools Validation versus Moto Mock AWS.

`class lamb: def init(self) -> None: self.profile = "test-profile" self.region = "us-east-1" self.session = boto3.Session(profile_name=self.profile,region_name=self.region) self.lambda_client = self.session.client('lambda')

def get_all(self):
    print(f"Getting All Lambda Functions - Profile: {self.profile}")

    all_functions = self.lambda_client.list_functions()`

chadmiracle avatar Feb 20 '24 17:02 chadmiracle

Hi @chadmiracle! Moto does not contain any data on startup (bar some IAM roles), so you would have to create functions (with a regular boto3-call) first. Note that we try to have the same validation where possible, so for example the IAM execution role that you use in the create_function-call needs to exist first.

bblommers avatar Feb 20 '24 17:02 bblommers

Just to list functions? Same "config" (which is none) works for Dynamo. If I'm going to go to all of that work, then why do I need Mock_AWS/Moto?

chadmiracle avatar Feb 20 '24 17:02 chadmiracle

And I'm calling these functions from another script that I didn't provide. With the appropriate decorator. It's just not working.

chadmiracle avatar Feb 20 '24 17:02 chadmiracle

I hadn't looked into AWS Powertools Validation before, but that is a very different beast. From what I can tell, it mostly focuses on JSON schema validation, so it's not a replacement/comparable with Moto at all.

Moto is mock/emulation tool. It will intercept all your calls to AWS and return a mock response, allowing you to validate that the business logic around the boto3 requests is correct without actually reaching out to AWS.

why do I need Mock_AWS/Moto?

It will save you a lot of time and money. If you have hundreds of unit tests that call AWS at some point, all these HTTP requests (and waiting for asyncronous operations like DynamoDB.create_table) increase the test runtime massively. With Moto intercepting the AWS requests, you don't have to wait for that. And it's obviously free, so you can verify/test expensive calls like ec2.create_instances() as part of a unit test, without worrying about bills.

It's just not working.

I can feel the frustration - so I hope this explanation helps you figure out what's going wrong. If any of the code that you're working on is open source, I'd be happy to have a look.

bblommers avatar Feb 20 '24 19:02 bblommers

I'll keep trying to use it but this stuff isn't documented well. I understand that invoking Lambda's require docker? But I don't see that documented? I'm tired of being frustrated with this stuff and just expect it to work.

chadmiracle avatar Feb 20 '24 20:02 chadmiracle

I'm assuming you've gone through the documentation here? http://docs.getmoto.org/en/latest/docs/getting_started.html

The Lambda-specific documentation can be found here: http://docs.getmoto.org/en/latest/docs/services/lambda.html

(Note that the reference to @mock_lambda_simple is outdated - that should say @mock_aws(config={"lambda": {"use_docker": False}}), as documented here: http://docs.getmoto.org/en/latest/docs/configuration/index.html. I'll update that now.)

Feedback on how to improve the docs is always welcome.

bblommers avatar Feb 20 '24 20:02 bblommers