Serverless API Key Management Plugin

4.2.1 · active · verified Tue Apr 21

The `serverless-add-api-key` plugin extends the Serverless Framework to provide enhanced management of AWS API Gateway API keys and usage plans. Unlike native Serverless functionality, this plugin allows associating multiple services with the same API key and usage plan, facilitating consistent authentication across microservices. It automatically creates API keys and usage plans if they don't exist and supports associating existing keys. The current stable version is 4.2.1, with releases typically following major Serverless Framework updates or when new features for API Gateway integration are required. Key differentiators include its ability to reuse existing keys, support for multiple keys per service, and advanced configuration options for usage plans, including quotas and throttling. It also provides secure handling of API key values through KMS encryption, preventing raw keys from being exposed in repositories.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure `serverless-add-api-key` to create multiple API keys, including one with a custom usage plan, one with a KMS-encrypted value, and another that defaults to the provider-level usage plan. It shows how to integrate keys with HTTP API Gateway endpoints and use stage-specific naming.

service: my-serverless-api

provider:
  name: aws
  runtime: nodejs18.x
  stage: dev
  region: us-east-1
  usagePlan:
    name: default-api-usage-plan
    description: Default plan for my-serverless-api
    throttle:
      rateLimit: 10
      burstLimit: 5
    quota:
      limit: 1000
      period: DAY

plugins:
  - serverless-add-api-key

custom:
  apiKeys:
    - name: MyServiceGlobalKey-${sls:stage}
      value: ${env:MY_API_KEY_VALUE, 'default-secure-key-1234567890'}
      usagePlan:
        name: CustomUsagePlanForGlobalKey-${sls:stage}
        description: Usage plan specific to the global key for this stage
        throttle:
          rateLimit: 20
          burstLimit: 10
        quota:
          limit: 5000
          period: MONTH
    - name: MyEncryptedKey-${sls:stage}
      value:
        encrypted: AQICAHinIKhx8yV+y97+qS5naGEBUQrTP8RPE4HDnVvd0AzJ/wGF2tC0dPMHO... # Replace with actual KMS encrypted value
        kmsKeyRegion: us-east-1 # Specify the region where the KMS key is located
    - name: AnotherServiceKey-${sls:stage} # This key will use the provider-level usagePlan

functions:
  hello:
    handler: handler.hello
    events:
      - httpApi:
          path: /hello
          method: get
          private: true # Enforce API key usage

# handler.js (example)
// exports.hello = async (event) => {
//   return {
//     statusCode: 200,
//     body: JSON.stringify({ message: 'Hello from Serverless API Key!' }),
//   };
// };

view raw JSON →