Skip to main content
All CollectionsConnectUsage Events
Enable Usage Logs on Amazon Gateway
Enable Usage Logs on Amazon Gateway
Updated over 3 months ago

Connecting your Amazon API Gateway to Apiable

This tutorial will guide you through the process of setting up the S3 bucket, and Firehose Stream using AWS CLI and AWS CDK. We’ll also cover how to set up the necessary tools, including Cygwin on Windows and Node.js (npm) using NVM. Let’s get started!

Precondition Setup

⚠️ The precondition setup is optional. If you have a Terminal or Cygwin, Node.js, AWS CLI and CDK installed, you can skip this part.

1. Install Cygwin on Windows

Cygwin allows you to run a Unix-like environment on Windows. Here's how to install it:

  1. Go to the Cygwin website.

  2. Click Download and run the setup file.

  3. During installation, ensure you select the following packages:

    • curl

    • git

    • openssh

  4. Complete the installation and open the Cygwin terminal.

2. Install Node.js and npm using NVM

For Mac/Unix Systems:

  1. Open the Terminal app.

  2. Install Homebrew if not already installed:

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  3. Use Homebrew to install NVM:

    brew install nvm
  4. Create an NVM directory and update your shell configuration file:

    mkdir ~/.nvm echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.zshrc echo '[ -s "/usr/local/opt/nvm/nvm.sh" ] && \. "/usr/local/opt/nvm/nvm.sh"' >> ~/.zshrc source ~/.zshrc
  5. Install Node.js and npm using NVM:

    nvm install node

For Windows:

  1. Open Cygwin.

  2. Install NVM by running:

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
  3. Update your Cygwin shell config file (e.g., ~/.bashrc) and add the following:

    export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
  4. Reload the shell config file:

    source ~/.bashrc
  5. Install Node.js and npm using NVM:

    nvm install node

3. Install AWS CLI

For Mac/Unix Systems:

  1. In the terminal, run:

    curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg" sudo installer -pkg AWSCLIV2.pkg -target /
  2. Verify the installation:

    aws --version

For Windows:

  1. In the Cygwin terminal, run:

    curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" unzip awscliv2.zip sudo ./aws/install
  2. Verify the installation:

    aws --version

4. Install AWS CDK

  1. In the terminal (Mac or Cygwin), run the following to install the AWS CDK:

    npm install -g aws-cdk
  2. Verify the installation:

    cdk --version

5. Set AWS Key/Secret

  1. Create the .aws directory and config files:

    mkdir -p ~/.aws touch ~/.aws/credentials
  2. Open the credentials file in a text editor and add your AWS Access Key and Secret Key:

    [default] aws_access_key_id = YOUR_AWS_ACCESS_KEY_ID aws_secret_access_key = YOUR_AWS_SECRET_ACCESS_KEY
  3. Save and close the file.

6. Run cdk bootstrap

In the terminal, run:

cdk bootstrap

This prepares your AWS account to use AWS CDK.

7. Clone the APIable CDK Repository

  1. Open the terminal (Mac or Cygwin).

  2. Run the following command to clone the repository:

    git clone https://github.com/apiable/cdk cd cdk

9. Deploy the S3 Logs Bucket

  1. Run the following command to install the S3 bucket where logs will be collected:

    ./deploy-apiable-logs-bucket.sh $AWS_ACCOUNT_ID $AWS_REGION $STACKNAME

    Example:

    ./deploy-logs-bucket.sh 034444869755 eu-central-1 dev

Copy & Paste the outputs of the run into some editor, which will look similar to this

Please provide this information to Apiable support in order to complete the integration:
Region (e.g. eu-central-1)
S3 bucket name (e.g apiable-logs-dev )
ARN (e.g. arn:aws:iam...)

Send to support@apiable.io

10. Deploy the Firehose Stream

  1. Take note of the ARN from the S3 bucket you created in the previous step.

  2. Run the following command to install the Firehose stream:

    ./deploy-usagelogs.sh $AWS_ACCOUNT_ID $AWS_REGION $LOGS_BUCKET_ARN $STACKNAME

    Example:

    ./deploy-usagelogs.sh 034444869755 eu-central-1 arn:aws:s3:::apiable-logs-dev dev

  3. Copy & Paste the outputs of the run into some editor, which will look similar to this

11. Enable usage reporting in the gateway

  • Go to the API Gateway console and select the deployed Gateway

  • Select the API

  • Go to the Stages

  • Select the Stage

  • Go to the Logs/Tracing and click Edit

  • Enable "Custom access logging"

  • Insert the FirehoseArn from the Output of Step 10 into "Access log destination ARN".

  • Paste following pattern in the "Log format" field and click save. It MUST be one-line:

{"api_id": "$context.apiId","api_key": "$context.identity.apiKey","key_id": "$context.identity.apiKeyId","ip": "$context.identity.sourceIp","method": "$context.httpMethod","uri": "$context.path","response_size":"$context.responseLength","response_status": "$context.status","resource_id": "$context.resourceId","request_id": "$context.requestId","request_latency": "$context.responseLatency","request_time":"$context.requestTimeEpoch","stage": "$context.stage", "plan_id": "$context.authorizer.apiable_plan_id", "subscription_id": "$context.authorizer.apiable_subscription_id"}

You're All Set Up!

Now you have successfully set up the Apiable Gateway Role, S3 bucket, Firehose Stream, and Gateway API connection and enabled usage reporting. You are ready to start using the system with Apiable!

Did this answer your question?