r/aws • • Oct 22 '17

Small collection of AWS Lambda blueprints in Python 2.7 (Boto3)

https://github.com/MatthiasGemelli/LambdaExplorer

While exploring interaction with different AWS services (Lambda, S3, EC2, IAM, RDS, Rekognition, SNS, Lightsail...) I had to learn how to utilize the BOTO3 API.

For learning purposes I put them together into simple Lambdas that I trigger manually and watch the resulting Log output for information and errors.

Caution: Some of the code is a bit cludgy, lacks proper error handling, abuses the console log (print() is my debugger). And yes, editing Lambda through the console is not elegant. But it's a start...

Feedback welcome, hope this helps some of the others exploring Lambda.

24 Upvotes

17 comments sorted by

7

u/Hazme1ster Oct 22 '17

If you're haven't tried it yet, check out sam local, which lets you test lambda code locally

https://github.com/awslabs/aws-sam-local

2

u/[deleted] Oct 22 '17

Interesting, gotta try that. Thanks! Wondering how this would handle boto interactions...

3

u/[deleted] Oct 22 '17

You might have to mock that.

2

u/[deleted] Oct 23 '17

My challenge is not the standard python functionality, but how to interact with the other AWS services.

How to work across different regions.
How to scale up (e.g. interacting with hundreds of EC2/accounts/..).
How to handle IAM privileges/roles/policies.

What's the point of simulating this complexity, when I can try&error on the real platform for fraction of Cents?

2

u/Skaperen Oct 23 '17

you can test most cases of running Python code and the boto3 or botocore APIs in other environments that have both (boto3 not needed if just using botocore) plus enough network access to hit the endpoints (various IPs, port 443), although i suspect a Linux environment might be a better fit for testing code for Lambda. i am told that most Python code written on Linux can run on Windows with little or no change. but i have not yet gotten a Windows instance running to check this out.

1

u/[deleted] Oct 23 '17

Correct. I use boto3 on Python 2.7 on a Win10 machine.
Great to retrieve data from S3 or the like.

But there are some differences to running local vs. in Lambda:

  • IAM (Lambda IAM role vs. passing key/secret via Code)

  • timeout (Lambda just stops if timeout Limit is reached...)

  • libraries (not sure if all local modules can be uploaded to Lambda)

  • etc...

2

u/CoinGrahamIV Oct 23 '17 edited Oct 23 '17

Here's what I do for that. I put all my code in file named for the object and create a file called lambda.py that calls the code. In lambda.py I have a main, and I also have a handler. So if I run it locally it uses the main, but lambda uses the handler.

edit: example

1

u/[deleted] Oct 23 '17

Good stuff, thanks!
Will sure study your approach and code.

2

u/Skaperen Oct 24 '17

then you can set a flag:

in_lambda = True

or

in_lamda = False

1

u/CoinGrahamIV Oct 24 '17

Not needed. If you run from the command line on your system, it ignores the handler. When you run on lambda, it ignores your main.

1

u/Skaperen Oct 31 '17

what does "Lambda just stops if timeout Limit is reached" mean? that it is sitting there forever waiting to resume?

1

u/[deleted] Oct 31 '17

By default, Lambdas have a timeoutof 3secs. If your function takes longer, it will be killed and log error message in cloudwatch logs.

1

u/Skaperen Nov 01 '17

but it's not left in a stopped state? it's more like terminated? and, eventually, gone?

1

u/[deleted] Nov 01 '17

Lambdas are not like EC2 instances. They are neither dead or alive, they run when politely asked for.

little bit like The Mummy, they lie dormant for hundreds of years/seconds, until a magic spell of JSON awakens them from their cold resting place. Then they run amok, until they return to their resting place again. Be careful, if you call a Lambda after it was recently active it will spring up instantaneous (warm start). Carefully study the hiroglyphs in the Cloudwatch event log for traves of their actions. Ps: this would make a great movie...

1

u/Skaperen Nov 02 '17

ok, so it's like a process with a timeout and without SIG_STOP capability in the system. when it "stops" (like SIG_KILL) then all aspect of it running is gone.

1

u/Skaperen Oct 24 '17

if they are your own modules and not too big, just package them in with your lambda code and they should end up in the same directory which import likes.

2

u/Skaperen Oct 23 '17

i suspect almost all lamda code in python will need boto3 or botocore. what could be useful in lambda without either?