|
1 |
| -# aws-lambda-debugger |
2 |
| -Debugging support for AWS Lambda Node 6.10+ (no joke) |
| 1 | +# AWS Lambda Debugger |
| 2 | + |
| 3 | +Do you want to step through code running live in Lambda? Do you want to fix bugs faster? |
| 4 | +Do you want free pizza? |
| 5 | + |
| 6 | +This project will help you with the first 2 questions. When you show it to a friend, |
| 7 | +you might get that 3rd one too :) |
| 8 | + |
| 9 | +*This is only for the AWS Node 6.10 runtime* |
| 10 | + |
| 11 | +## Isn't this impossible? |
| 12 | + |
| 13 | +No. Well, not anymore. |
| 14 | + |
| 15 | +## How? |
| 16 | + |
| 17 | +Normally, debugging is a one hop process: developer's debugger connect directly to the process. This *is* impossible with Lambda. |
| 18 | + |
| 19 | +However, we fork your code to a separate child process that is |
| 20 | +running in debug mode and connected to the original via AN interprocess communication |
| 21 | +channel. The parent process opens 2 WebSockets as well: one to the child process' |
| 22 | +V8 Inspector and the other to a broker server, becoming a proxy between the 2 |
| 23 | +connections. Next, the developer connects a debugger to the broker server, which |
| 24 | +connects them to the proxy, which is connected to the child's debugger port. |
| 25 | +Now, you have a 3 hop chain like this: |
| 26 | + |
| 27 | +``` |
| 28 | +Debugger <=> Broker <=> Proxy <=> Child |
| 29 | +``` |
| 30 | + |
| 31 | +Once the developer's debugger connects to the broker server, the debug |
| 32 | +version of the handler is executed. The proxy and the child coordinate to |
| 33 | +shim the event, context, and callback. *Result:* the developer is connected |
| 34 | +to a live running Lambda function with full control. |
| 35 | + |
| 36 | +Oh, and you only have to add one line of code *at the end* of your handler file(s)... |
| 37 | + |
| 38 | +## I want one! |
| 39 | + |
| 40 | +Good. There are 5 steps: |
| 41 | + |
| 42 | +1. Deploy the broker server |
| 43 | +2. Add the proxy to your code |
| 44 | +3. Configure the proxy via environment variables |
| 45 | +4. Increase your Lambda timeout |
| 46 | +5. Use it! |
| 47 | + |
| 48 | +### Deploy the broker server |
| 49 | + |
| 50 | +- Kick off an EC2 Amazon Linux Instance |
| 51 | +- Attach Security Group |
| 52 | + - exposing ports `8080` and `9229` to `0.0.0.0/0` |
| 53 | + - expose port 22 to [YOUR IP](https://www.google.com/search?q=whats+my+ip) |
| 54 | +- SSH in to the box |
| 55 | + |
| 56 | +```bash |
| 57 | +# Install Docker |
| 58 | +sudo yum update -y |
| 59 | +sudo yum install -y docker |
| 60 | +sudo service docker start |
| 61 | + |
| 62 | +# Run the Broker |
| 63 | +docker run --name debug-broker \ |
| 64 | + -d -p 8080:8080 -p 9229:9229 \ |
| 65 | + trek10/aws-lambda-debugger-broker --restart always |
| 66 | + |
| 67 | +# To view logs |
| 68 | +docker logs debug-broker |
| 69 | +``` |
| 70 | + |
| 71 | +### Add the proxy to your code |
| 72 | + |
| 73 | +Add the package to your repo: |
| 74 | + |
| 75 | +```bash |
| 76 | +npm install aws-lambda-debugger --save |
| 77 | +``` |
| 78 | + |
| 79 | +Require the package at the very end of each file that contains a Lambda handler |
| 80 | +that you want to debug. Example: |
| 81 | + |
| 82 | +```javascript |
| 83 | +module.exports.myHandler = (event, context, callback) => { |
| 84 | + // put some code that you want to debug here |
| 85 | +} |
| 86 | + |
| 87 | +require('aws-lambda-debugger'); |
| 88 | +``` |
| 89 | + |
| 90 | +That's it!!! |
| 91 | + |
| 92 | +### Configure the proxy via environment variables |
| 93 | + |
| 94 | +There are 3 magic environment variables that need to be set: |
| 95 | + |
| 96 | +- `DEBUGGER_ACTIVE`: As long as this value is present and it is not 'false' |
| 97 | +or empty string, the proxy will do its job. |
| 98 | +- `DEBUGGER_BROKER_ADDRESS`: This is the IP address or domain for the broker server. |
| 99 | +- `DEBUGGER_FUNCTION_ID`: This is a unique ID (per function!) that is used to route the |
| 100 | +debugger to the appropriate function. It is used as part of the URL to connect. |
| 101 | + |
| 102 | +### Increase your Lambda timeout |
| 103 | + |
| 104 | +This is pretty straight forward. Alter the timeout to 300 seconds to allow |
| 105 | +maximum debug time. |
| 106 | + |
| 107 | +### Use it! |
| 108 | + |
| 109 | +1. Launch your Lambda function (from console, via CLI, etc) |
| 110 | +2. Replace the `DEBUGGER_BROKER_ADDRESS` and `DEBUGGER_FUNCTION_ID` in the following URL |
| 111 | +and paste it into Chrome. |
| 112 | +```chrome-devtools://devtools/remote/serve_file/@60cd6e859b9f557d2312f5bf532f6aec5f284980/inspector.html?experiments=true&v8only=true&ws=[DEBUGGER_BROKER_ADDRESS]:9229/[DEBUGGER_FUNCTION_ID]``` |
| 113 | +3. DEBUG!!! |
| 114 | + |
| 115 | +## What's the catch? |
| 116 | + |
| 117 | +There are a few catches/known issues: |
| 118 | + |
| 119 | +- Multiple `console.log` calls close together sometimes causes them to be |
| 120 | +aggregated in a single CloudWatch Log entry |
| 121 | +- This only works with the AWS Node 6.10 runtime. We expect it to work with |
| 122 | +Node 8 whenever AWS offers support for it too. |
| 123 | +- Chrome DevTools is the only debugger that we have proven to work. YMMV with |
| 124 | +your IDE. |
| 125 | +- You pay for your debug time. No way around this. It is a running Lambda |
| 126 | +function after all. |
| 127 | +- `context.callbackWaitsForEmptyEventLoop` is defaulted to `false`. You can |
| 128 | +change it back to `true` (we even shimmed that!), but your function *will* |
| 129 | +run until it times out if you do. |
| 130 | +- `context.getRemainingTimeInMillis` is technically an approximation. We |
| 131 | +grab the remaining time and the current timestamp when the debugger connects |
| 132 | +and ship both to the child. The time delta is then subtracted from the original |
| 133 | +time. Since all times are retrieved inside of the Lambda, this should be a |
| 134 | +*very close* approximation. |
| 135 | + |
| 136 | +## Anything else I should know? |
| 137 | + |
| 138 | +Functionally, this thing is complete. However it is still very new, |
| 139 | +so don't be surprised if something goes wrong. Feel free to file an issue. |
| 140 | +We're happy to take PRs too. |
| 141 | + |
| 142 | +## Items that need work still |
| 143 | + |
| 144 | +- Tests: There are no tests. Because of all of the dark arts, eventing stuff, |
| 145 | +and the sockets, we didn't want to delay release because the tests are going |
| 146 | +to be hard to write. If you're a ninja at this, feel free to reach out. |
| 147 | +- Improving logging in the broker server |
| 148 | +- Bulletproof socket cleanup: We're still trying to make sure that all sockets |
| 149 | +are cleaned up as fast as possible. If you find any leaks, please let us know. |
| 150 | +- Study memory usage in the broker. |
| 151 | + |
| 152 | +## Future ideas: |
| 153 | + |
| 154 | +- Make things more configurable |
| 155 | +- Web UI for broker server |
| 156 | + - Get direct link as soon as Lambda connects to broker |
| 157 | + - Track remaining time |
| 158 | + |
| 159 | +**Made with :gift_heart: and :sparkles:magic:sparkles: by [Rob Ribeiro](https://github.com/azurelogic) + [Trek10](https://www.trek10.com/)** |
| 160 | + |
| 161 | +P.S.: We do AWS consulting and monitoring. Come talk to us. |
| 162 | + |
| 163 | +Twitter: [Rob](https://twitter.com/azurelogic) + [Trek10](https://twitter.com/trek10inc) |
0 commit comments