npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

serverless-import-apigateway

v1.1.1

Published

Dynamically import an existing AWS API Gateway into your Serverless stack.

Downloads

7,142

Readme

serverless-import-apigateway

Dynamically import an existing AWS API Gateway into your Serverless stack.

Purpose

This plugin allows you to specify the name and paths of your existing API Gateway's REST API and it will lookup and configure the provider's API Gateway with the necessary IDs.

By default, Serverless creates automatically an API Gateway for each Serverless stack or service (i.e. serverless.yml) you deploy. This is sufficient if you only have a single service and monolithic serverless.yml or can deploy your entire Serverless app at once, but if you wish to break up your monolithic Serverless app into multiple serverless.yml services and deploy each stack independently but share the same API Gateway stage/ REST API, then you want each service to use the same API Gateway.

Suppose you have the following existing Serverless service and it has an API Gateway created for an Lambda performing a status check on the root path:

service: first-service

provider:
  name: aws
  runtime: nodejs8.10

functions:
  submit:
    handler: handler.status
    events:
      - http:
          path: /
          method: GET

resources:
  Outputs:
    ExportedApiGatewayRestApi:
      Description: First service's API Gateway REST API resource ID
      Value:
        Ref: ApiGatewayRestApi # Logical ID
      Export:
        Name: ExportedApiGatewayRestApi

Now if you want to create another service and add additional endpoints to this existing API Gateway, Serverless supports configuring the AWS provider to have a service use an existing API Gateway with something like the following:

service: second-service

provider:
  name: aws
  runtime: nodejs8.10
  apiGateway: # Optional API Gateway global config
    restApiId: 2kd8204f8d # REST API resource ID. Default is generated by the framework
    restApiRootResourceId: 9df5ik7fyy # Root resource ID, represent as / path

The restApiId and restApiRootResourceId can be obtained via the AWS CLI with the aws apigateway get-rest-apis and aws apigateway get-resources commands, respectively.

However, it's not ideal to hardcode these IDs into your serverless.yml and unfortunately you cannot import the first service's exported CloudFormation stack output variable ExportedApiGatewayRestApi from within the provider's configuration using something like 'Fn::ImportValue': ExportedApiGatewayRestApi. This will produce an error.

The next best thing is to populate environment variables or pass in options from the CLI using a shell script which queries these IDs from the AWS CLI using the REST API's name so that you can reference them from the provider's configuration with ${opt:restApiId} or ${env:restApiId}. The script might look something like this:

#!/usr/bin/env bash

STAGE=${1:-dev}
REGION=${2:-us-east-1}

RESTAPI_ID=$(aws apigateway get-rest-apis --region ${REGION} \
  --query "items[?name=='${STAGE}-first-service'].id" --output text)
ROOT_RESOURCE_ID=$(aws apigateway get-resources --region ${REGION} --rest-api-id ${RESTAPI_ID} \
  --query "items[?path=='/'].id" --output text)

sls deploy --restApiId ${RESTAPI_ID} --restApiRootResourceId ${ROOT_RESOURCE_ID}

This is hacky and less than ideal.

Install

npm install serverless-import-apigateway --save-dev

Configuration

Add the plugin to your serverless.yml:

plugins:
  - serverless-import-apigateway

Add the custom configuration:

custom:
  importApiGateway:
    name: ${self:provider.stage}-existing-service  # Required
    path: /                                        # Optional
    resources:                                     # Optional
      - /existing
      - /existing/resource

| Property | Required | Type | Default | Description | |-------------|----------|----------|---------|------------------------------------------------------------| | name | true | string | | The name of the REST API for the AWS API Gateway to import | | path | false | string | / | The root resource path to import from the REST API | | resources | false | array | [] | The existing resource paths to import from the REST API |

Usage

Configuration of your serverless.yml is all you need.

There are no custom commands, just run: sls deploy