DEV Community

Alonso Suarez for AWS Community Builders

Posted on • Originally published at mymakerspace.substack.com

This is THE simplest way to create a serverless HTTP App

  • 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

Top comments (3)

Collapse
 
kalkwst profile image
Kostas Kalafatis

Just a heads up that you can add highlighting to the code blocks if you'd like. Just change:

code block with no colors example

... to specify the language:

code block with colors example

More details in our editor guide!

Collapse
 
alonch profile image
Alonso Suarez

Thanks for the feedback ❤️

Collapse
 
der_gopher profile image
Info Comment hidden by post author - thread only accessible via permalink
Alex Pliutau

Great write-up, we have a bunch of articles on Github Actions in our Newsletter, check it out - packagemain.tech/p/github-actions-...

Some comments have been hidden by the post's author - find out more