Coder Social home page Coder Social logo

lambda-s3-static-proxy's Introduction

AWS Serverless Static Site Gateway

architecture

This project provides a serverless architecture for hosting static websites in S3 using AWS Lambda and API Gateway, without the need for enabling S3 Public Access or using Amazon CloudFront. The setup serves static content stored in an S3 bucket through a Lambda function, which acts as a reverse proxy.

Prerequisites

Before starting the setup, ensure you have the following:

  • AWS Account: An active account with AWS that can access Lambda, S3, and API Gateway services.
  • Golang Compilation Environment: A setup with Go installed, enabling you to compile the source code for deployment.

Setup

Step 1: Prepare the Lambda Function

  1. Create the Lambda Function:

    • Navigate to the AWS Lambda console and create a new Lambda function.

    • Choose the runtime as Amazon Linux 2 arm64.

    • Set the handler to bootstrap. This configuration is necessary because you are using a custom runtime environment.

  2. Compile the Code:

    • Use make package command in your local Go environment to compile the Lambda function code. This command should create a function.zip file in the build directory.
  3. Upload the Compiled Function:

    • Upload the build/function.zip file to your Lambda function.
  4. Configure Environment Variables:

    • Set environment variables BUCKET_NAME to specify the S3 bucket your Lambda.
  5. Set IAM Role Permissions:

    • Ensure the IAM role associated with your Lambda function has the necessary permissions to access S3. This typically includes actions like s3:GetObject within the policy attached to the role.

Step 2: S3 Bucket Configuration

  • Create an S3 Bucket: If you don’t already have one, create a new S3 bucket where your static files and SPA will reside.

  • Upload Files: Populate your bucket with the static and SPA files like index.html, JavaScript, CSS, and images.

Step 3: Configure API Gateway

  1. Create API Gateway:

    • Open the AWS API Gateway console and create a new REST API.
  2. Configure GET Method for Root:

    • Create a new resource corresponding to the root path /.

    • Attach a GET method to this resource that triggers your Lambda function. This setup handles requests directed at your root URL.

  3. Setup {proxy+} with ANY Method:

    • Add a {proxy+} resource under the root to handle all other paths and HTTP methods. Set this to trigger the same Lambda function.
  4. Enable Binary Support:

    • Go to the API settings and add / to the binary media types list to handle all content types as binary data.
  5. Deploy the API:

    • Deploy your API to make it accessible, selecting or creating a new deployment stage.
  6. Configure Custom Domain Name

    • Navigate to the “Custom Domain Names” section in the API Gateway console.
    • Click “Create”, and specify your desired domain name.
    • Link the domain name with a certificate from AWS Certificate Manager (ACM). Note: Ensure you have a certificate ready in ACM for the domain you intend to use.
    • Set up a base path mapping that connects your custom domain to the deployed API stage.

After deploying the API Gateway and Lambda function, you can access your SPA and static files via the URL provided by API Gateway. Use this URL to access your site:

https://example.com/

Troubleshooting

Images not displaying: Verify that binary media types are properly set in API Gateway.

lambda-s3-static-proxy's People

Contributors

hephyr 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.