This is THE simplest way to create a serverless HTTP App

Alonso Suarez - Jul 3 - - Dev Community
  • Let’s provision a new HTTP server:
  • Create a new Github Repo
  • Add a workflow on push
# .github/workflows/on-push-main.yml
name: demo
on:
  push:
    branches:
      - main
jobs:
  deploy:
    environment:
      name: main
      url: ${{ steps.backend.outputs.url }}
    permissions: 
      id-token: write
    runs-on: ubuntu-latest
    steps:
      - name: Check out repo
        uses: actions/checkout@v4
      - uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-region: us-east-1
          role-to-assume: ${{ secrets.ROLE_ARN }}
          role-session-name: ${{ github.actor }}
      - uses: alonch/actions-aws-backend-setup@main
        with: 
          instance: sample
      - uses: alonch/actions-aws-http-node@main
        with:
          name: actions-aws-http-node-sample
          artifacts: src
          allow-public-access: true
          routes: |
            - entrypoint-file: plus
              entrypoint-function: handler
              path: "/plus/{z}"
              method: post
              parameters:
                path:
                - name: z
                  type: number
                query:
                - name: x
                  type: number
                body: 
                - name: y
                  type: number
Enter fullscreen mode Exit fullscreen mode
  • Create function handler
// src/plus.js
exports.handler = async function (parameters) {
  const {
    path: { z },
    query: { x },
    body: { y }
  } = parameters
  return {
    status: 200,
    body: {
      total: z+x+y
    }
  };
};
Enter fullscreen mode Exit fullscreen mode
  • Add AWS ROLE_ARN repo secret
  • Git push changes to Github
  • Navigate to Github Actions Tab, less than a minute later, the api-docs will be generated

Screenshot of Github Actions Tab

Screenshot of Swagger

But wait there is more, clients sending invalid payload will get 400 status code with the following payload:

$ curl -X 'POST' \
  'https://riga7yssypqgxndyz3xigmzsxy0jkhip.lambda-url.us-east-1.on.aws/plus/2?x=1' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{ }'  # <-- Invalid body  

{"in":["request","body-params"],"coercion":"malli","type":"request-coercion","value":{},"humanized":{"y":["missing required key"]}}
Enter fullscreen mode Exit fullscreen mode

Code available on this sample repo and the full documentation here

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .