Serve API in Lambda Containers

Hi guys! I’m the devops of my team and I’m trying to deploy a redwoodjs application, so far I’ve contenerized the aplication with separated api and web stages, it runs perfectly on kubernetes and docker but our client wants to run it on lambda to take advantage of the free tier.

I’ve been able to run the api as a container in lambda with the serverless framework:

FROM node:14-alpine as base

WORKDIR /app

ARG NODE_ENV

ENV NODE_ENV=$NODE_ENV

ENV RUNTIME_ENV=$RUNTIME_ENV

ARG RUNTIME_ENV

ARG DATABASE_URL

ENV DATABASE_URL=$DATABASE_URL

COPY api/package.json api/package.json

COPY web/package.json web/package.json

COPY package.json .

COPY yarn.lock .

COPY redwood.toml .

COPY graphql.config.js .

COPY .env.docker .

COPY .yarn .yarn

COPY .yarnrc.yml .

RUN apk add --update --no-cache openssl1.1-compat

RUN yarn install

FROM base as build

COPY api api

COPY scripts scripts

RUN yarn rw build api

RUN yarn rw generate types

FROM node:16.14-alpine

WORKDIR /app

# Copy

COPY --from=build /app/api/dist /app/api/dist

COPY --from=build /app/node_modules/.prisma /app/node_modules/.prisma

COPY --from=build /app/.redwood /app/.redwood

COPY api/db api/db

COPY api/server.config.js api/server.config.js

COPY api/package.json .

COPY graphql.config.js .

COPY redwood.toml .

COPY .env.docker .

RUN apk add --update --no-cache openssl

# Install dependencies

RUN yarn install

# Install additional dependencies

RUN yarn add @redwoodjs/api-server @redwoodjs/internal @redwoodjs/cli

RUN yarn cache clean

EXPOSE 8911

CMD []

ENTRYPOINT [ "yarn", "rw", "serve", "api"]

but neither function urls o api gateway sends the requests correctly to the server, instead it starts correctly and waits for a request until the lambda times out.

my serverless.yml looks like this:

service: xxxxx-api
frameworkVersion: '3'

provider:
  name: aws
  ecr:
    scanOnPush: false
    images:
      api:
        path: "../"
        file: "api/Dockerfile"
        cacheFrom:
          - api:latest

functions:
  api:
    image: api
    fileSystemConfig:
      localMountPath: /mnt/tmp
      arn: arn:aws:xxxxxxxxxxx
    vpc:
      securityGroupIds:
        - sg-xxxxxxxxxx
        - sg-xxxxxxxxxx
      subnetIds:
        - subnet-0ae3f5751add977e2
        - subnet-02791847f1e10b73e
    events:
        - httpApi: '*'

The logs in cloudwatch looks like this:

I’ve tried to configure the api gateway to route to /{proxy+} but that didin’t worked either

I’m pretty sure it may be related to the entrypoint I’m using, but I’m not sure how to solve it.

Do you guys have any thought about this? What did I miss? Thanks!

could it be related to this? App Configuration | RedwoodJS Docs

@dthyresson I read that you’re coordinating docker efforts, hopefully you can give me a hand. Thanks in advance!

Hey! Thank you for your response.
Unfortunately I’ve already tried this and still doesn’t work.
The function is accesible and you can reach it, the lambda will trigger but it only starts the server and wait for a request, instead of process the incoming request that trigger it.

@jeliasson Hey! How are you doing?
I’ve read your vlog about dockerize and I’ve followed it and it works perfectly, thanks for it!
Perhaps you know something about dockerize for lambda, maybe it has to be with the lambda runtime interface, but I don’t know how to integrate it with the “yarn rw serve api” command.
Sorry for the spam and if you can help I’d be truly grateful!

1 Like

Hi @mbarbosa
Sorry for not getting back to you until now. Unfortunately, I have not done any big leap into Serverless/Lambda so I can’t be much of help here.

I know that Redwood had a strong ambition to go Serverless (as part of the whole JAMstack setup) but I think that was proven to be harder than initially thought.

Did you manage to find a way forward on this, or should we reach out to the team and ask if they know of any actual Serverless deployments in the wild?