Coder Social home page Coder Social logo

psenger / blog-example-single-cf-distribution Goto Github PK

View Code? Open in Web Editor NEW

This project forked from evnz/blog-example-single-cf-distribution

0.0 1.0 0.0 949 KB

Example of single Amazon CloudFront distribution for both your web app hosted on S3 bucket and API Gateway backend

License: MIT License

JavaScript 5.53% TypeScript 93.69% HTML 0.79%

blog-example-single-cf-distribution's Introduction

Blog example: Single CloudFront Distribution for both your S3 Web App and API Gateway backend

Example of a single Amazon CloudFront distribution for both your web app hosted on S3 bucket and API Gateway backend

Link to blog

This example uses AWS CDK to build out a working example of a single CloudFront distribution that routes to both a web app hosted in S3 and a backend API. Although API GatewayV2 has been used as the backend here, the principles used in this example should work with any other backends (or otherwise known as custom origins in CloudFront). For more information please visit the blog linked above.

Deployment

CAUTION: This stack contains a Lambda@Edge resource which may take some time to delete (you also may see errors requiring manual intenvention using CDK destroy)

  1. Prequisites

    • AWS account with permissions to create CloudFront, S3 and API GatewayV2 resources and everyting inbetween (IAM etc)
    • AWS CLI (configured with your account so you can deploy this directly into your environment)
    • git
    • NodeJS
    • Yarn (You can use NPM)
    • Docker (for compiling the lambdas)

    For more information please visit the blog post instead (linked above).

  2. Clone this repository locally

$ git clone https://github.com/evnz/blog-example-single-cf-distribution.git
  1. Download all the dependencies locally
# change directory to single-cf
$ cd single-cf
# download all the dependencies via yarn (you can also use npm)
$ yarn
  1. Build and deploy
$ yarn build
# if you haven't bootstrapped your environment previously, bootstrap it first
$ yarn cdk bootstrap
# This might take awhile to compile and package all the typescript lambdas and deploying the cloudfront distribution
$ yarn cdk deploy
  1. Test it by visiting different endpoints on the cloudfront distribution
1. `/` should return you the hello world HTML page.
2. `/api/helloworld` should return you the machine friendly hello world message
3. `/api/non-existent-endpoint` should return you a 'not found' message
4. `/api/non-existent-page` should redirect back to `/` with the hello world HTML page
  1. Now you can delete the stack and remove the CDK bootstrap
Go to S3 Web Console and remove the content inside the S3 bucket we created (should just be the index.html file)
We can't delete a non-empty bucket so we will just have to manually empty it out first.
$ yarn cdk destroy
...
# And you may see some error about Lambda@Edge (at this point you will have to wait for Lambda@Edge to be removed from all the cache locations and then manually delete the stack from the Cloudformation console - yes I know it sucks but caching is hard)
If you are not planning to use CDK in this account anymore remove the bootstrap manually via the AWS Cloudformation web console (the stack name should be called CDKToolKit)

blog-example-single-cf-distribution's People

Contributors

evnz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.