DEV Community

DarkEdges
DarkEdges

Posted on

Protecting an API with AWS API Gateway and Auth0

You have an API that you would like to protect with an OAuth Access Token from Auth0 using AWS API Gateway. Sounds simple enough, so lets do this using a local development environment thats hosts

  • API - Deployed as a simple Go API Services using AWS Fargate
  • API Gateway - Deployed using API Gateway V2

Now this normally would require creating an AWS Account, but we are going to be using LocalStack which provide those services (and more) in a container that can be deployed locally. This way we can learn AWS without the costs (its not much anyway) by deploying the services via Terraform.

Pre-requisites

We are assuming that you have the following deployed locally

  • Docker
  • Terraform
  • Visual Studio Code
  • Git

You will also need to sign up at https://app.localstack.cloud/sign-up to get an API Key that we need to run this stack.

Signup and New Instance

The following outlines the basic steps to get a localstack instance registered

Signup

  1. Open https://app.localstack.cloud/sign-up
  2. Provide an email address and click Continue.
  3. Provide a password and click Continue.
  4. Provide your details and click Start Trial.
  5. Check your email and follow the instrucions provided to Activate Account
  6. When completed login using the details your provided earlier and click Continue.

Get Personal Auth Token

  1. Go to https://app.localstack.cloud/settings/auth-tokens
  2. Under Personal Auth Token click the Copy icon.

Deploying LocalStack

Create a file docker-compose.yaml with the following and replace <copy PAT here> with your PAT you copied earlier.

services:
  localstack:
    container_name: "lsa-localstack"
    image: localstack/localstack-pro:latest
    environment:
      LOCALSTACK_AUTH_TOKEN: <copy PAT here>
    ports:
      - "127.0.0.1:4566:4566"
      - "127.0.0.1:4510-4559:4510-4559"
      - "127.0.0.1:443:443"
    volumes:
      - "localstack:/var/lib/localstack:rw"
      - "/var/run/docker.sock:/var/run/docker.sock"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:4566/_localstack/health"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 15s
    networks:
      localstack:
        aliases:
          - localstack
volumes:
  localstack:
networks:
  localstack:
Enter fullscreen mode Exit fullscreen mode

and deploy it using docker compose up -d

You can confirm it is working by going to https://app.localstack.cloud/inst/default/status and the services should be marked as available

CLI

You can install a version of the AWS CLI that works with localstack by reviewing https://docs.localstack.cloud/aws/integrations/aws-native-tools/aws-cli/

That concludes this part. Next we configure Terraform and deploy our API Service into AWS Fargate.

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.