Docs / Guides

Tailscale on AWS Lambda

AWS Lambda is a popular method of deploying application using containers, rather than managing servers yourself. However, it can be difficult to use Tailscale on AWS Lambda, since it doesn’t provide a /dev/net/tun device that Tailscale needs.

In Tailscale v1.10 or greater, you can use Tailscale’s userspace networking mode to connect your AWS Lambda apps to your Tailscale network.

Step 1: Generate an auth key to authenticate your AWS Lambda containers

First, we’ll generate an auth key to allow AWS Lambda to authenticate our container to join our network.

Navigate to the auth keys page of the admin console. We recommend using an ephemeral key for this purpose, since it will automatically clean up devices after they shut down.

Tailscale's auth key generation page

Next, navigate to AWS Lambda’s Configuration tab and click on the Environment Variables item. From here click Edit, add a new environment variable, and name it TAILSCALE_AUTHKEY. Use the tskey-<key> string as the Value.

Step 2: Configure your Dockerfile to install Tailscale

Next, we’ll use a multistage Dockerfile, where the first stage builds your application, the second fetches and unpacks Tailscale binaries, and the final stage pulls application code and Tailscale into the final image to be uploaded to AWS.

In your Dockerfile:

FROM public.ecr.aws/lambda/provided:al2 as builder
WORKDIR /app
COPY . ./
# This is where one could build the application code as well.


FROM alpine:latest as tailscale
WORKDIR /app
COPY . ./
ENV TSFILE=tailscale_1.14.3_amd64.tgz
RUN wget https://pkgs.tailscale.com/stable/${TSFILE} && \
  tar xzf ${TSFILE} --strip-components=1
COPY . ./


FROM public.ecr.aws/lambda/provided:al2
# Copy binary to production image
COPY --from=builder /app/bootstrap /var/runtime/bootstrap
COPY --from=tailscale /app/tailscaled /var/runtime/tailscaled
COPY --from=tailscale /app/tailscale /var/runtime/tailscale
RUN mkdir -p /var/run && ln -s /tmp/tailscale /var/run/tailscale && \
    mkdir -p /var/cache && ln -s /tmp/tailscale /var/cache/tailscale && \
    mkdir -p /var/lib && ln -s /tmp/tailscale /var/lib/tailscale && \
    mkdir -p /var/task && ln -s /tmp/tailscale /var/task/tailscale

# Run on container startup.
ENTRYPOINT ["/var/runtime/bootstrap"]

The Dockerfile specifies /var/runtime/bootstrap as the initial process to run. This script needs to bring Tailscale up and then start the application binary. This is where we can use the TAILSCALE_AUTHKEY variable we defined earlier.

Then, create a file named bootstrap at the root of your app:

#!/bin/sh

mkdir -p /tmp/tailscale
/var/runtime/tailscaled --tun=userspace-networking --socks5-server=localhost:1055 &
until /var/runtime/tailscale up --authkey=${TAILSCALE_AUTHKEY} --hostname=aws-lambda-app
do
    sleep 0.1
done
echo Tailscale started
ALL_PROXY=socks5://localhost:1055/ /var/runtime/my-app

Done! The next time your AWS Lambda container deploys, it should be able to connect to your private Tailscale network.

We’d love to hear about the cool things you build. Mention @tailscale on Twitter or email us at [email protected].

Last updated