> For the complete documentation index, see [llms.txt](https://tutorial.specian.co.uk/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tutorial.specian.co.uk/devops/aws/serverless.md).

# Serverless

We are going to deploy the API using the [Serverless Framework](https://serverless.com). The [documentation](https://serverless.com/framework/docs/) is excellent and covers everything we need. If you are not familiar with Serverless, try the [hello work](https://serverless.com/framework/docs/providers/aws/examples/hello-world/node/) example and the [user guide](https://serverless.com/framework/docs/providers/aws/guide/quick-start/).

In our setup, we will use 3 serverless plugins:

* [Typescript](https://github.com/prisma-labs/serverless-plugin-typescript) to transpile our code Javascript.
* [Offline](https://github.com/dherault/serverless-offline) to run the app locally. (Note: add `.build` to `.gitignore`)
* [SSM](https://github.com/janders223/serverless-offline-ssm#readme) to read variables from our `.env`
* [Finch](https://github.com/fernando-mc/serverless-finch) to deploy the static files to S3.

### Using Express with Serverless

We also need to change our `index.ts` file to use [aws-serverless-express](https://github.com/awslabs/aws-serverless-express).

```typescript
exports.handler = (event: any, context: any) => {
  bootstrap().then(() => {
    const { app } = createApp();
    const server = awsServerlessExpress.createServer(app);
    awsServerlessExpress.proxy(server, event, context);
  });
}
```

### Common problems

`Error: Cannot find module './migrations'`

Add `import './migration.ts'` to `src/inddex.ts` and create `` `migrations/index.ts` ``importing all migration files.

`handler 'server' returned a promise and also uses a callback!`

You can't use `async` with the main Serverless file. Use `.then` for the promises instead.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://tutorial.specian.co.uk/devops/aws/serverless.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
