Skip to content

Commit ebf9dac

Browse files
authored
feat: add warmer lambda function (#6)
1 parent 7a6d60d commit ebf9dac

File tree

11 files changed

+1870
-170
lines changed

11 files changed

+1870
-170
lines changed

.projen/deps.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.projen/tasks.json

Lines changed: 31 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.projenrc.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const project = new awscdk.AwsCdkConstructLibrary({
3636
peerDeps: ['tailscale-lambda-extension'],
3737
// description: undefined, /* The description is just a string that helps people understand the purpose of the package. */
3838
devDeps: ['husky'], /* Build dependencies for this module. */
39-
bundledDeps: ['@types/aws-lambda', '@aws-lambda-powertools/metrics', 'socks-proxy-agent'],
39+
bundledDeps: ['@types/aws-lambda', '@aws-lambda-powertools/metrics', 'socks-proxy-agent', '@aws-sdk/client-lambda'],
4040
publishToPypi: {
4141
distName: 'tailscale_lambda_proxy',
4242
module: 'tailscale_lambda_proxy',
@@ -56,10 +56,18 @@ project.gitignore.addPatterns('*.DS_Store');
5656

5757
project.bundler.addBundle('./src/lambda/tailscale-proxy/', {
5858
platform: 'node',
59-
target: 'node20',
59+
target: 'node22',
6060
sourcemap: true,
6161
});
6262
project.postCompileTask.exec('cp assets/lambda/tailscale-proxy/index.js lib/lambda/tailscale-proxy/index.js');
6363
project.postCompileTask.exec('cp assets/lambda/tailscale-proxy/index.js lib/lambda/tailscale-proxy/index.js.map');
6464

65+
project.bundler.addBundle('./src/lambda/proxy-warmer/', {
66+
platform: 'node',
67+
target: 'node22',
68+
sourcemap: true,
69+
});
70+
project.postCompileTask.exec('cp assets/lambda/proxy-warmer/index.js lib/lambda/proxy-warmer/index.js');
71+
project.postCompileTask.exec('cp assets/lambda/proxy-warmer/index.js lib/lambda/proxy-warmer/index.js.map');
72+
6573
project.synth();

API.md

Lines changed: 74 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ export class MyStack extends cdk.Stack {
8686
// }
8787
// },
8888
// debug: true, // Enable debug logging, show request + response, search for lines starting with "[tailscale-"
89+
// warmer: { // Optional, enables a "warmer" Lambda to keep proxy connections open/warm (cold start is about 5-10 seconds)
90+
// functionName: name("tailscale-proxy-warmer"),
91+
// concurrentInvocations: 2,
92+
// }
8993
});
9094

9195
const caller = new NodejsFunction(this, "tailscale-caller", {
@@ -182,6 +186,16 @@ A Proxy Lambda error can be identified by the following headers in the response:
182186
- `ts-error-name`: The error name.
183187
- `ts-error-message`: The error message.
184188

189+
### Reducing Cold starts
190+
191+
Cold starts occur when your function first has to connect to the Tailscale network, which can take between 5-10 seconds
192+
as mentioned in the [Tailscale Lambda Extension](https://github.com/rehanvdm/tailscale-lambda-extension?tab=readme-ov-file#limitations)
193+
docs. Subsequent requests are much faster as the connection is reused.
194+
195+
Cold starts can be mitigated by configuring a "warmer" Lambda function that periodically invokes the Proxy Lambda. The
196+
number of warm connections is determined by the `concurrentInvocations` property. This setup helps maintain a pool of
197+
warm connections to the Tailscale network, reducing latency for subsequent requests.
198+
185199
### Code Examples
186200

187201
Refer to the [tailscale-lambda-proxy-example](https://github.com/rehanvdm/tailscale-lambda-proxy-example) repository

npm-link-manual.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ npm run default
44
npm run bundle
55
npm run compile
66
cp assets/lambda/tailscale-proxy/index.js lib/lambda/tailscale-proxy/index.js
7+
cp assets/lambda/proxy-warmer/index.js lib/lambda/proxy-warmer/index.js
8+
79

810

911
# For this proxy package to be used in the -caller project

0 commit comments

Comments
 (0)