bensiu
bensiu

Reputation: 25584

Nuxt 3 in AWS Lambda with Serverless Framework

I try to deploy simple Nuxt 3 application to AWS Lambda for SSR. So far I have:

in nitro.config.ts

import { defineNitroConfig } from 'nitropack';

export default defineNitroConfig({
  preset: 'aws-lambda',
  serveStatic: true
});

in Lambda handler

import { handler } from './output/server/index.mjs';

export const runner = (event, context) => {
  const { statusCode, headers, body } = handler({ rawPath: '/' });

  return {
    statusCode,
    body,
    headers
  }
}

in serverless.yaml

functions:
  ssr:
    handler: handler.runner
    timeout: 15
    package:
      individually: true
      include:
        - output/**
    events:
      - httpApi:
          path: '*'
          method: '*'

I run yarn build, change .output folder name to output to be able to include it with package, but I still have errors like "errorMessage": "SyntaxError: Cannot use import statement outside a module".

Is someone has idea how it could be done?

Upvotes: 3

Views: 3940

Answers (1)

Francisco Ramos
Francisco Ramos

Reputation: 216

Its easier than that bro, nuxt already exports the handler nothing for us to do:

serverles.yml:

service: nuxt-app-lamba

provider:
  name: aws
  runtime: nodejs14.x
  stage: dev
  region: us-east-1

functions:
  nuxt:
    handler: .output/server/index.handler
    events:
      - httpApi: '*'

nuxt.config.ts:

// https://v3.nuxtjs.org/api/configuration/nuxt.config
export default defineNuxtConfig({
    nitro: {
        preset: 'aws-lambda',
        serveStatic: true
      }
})

package.json:

{
  "private": true,
  "scripts": {
    "build": "nuxt build",
    "build:lamba": "NITRO_PRESET=aws-lambda nuxt build",
    "dev": "nuxt dev",
    "generate": "nuxt generate",
    "preview": "nuxt preview",
    "postinstall": "nuxt prepare",
    "deploy": "sls deploy"
  },
  "devDependencies": {
    "nuxt": "3.0.0-rc.11"
  }
}

This example is not working anymore (2022.12.09):

Working example: https://8cyysz2g5f.execute-api.us-east-1.amazonaws.com/

Upvotes: 12

Related Questions