go-lambda-webapp-template

command module
v0.0.0-...-72b052d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 1, 2022 License: MIT Imports: 15 Imported by: 0

README

Go Lambda WebApp Template

This repository is a template that can be used to start building a self contained web-application written in Go and deployed on AWS Lambda.

This template makes use of minimal external components while providing a ready-to-use template to develop an all-in-one Lambda web application.

This template makes use of:

  • Apex Gateway to map between Lambda requests and standard net.http requests.
  • CHI to route incoming requests.

Usage

Clone the Repo

git clone https://github.com/ericdaugherty/go-lambda-webapp-template newappname

Since you are creating your own project, remove the remote repo reference

git remote rm origin

Edit go.mod and change the module name to reflect your new project.

Run Locally

make run

This will start the default webserver on http://localhost:3000. You should be able to modify the HTML templates without recompiling the app or restarting the webserver.

Deploy To AWS

The template includes a serverless file to support easy deployment to AWS Lambda, including creating an API Gateway HTTP Endpoint.

To deploy, first make sure serverless is installed and configured with your AWS credentials.

Then deploy:

make deploy

This should create a new CloudFormation stack on AWS including your all-in-one lambda and an REST API Gateway HTTP Endpoint.

Serverless should provide the HTTP Endpoint URL for you. It should look something like:

https://<app-id>.execute-api.<region>.amazonaws.com/<stage> 

You will want to add a trailing / to make sure the links work.

Deploy using CloudFormation with a Custom Domain

This basic setup has some limitations.

  • You must include the stage name as part of the url (ie /dev)
  • AWS API Gateway only serves via HTTPS, which is fine, but you can't easily force http -> https
  • You are using a generic AWS Domain

All of this can be solved with a few easy steps. The most complete solution is to use CloudFront with a custom domain.

  • Deploy the REST API Gateway Endpoint as Regional (Uncomment "endpointType: regional" in serverless.yml and "make deploy")
  • Configure a certificate for your custom domain (https://console.aws.amazon.com/acm/home)
  • Configure a CloudFront Distribution
    • Origin Domain Name: https://.execute-api..amazonaws.com
    • Origin Path: / (ex /dev)
    • Minimum Origin SSL Protocol: TLSv1.2
    • Origin Protocol Policy: HTTPS Only
    • Viewer Protocol Policy: Redirect HTTP to HTTPS (Not required but best practice)
    • Alternate Domain Names (CNAMEs): yourcustomdomain.com
    • SSL Certificate: Choose Custom Certificate and select the certificate you setup for this domain

Once the distribution is setup, go to your DNS provider and setup your custom domain CNAME to point to the domain name of your newly created CloudFront Distribution.

Once the CloudFront distribution is deployed and your DNS entries have propagated, you should be able to access your Lambda web app via your custom domain, and HTTP requests should auto-upgrade to HTTPS.

Note: This example uses the REST API Gateway by default. You can also use the newer HTTP API Gateway by changing the "- http:" lines in serverless.yml to "- httpApi:". For usage as a webapp this may be a better (cheaper) alternative. Please view the feature comparison for details.

Deploy using API Custom Domain

Note: This approach only supports https, not http or http->https

  • Configure a certificate for your custom domain (https://console.aws.amazon.com/acm/home)
  • Navigate to the API Gateway Console
  • Select 'Custom domain names' and click "Create"
    • Enter the full name of the domain you created a certificate for
    • Leave the Endpoint and TLS Defaults
    • Select your certificate from the drop-down
    • Select 'Create'
    • On the next page, select 'Configure API Mappings'
    • Select 'Add new mapping'
    • Map your API Endpoint to the domain
  • Update your DNS records to point your custom CNAME to the 'API Gateway domain name' value.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL