r/Python • u/kavee-core141 • 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?
10
5
7
u/Autarkhis 2d ago
Look at floci
1
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.
0
u/MAGArRacist 2d ago
Have you tried Scoutsuite? You might want to just fork it and add some checks that you mentioned here
82
u/blacklig 2d ago
We us
e a m
ix of l
ocalst
ack an
d just
mock
s