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

awsm-cloudfront

v0.2.0

Published

AWSM Module for JAWS projects that demonstrates how to host a single page web application behind a CloudFront distribution.

Downloads

8

Readme

AWSM CloudFront

This is an awsm module that puts a CloudFront distribution in front of your JAWS project.

Setup and Insallation

First install this module. This takes two commands, first run: npm install --save awsm-cloudfront then run jaws postinstall awsm-cloudfront npm.

This will update your resources-cf.json template for each stage. Now you need to update your resources-cf.json files.

Manually update your resources-cf.json

Right now this project uses hard coded values for the S3 bucket and the API Gateway endpoint. You'll need to update your resources-cf.json accordingly. You'll want to set your own S3 bucket for hosting the static contents of your website, and you'll want to specify your API Gateway host as the host for the custom origin, and the API Gateway stage as the path for that custom origin. This is an issue that we're working to resolve.

Now: Web Applications using Just AWS Without Servers!

The Pieces

Amazon S3 Static Website Hosting

Amazon has documentation about static website hosting that explains how to use S3 to host a static website.

API Gateway and Lambda with JAWS

We then use JAWS to host our backend API. JAWS makes deploying to API Gateway and Lambda simple and repeatable. It also integrates with the awsm module system to make coordinating with tools like this one easy!

CloudFront to merge the two domains

The above mentioned technologies are great, but the best you can do is host your web application at www.example.com and then host your API at api.example.com. This can work, but relies on CORS and generally just adds a bunch of extra work.

Using CloudFront you can use both of these setups on the same domain. We create a CloudFront distribution with two origins. The first (default) origin is our static S3 bucket, then we add our API Gateway endpoint as our second origin. We then setup a Cache Behavior that routes traffic for a specific set of routes, like /api to our API Gateway endpoint.

Gotchas

Watch your paths

There are many paths involved here, keeping them all straight is of paramount importance.

Here's the rundown:

  • The API Gateway origin has a config named OriginPath.
  • The Cache Behavior uses a PathPattern to determine which requests are sent to our API Gateway.
  • There is the path of the request the user makes to our CloudFront distribution.
  • Then there is the path of the request that CloudFront makes to our API Gateway.

There are a few correct ways to configure these, but I'll walk through just one.

You can set OriginPath to you JAWS / API Gateway stage, "/dev" or "/production". This means that calls from our frontend to our backend don't need to know about which stage they're operating on. Note: If you don't want to specify an origin path then use "" not "/".

Next you can configure your Cache Behavior to use "api/*" as the PathPattern. This means all requests to your CloudFront distribution that have api/ at the start of their route will be redirected to our API Gateway.

Then the paths on API Gateway should all start with api/. This matches with the PathPattern we specified for our Cache Behavior. For the time being this requires you to manually update the awsm.json for each API Gateway endpoint and add api/ to the front, hopefully that will change soon. Note: Multiple people have expected that the api/ portion of our request would be trimmed when sent to API Gateway, but it is not.

An Example Request

We run something like curl https://d1z31i778g9an.cloudfront.net/api/users/list (that is not a real CloudFront distribution).

CloudFront uses the Cache Behavior with api/* since it matches and forwards this request to API Gateway. The request that is sent to API Gateway is equivalent to: curl https://hga136gdlt.execute-api.us-west-2.amazonaws.com/dev/api/users/list

Note how the OriginPath was prepended to the request path, and the PathPattern was not removed.

HTTPS Only

The API Gateway is HTTPS only. You must create the API Gateway origin to have match-viewer for the protocol policy. You must then ensure that the CacheBehavior for the API Gateway uses redirect-to-https for the ViewerProtocolPolicy. Luckily the CloudFormation resource defined in this module has all of those settings built in, so these settings are all taken care of.