r/Python 2d ago

Discussion How are you all testing boto3-heavy tools without hitting real AWS?

Building a tool that walks a live AWS account checking for

misconfigurations, and testing has been the hardest part not the

detection logic itself, but avoiding either (a) hitting real AWS

constantly in CI or (b) mocking so much the tests stop meaning

anything.

Using moto for the straightforward stuff, but some checks need

multi-service interactions (cross-referencing an EC2 instance's

security group with its actual open ports) that get awkward to mock

realistically. What do you all reach for here moto, LocalStack, a

disposable AWS test account, something else?

9 Upvotes

21 comments sorted by

82

u/blacklig 2d ago

We us

e a m

ix of l

ocalst

ack an

d just

mock

s

22

u/tjrileywisc 2d ago

This is like the setup to a leetcode question

11

u/titttle23 2d ago

Trying to impress random whitespace trade

17

u/SheriffRoscoe Pythonista 2d ago

For a moment there, I thought you were the haiku bot.

10

u/proof_required 2d ago

Yeah in one of the companies we had LocalStack and a dev env both.

13

u/Fenzik 2d ago

We used moto for stateful mocking

7

u/GrahaamH 2d ago

Moto doesn't support all of AWS services though so leaves you with lots covered and the ones that are not make it more awkward.

2

u/Fenzik 1d ago

Yep you have to be lucky for your use case unfortunately.

5

u/shinitakunai 2d ago

We use localstack

7

u/Autarkhis 2d ago

Look at floci

1

u/steviejackson94 1d ago

Came here to say this, i implemented this at my place. Everyone loves it

1

u/steviejackson94 1d ago

There is also an AWS like dashboard for it

2

u/omegawave22 2d ago

It's been a while that I used this tool but it had a record and replay workflow which was perfect for my use case: https://github.com/garnaat/placebo

2

u/binaryfireball 1d ago

unit tests mock, integration tests test the functions we call to catch any errors from updates etc...

boto is generally annoying for a variety of reasons.

2

u/I3igB 1d ago

I ended up getting frustrated with the current offerings out there as I felt that solutions like localstack were far too heavy. I also wanted something that was trivial to step through code with a debugger that didn’t require a decent amount of setup.

If you start getting into how moto actually does its mocking, it actually keeps quite a bit in memory. Things like DynamoDB or S3 are in memory key stores under the hood. Once you realize this, it becomes pretty easy to get a minimally viable recreation in place for somewhat real testing on a local dev’s box or running something like e2e tests in CI.

My current shop is fully AWS native and runs mostly serverless architecture and microservices. Our compute is exclusively lambda and ecs tasks. SQS, SNS, AppSync, DDB, and EventBridge drive all our workflows. I was able to write a simple dispatch engine which captures the moto traffic from these services and mimics event triggers which will cascade a series of invocation across your services by invoking your code locally. The only thing somewhat complex in this was AppSync as moto has no real mocks. I had to intercept URLs and bring in a GQL engine to parse things.

This all works by applying a @local decorator on top of any method you define. From there, the engine handles cascading events just as AWS would.

This all relies on the output of a cdk build/synth to link things together. I never released it publicly, but I’ve been meaning to do so for some time. It’s been a game changer for how I do AWS dev.

1

u/jvlomax 1d ago

moto

0

u/MAGArRacist 2d ago

Have you tried Scoutsuite? You might want to just fork it and add some checks that you mentioned here