Hi all, I’ve been setting up an API with API Gateway and Lambda. I have a few questions about best practices and security:
I’ve noticed that logs from my lambda aren’t showing up in CloudWatch when the lambda is invoked from API Gateway. However, logs are showing up if I manually invoke with the Test button. Do I need to grant API Gateway logging permissions somehow?
Is there any way to direct lambda logs into an s3 bucket instead (without manual exporting)?
Let’s say that someone managed to get RCE on my lambda. In that case, would their access be limited to the permissions I’ve granted the lambda execution environment (e.g with the role I assigned)?
I’ve been learning nodejs as I go along (I’m more familiar with ruby)… Is there any way to get a sanity check of my code to make sure it meets best practices?
I noticed that API Gateway asks for a regex to determine the response code. This seems a little clunky to me (e.g matching regexes against JSON). Is there a better way?
What’s the best practice for exception handling? Is there any way to use something like bugsnag with lambda?
Thanks in advance. API Gateway seems really cool, very excited to learn it.
Here’s a few answers to your questions, hope it helps:
I haven’t had a chance to use API GW in anger yet, so can’t say either way (although what you’re saying makes sense).
Unfortunately there doesn’t seem to be an out-of-the-box way to do this. You could probably do it with a(nother) Lambda function, but that might be getting a bit meta…
Correct. Ideally your Lambda function’s IAM Role will have the most limited policies it needs to run.
This is “how long is a piece of string” thing, no matter what language you use. In the case of Node, the main thing is don’t block the event loop (especially since you’ll pay for it - literally - with Lambda) - it’s “just” Javascript.
See point 1.
Definitely use context.fail(Error error); to “bubble up” any errors in to your CloudWatch logs. From there you could create alarm actions to Do Stuff.
The IAM role that you give Lambda does need CloudWatch permissions (put logs, create log groups, create log streams). When you test it on the Lambda page, they display it right there, but for other invocations, it needs those IAM permissions.
Not directly, but once the logs show up in CloudWatch, I believe you can click on the group and then stream them to Kinesis. From there, you can copy them to S3 I think. If not, you can setup another Lambda function to poll the group, download the logs and copy them to S3. A bit roundabout, but that’s all we get for now.
Yes, from what I understand, the container hosting the function assumes the IAM role just like EC2. So if someone managed to get control of it, they’d have those same permissions.
Nothing specific, but definitely make sure that you always exit the function using the context.succeed or context.fail methods. With Node’s callbacks, it’s very easy to miss these and then you’ll pay for the full execution time until it gets killed for timeout.
This is something I hate. You can’t regex the JSON. So for now (AWS has this on their radar according to some forum posts), I just fail with a string. You can then use an API Gateway mapping to send back actual JSON.
This I’m not too sure of. Pretty much anything that runs on Node will run in Lambda, so if you have a module you like, that should work. One thing to keep in mind though is that if you miss an error, Lambda and the API Gateway will send the whole stack trace back. Kind of a bad thing to send to users.
Good luck with the service! I’m a big fan, but it definitely needs some more work from AWS. It’s so difficult to setup hundreds of routes since every one needs to be configured individually.
Also, shameless plug, but if you’re interested in Lambda, I wrote a whole eBook about it. You can pre order it now, but it’ll be out on January 11th.
One other question - do you know if there’s a best practice for version control on lambdas? Is it to just have a separate repo (e.g. on github) and manually keep them in sync?
You can get (all) the alarm states from CloudWatch with the SDK.
If you wanted to take some specific action when it goes off, a CloudWatch Alarm sends a message to an SNS Topic, which can be a trigger event for a Lambda function. The event that the function is called with will have a bunch of info about the alarm.
There’s no “best practices” that I have found yet, but I’m leaning towards a repo per function.
While this might mean more overhead, I think it’ll be worth it because you want to ensure you functions are fully decoupled from any of your other code e.g. managing dependencies with multiple functions in one repo/package.json would get messy, etc.