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

grunt-aws-apigateway

v0.0.9

Published

A grunt plugin to easily configure and deploy AWS API Gateway.

Downloads

3

Readme

grunt-aws-apigateway

A grunt plugin to easily configure and deploy AWS API Gateway.

Scope

AWS API Gateway configuration through the web console takes lot of mouse clicks and is error prone. This grunt plugin allows you to script and automatically deploy your API Gateway configuration.

Install

npm install grunt-aws-apigateway --save-dev

Usage

The following usage example could look a bit verbose at a first glance. However, AWS API Gateway setup is pretty complex and the following resources structure tries to map as much close as possible the AWS API, in order to provide the same degree of flexibility.

grunt.initConfig({
    apigateway_deploy: {
        options: {
            accessKeyId: "key",
            secretAccessKey: "secret",
            region: "us-east-1"
        },
        default: {
            restApiId: "xxx",
            deployment: {
                stageName: "prod"
            },
            resources: {
                "/users": {
                    methods: {
                        GET: {
                            integration: {
                                type: "AWS",
                                uri: "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:xxx:function:getUsers/invocations",
                                integrationHttpMethod: "POST",
                                requestTemplates: {
                                    "application/json": JSON.stringify({
                                        "filter": "$input.params('filter')"
                                    })
                                }
                            },
                            responses: {
                                200: {
                                    // Pass-through
                                    responseModels: {
                                        "application/json": "Empty"
                                    }
                                },
                                400: {
                                    selectionPattern: "error code: 400",
                                    responseTemplates: {
                                        "application/json": JSON.stringify({"error": "$input.path('$.errorMessage')"})
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
});

grunt.loadNpmTasks('grunt-aws-apigateway');

grunt.registerTask(
    "deploy-api",
    "Deploy the API Gateway config",
    ["apigateway_deploy"]
);

Features

What you can do:

  • Create API resources
  • Configure resource's Method Request, Integration Request, Method Response, Integration Response
  • Deploy API changes to a stage

What you can't do:

  • Create models

One time configuration you should do by hand:

  • Create a new API
  • Create stages (once created, the plugin will deploy the API to the configured stage)
  • Create an IAM user with the policy required to deploy your API
  • Add permission to each Lambda function will get invoked by API Gateway (in case you're integration API Gateway with AWS Lambda functions). Troubleshooting section explains how to do it.

NOTE: to ease the development of this plugin, each run deletes all resources and re-creates them. This means that it doesn't apply differential changes and if you've already created some resources that are not part of plugin configuration, you will loose it at the first run. In other words, make sure the plugin configuration contains all resources of your API.

Configuration

The plugin config is made of 3 required properties:

  • restApiId (your API Gateway id)
  • resources
  • deployment

resources

The resources property contains the configuration of all your API Resources. Resources are organized in a tree, the path must start with a / and each resource's full path is built concatenating together all parent resources paths.

Example - create /tweets/trends resource:

{
    resources: {
        "/tweets": {
            "/trends": {}
        }
    }
}

Each resource can have zero or more methods. For each method you must define a request and response integration (ie. a AWS Lambda function).

Example - add GET method to /tweets/trends resource:

{
    resources: {
        "/tweets": {
            "/trends": {
                methods: {
                    GET: {
                        authorizationType: "NONE",
                        apiKeyRequired: false,
                        integration: { /* Integration Request config */ },
                        responses: { /* Integration Response config */ }
                    }
                }
            }
        }
    }
}

The resource's Integration Request config must contain the integration type and uri, along with other optional settings (ie. requestTemplates).

Example - integrate a lambda function to GET /tweets/trends:

integration: {
    type: "AWS",
    uri: "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:xxx:function:getUsers/invocations",
    integrationHttpMethod: "POST", // Lambda functions must be called with a POST
    credentials: undefined,
    requestParameters: {},
    cacheNamespace: undefined,
    cacheKeyParameters: [],
    requestTemplates: {
        "application/json": JSON.stringify({
            "filter": "$input.params('filter')"
        })
    }
}

The resource's Integration Response config is a map whose keys are the status codes, and the value is the configuration for each status code.

Example - integrate 200 and 400 response codes:

responses: {
    200: {
        responseModels: {
            "application/json": "Empty"
        },
        responseParameters: {},
        responseTemplates: {}
    },
    400: {
        selectionPattern: "error code: 400",
        responseParameters: {},
        responseTemplates: {
            "application/json": JSON.stringify({"error": "$input.path('$.errorMessage')"})
        }
    }
}

deployment

The last step of apigateway_deploy is to deploy all the changes to a stage. The configuration of this phase is made through the deployment property. stageName is the only required property.

{
    deployment: {
        stageName: "prod",
        cacheClusterEnabled: false,
        cacheClusterSize: "1G"
        description: "",
        stageDescription: "",
        variables: []
    }
}

Options

options.region

AWS region where you would like to deploy your API.

options.accessKeyId and options.secretAccessKey

If you prefer to use hardcoded AWS credentials, you should both set accessKeyId and secretAccessKey.

options.credentialsJSON

If you prefer to use AWS credentials stored in a JSON file, you should set the JSON file path here.

options.profile

If you prefer to use a specific AWS credentials profile you can set it here.

Troubleshooting

"Internal server error" due to "Invalid permissions on Lambda function"

This error occurs when you didn't add permission to run the Lambda function from the API Gateway service. To fix it, run the following command:

$ aws lambda add-permission --function-name "<your function name>" --action "lambda:InvokeFunction" --principal "apigateway.amazonaws.com" --source-arn "<your api gateway endpoint arn>" --statement-id "522028352"

Example:

aws lambda add-permission --function-name "getUsers" --action "lambda:InvokeFunction" --principal "apigateway.amazonaws.com" --source-arn "arn:aws:execute-api:us-east-1:123456789:abcdefg/*/GET/users" --statement-id "522028352"

Contributing

You're very welcome to contribute to this pluging, in case you spot any bug, or to add some missing features (ie. create models). Please do your best to:

  • Keep the coding style
  • Keep your code as much clean (and readable) as possible

License

MIT