Code Monkey home page Code Monkey logo

aws-lambda-dogs's Introduction

AWS Lambda Dogs REST API โ€” Formidable, We build the modern web

Maintenance Status

A simple REST API for our beloved formidadogs using json-server and serverless.

Contents

Usage

The infrastructure, deployment, and operational parts of this project are identical to aws-lambda-serverless-reference. Follow the entire guide there.

Note: When packaging/deploying the serverless application you'll not only need the normal STAGE, etc. environment variables in that guide but will also need a API_KEY_SECRET value.

Development

Run in memory with persistence to local disk.

# nodemon (`.db-localdev-lambda.json`): http://localhost:3000
$ yarn node:localdev

# serverless-offline (`.db-localdev-node.json`): http://localhost:3001/localdev/
# We use a temp value for API_KEY_SECRET (can pick anything).
$ API_KEY_SECRET=localdev yarn lambda:localdev

API

Authentication

To prevent miscreants entering mean information about our doggos, we require an API key header to mutate the underlying mutable, persistent data. Without this key, only GET requests are supported against the original unchanged dogs data.

To switch to "read-write + persistent" mode, add this header to all requests x-dogs-api-key: <key>. For Formidables, the key for each environment (sandbox, development, staging, production) is stored in our 1Password IC vault under the entry aws-lambda-dogs keys.

A new key can be generated by running the command openssl rand -hex 16.

The key needs to be provided during deployment in the API_KEY_SECRET environment variable.

$ STAGE=sandbox \
  API_KEY_SECRET=<sandbox key> \
  aws-vault exec FIRST.LAST-developer --no-session -- \
  yarn lambda:deploy

Examples

Localdev

These examples use node:localdev at http://localhost:3000 but you can adjust them easily for lambda:localdev or the real API at yarn lambda:info:

# All the doggos
$ curl "http://localhost:3000/dogs"

# 5 doggos
$ curl "http://localhost:3000/dogs?_start=0&_end=4"

# Just Rusty by ID
$ curl "http://localhost:3000/dogs/cLnG8C2d_"

# Searches for Rusty
$ curl "http://localhost:3000/dogs?name=Rusty"
$ curl "http://localhost:3000/dogs?q=rust"

# Update Rusty's name in read-write datastore.
$ curl -X PATCH "http://localhost:3000/dogs/cLnG8C2d_" \
  --data name="Rustinus B. Rutherford" \
  --header "x-dogs-api-key: <key>"

$ curl "http://localhost:3000/dogs/cLnG8C2d_" \
  --header "x-dogs-api-key: <key>"

# Reset the read-write datastore back to original state.
$ curl -X POST "http://localhost:3000/reset" \
  --header "x-dogs-api-key: <key>"

Staging

Some sample Lambda workflows (for sandbox environment, which may get nuked/recreated at any time):

# First, check the Sandbox info (currently `30lxcuxu8d`)
$ STAGE=sandbox \
  aws-vault exec FIRST.LAST-developer --no-session -- \
  yarn lambda:info

# All the doggos
$ curl "https://30lxcuxu8d.execute-api.us-east-1.amazonaws.com/sandbox/dogs"

# Just Rusty by ID
$ curl "https://30lxcuxu8d.execute-api.us-east-1.amazonaws.com/sandbox/dogs/cLnG8C2d_"

# Update Rusty's name in read-write datastore.
$ curl -X PATCH "https://30lxcuxu8d.execute-api.us-east-1.amazonaws.com/sandbox/dogs/cLnG8C2d_" \
  --data name="Rustinus B. Rutherford" \
  --header "x-dogs-api-key: <key>"

$ curl "https://30lxcuxu8d.execute-api.us-east-1.amazonaws.com/sandbox/dogs/cLnG8C2d_" \
  --header "x-dogs-api-key: <key>"

# Reset the read-write datastore back to original state.
$ curl -X POST "https://30lxcuxu8d.execute-api.us-east-1.amazonaws.com/sandbox/reset" \
  --header "x-dogs-api-key: <key>"

Production

For production, we have a custom mapping of https://HASH.execute-api.us-east-1.amazonaws.com/production/ to https://dogs.formidable.dev/.

# All the doggos
$ curl "https://dogs.formidable.dev/dogs"

# Just Rusty by ID
$ curl "https://dogs.formidable.dev/dogs/cLnG8C2d_"

# Update Rusty's name in read-write datastore.
$ curl -X PATCH "https://dogs.formidable.dev/dogs/cLnG8C2d_" \
  --data name="Rustinus B. Rutherford" \
  --header "x-dogs-api-key: <key>"

$ curl "https://dogs.formidable.dev/dogs/cLnG8C2d_" \
  --header "x-dogs-api-key: <key>"

# Reset the read-write datastore back to original state.
$ curl -X POST "https://dogs.formidable.dev/reset" \
  --header "x-dogs-api-key: <key>"

Also note to deploy, you will need a privileged user. Please talk to the ops team to make sure you've got the right credentials.

Maintenance Status

Active: Formidable is actively working on this project, and we expect to continue for work for the foreseeable future. Bug reports, feature requests and pull requests are welcome.

aws-lambda-dogs's People

Contributors

cpresler avatar dependabot[bot] avatar jpdriver avatar keithcom avatar paulmarsicloud avatar ryan-roemer avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

aws-lambda-dogs's Issues

BUG: query fails when passing a valid API key

In the sandbox env, the following query succeeds:
curl -X GET "https://30lxcuxu8d.execute-api.us-east-1.amazonaws.com/sandbox/dogs/isl15_ks84"

But it returns an empty object if you pass a valid API key value in the headers:
curl -X GET "https://30lxcuxu8d.execute-api.us-east-1.amazonaws.com/sandbox/dogs/isl15_ks84" --header "x-dogs-api-key: <sandbox key from 1Password>"

Bug: DB reset doesn't work.

Reproduction:

# Update Rusty's name in read-write datastore.
$ curl -X PATCH "https://30lxcuxu8d.execute-api.us-east-1.amazonaws.com/sandbox/dogs/cLnG8C2d_" \
  --data name="Rustinus B. Rutherford" \
  --header "x-dogs-api-key: <key>"

$ curl "https://30lxcuxu8d.execute-api.us-east-1.amazonaws.com/sandbox/dogs/cLnG8C2d_" \
  --header "x-dogs-api-key: <key>"

# EXPECTED/ACTUAL: Name is Rustinus B. Rutherford

# Reset the read-write datastore back to original state.
$ curl -X POST "https://30lxcuxu8d.execute-api.us-east-1.amazonaws.com/sandbox/reset" \
  --header "x-dogs-api-key: <key>"

$ curl "https://30lxcuxu8d.execute-api.us-east-1.amazonaws.com/sandbox/dogs/cLnG8C2d_" \
  --header "x-dogs-api-key: <key>"

# EXPECTED: Name is back to Rusty
# ACTUAL: Name is still Rustinus B. Rutherford

Infra: Make package.json scripts work on windows.

We presently have things like eval (yarn -s env) to place environment variables for usage in downstream commands. This won't work on windows.

Tasks:

  • Come up with windows friendly package.json scripts
  • With overridable environment variables (e.g., STAGE=sandbox)
  • That also has default values (e.g. ${AWS_REGION:-us-east-1})

Some ideas:

  • cross-env (but doesn't support default values)
  • builder (but kind of a pain since env support is all big json strings and you have to invoke builder CLI
  • A custom Node.js script to provide defaults and take in options easily in whatever format we want.

Updates

  • Package update and deploy to prod
  • Update README with formidable.dev links

Infra: Remove hard-coded API key and take from environment.

  • Remove hard coded key from here https://github.com/FormidableLabs/aws-lambda-dogs/blob/master/src/server/index.js#L28
  • Provide new instructions to generate a new key in README
  • Add key to 1password for each existing stage.
  • Add support for getting the api key via probably environment variable all the way to lambda (probably via serverless custom config and env vars).
  • Fail the deploy if key is not present.
  • Add instructions to README of correctly getting a key from 1password and providing the key during deploys

Infra: add a domain name

  • Something like {STAGE}.dogs.formidable.dev
  • Maybe reserve dogs.formidable.dev for STAGE = production

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.