# @microbackend/plugin-serverless-aws

> Micro-backend AWS serverless plugin

Latest version **1.0.1** (published 2022-02-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install @microbackend/plugin-serverless-aws
pnpm add @microbackend/plugin-serverless-aws
yarn add @microbackend/plugin-serverless-aws
bun add @microbackend/plugin-serverless-aws
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2022-02-02 |
| First published | 2021-11-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 26.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Hai Pham |
| Maintainers | protoman92, haipham |

## Links

- npm: https://www.npmjs.com/package/@microbackend/plugin-serverless-aws
- npm.io page: https://npm.io/package/@microbackend/plugin-serverless-aws

## Dependencies (7)

- [fs-extra](https://npm.io/package/fs-extra.md) ^10.0.0
- [handlebars](https://npm.io/package/handlebars.md) ^4.7.7
- [serverless](https://npm.io/package/serverless.md) ^2.66.1
- [ts-essentials](https://npm.io/package/ts-essentials.md) ^9.0.0
- [serverless-http](https://npm.io/package/serverless-http.md) ^2.7.0
- [serverless-webpack](https://npm.io/package/serverless-webpack.md) ^5.6.0
- [@microbackend/build-utils](https://npm.io/package/@microbackend/build-utils.md) ^1.0.1

## Recent versions

- 1.0.1 (latest) — 2022-02-02
- 1.0.1-alpha.43 (canary) — 2022-01-15
- 1.0.1-alpha.42 — 2022-01-15
- 1.0.1-alpha.41 — 2022-01-15
- 1.0.1-alpha.31 — 2022-01-08
- 1.0.1-alpha.27 — 2022-01-08
- 1.0.1-alpha.26 — 2022-01-08
- 1.0.1-alpha.15 — 2021-12-20
- 1.0.1-alpha.0 — 2021-12-07
- 1.0.0 — 2021-12-07
- 1.0.1-alpha.135 — 2021-12-06
- 1.0.1-alpha.133 — 2021-12-06
- 1.0.1-alpha.131 — 2021-12-05
- 1.0.1-alpha.129 — 2021-12-05
- 1.0.1-alpha.126 — 2021-12-04
- … 11 more at https://npm.io/package/@microbackend/plugin-serverless-aws/versions

## README

# @microbackend/plugin-serverless-aws

Microbackend plugin to add support for the
[serverless framework](https://www.npmjs.com/package/serverless).

## Installation

```shell
npx microbackend plugin add @microbackend/plugin-serverless-aws
```

## Usage

### Serverful/Serverless switch

```typescript
import createPluginConfig from "@microbackend/plugin-core/build/webpack.build";

await createPluginConfig({
  pluginOptions: {
    "@microbackend/plugin-serverless-aws": {
      isEnabled: process.env["INFRASTRUCTURE"] === "serverless",
      serverfulConfig: {
        entry: path.resolve("src", "index.ts"),
        mode: "development",
        output: { path: path.join(__dirname, "build"), filename: "index.js" },
      },
      serverlessConfig: {},
    },
  },
});
```

This plugin allows running the application in both serverful and serverless
modes, depending on whether `isEnabled` is true or false. In the entry file,
we will need to make some modification to the server start code:

```typescript
import { createExpressApp } from "@microbackend/plugin-express";
import startServerfulApp from "@microbackend/plugin-serverless-aws";
import express from "express";
import { Server } from "http";

let server: Server;

export const handler = startServerfulApp({
  getApp: () => {
    return createExpressApp().then((app) => {
      app.get("/", (...[, res]) => {
        res.sendStatus(204);
      });

      app.use(((err, _req, res, _next) => {
        res.status(err.status || 500).json({ error: err.message });
      }) as express.ErrorRequestHandler);

      return app;
    });
  },
  startApp: async ({ app }) => {
    const port = process.env["PORT"] || 3001;

    server = app.listen(port, () => {
      console.log("Started example server on port", port);
    });
  },
});
```

The `startServerfulApp` function abstracts away the work needed to switch
between serverful and serverless. We can then add some scripts to our
`package.json`:

```json
{
  "scripts": {
    "build": "webpack",
    "build:sls": "cross-env INFRASTRUCTURE=serverless sls package",
    "deploy": "cross-env INFRASTRUCTURE=serverless sls deploy",
    "start": "webpack --watch",
    "start:sls": "cross-env INFRASTRUCTURE=serverless sls invoke local--function core"
  }
}
```

In the above example, the serverful/serverless switch is determined using the
`process.env["INFRASTRUCTURE"]` flag, but it is up to us how we want to
implement it.

### Configuration generation

Running:

```shell
npx microbackend gen
```

will generate a `serverless.yml` file in the current project directory, if
there is not yet an existing file. It serves as a base for you to add on, and
is not meant to be an one-size-fits-all configuration.

---
_Source: https://npm.io/package/@microbackend/plugin-serverless-aws · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
