All Projects → builtinnya → aws-lambda-edge-basic-auth-terraform

builtinnya / aws-lambda-edge-basic-auth-terraform

Licence: MIT License
A Terraform module that creates AWS Lambda@Edge resources to protect CloudFront distributions with Basic Authentication.

Programming Languages

javascript
184084 projects - #8 most used programming language
HCL
1544 projects
shell
77523 projects
Dockerfile
14818 projects

Projects that are alternatives of or similar to aws-lambda-edge-basic-auth-terraform

rust-wasm-on-lambda-edge
Rust/WASM on AWS Lambda@Edge (CloudFront)
Stars: ✭ 12 (-33.33%)
Mutual labels:  aws-lambda, cloudfront, aws-cloudfront, aws-lambda-edge
Serverless Next.js
⚡ Deploy your Next.js apps on AWS Lambda@Edge via Serverless Components
Stars: ✭ 2,977 (+16438.89%)
Mutual labels:  lambda, aws-lambda, cloudfront
Serverless Sharp
Serverless image optimizer for S3, Lambda, and Cloudfront
Stars: ✭ 102 (+466.67%)
Mutual labels:  lambda, aws-lambda, cloudfront
terraform-aws-cloudfront
Terraform module which creates CloudFront resources on AWS 🇺🇦
Stars: ✭ 56 (+211.11%)
Mutual labels:  cloudfront, aws-cloudfront
cloudfront-image-proxy
Make CloudFront resize images "on the fly" via lambda@edge, cache it and persists it in S3.
Stars: ✭ 32 (+77.78%)
Mutual labels:  cloudfront, lambda-edge
terraform-aws-efs-backup
Terraform module designed to easily backup EFS filesystems to S3 using DataPipeline
Stars: ✭ 40 (+122.22%)
Mutual labels:  lambda, terraform-modules
Algnhsa
AWS Lambda Go net/http server adapter
Stars: ✭ 226 (+1155.56%)
Mutual labels:  lambda, aws-lambda
shim
HTTP Handler shim for Go projects running on AWS Lambda
Stars: ✭ 64 (+255.56%)
Mutual labels:  lambda, aws-lambda
gozeit
GoZeit
Stars: ✭ 19 (+5.56%)
Mutual labels:  lambda, cloudfront
lambda-cron
LambdaCron - serverless cron tool
Stars: ✭ 22 (+22.22%)
Mutual labels:  lambda, aws-lambda
super-serverless-sample
Backend serverless que simula o sistema de votação do BBB
Stars: ✭ 30 (+66.67%)
Mutual labels:  lambda, aws-lambda
nuxt-on-lambda
Nuxt.jsをAWS Lambdaで動かす
Stars: ✭ 78 (+333.33%)
Mutual labels:  lambda, aws-lambda
terraform-aws-lambda-function
A Terraform module for deploying and managing Lambda functions on Amazon Web Services (AWS). https://aws.amazon.com/lambda/
Stars: ✭ 37 (+105.56%)
Mutual labels:  lambda, terraform-modules
static-auth
The most simple way to add Basic Authentication to a static website hosted on Vercel.
Stars: ✭ 25 (+38.89%)
Mutual labels:  basic-authentication, basic-auth
aws-cfn-custom-resource-lambda-edge
🏗 AWS CloudFormation custom resource that allows deploying Lambda@Edge from any region
Stars: ✭ 19 (+5.56%)
Mutual labels:  aws-cloudfront, aws-lambda-edge
Lambdium
headless chrome + selenium webdriver in AWS Lambda using the serverless application model
Stars: ✭ 246 (+1266.67%)
Mutual labels:  lambda, aws-lambda
terraform-aws-lambda-auto-package
A terraform module to define a lambda function which source files are automatically built and packaged for lambda deployment.
Stars: ✭ 23 (+27.78%)
Mutual labels:  lambda, terraform-modules
cerberus-serverless-components
A collection of AWS Serverless components for Cerberus
Stars: ✭ 12 (-33.33%)
Mutual labels:  lambda, cloudfront
Apilogs
Easy logging and debugging for Amazon API Gateway and AWS Lambda Serverless APIs
Stars: ✭ 216 (+1100%)
Mutual labels:  lambda, aws-lambda
Serverless Chrome
🌐 Run headless Chrome/Chromium on AWS Lambda
Stars: ✭ 2,625 (+14483.33%)
Mutual labels:  lambda, aws-lambda

aws-lambda-edge-basic-auth-terraform

Basic Authenticate CloudFront with Lambda@Edge

This is a Terraform module that creates AWS Lambda@Edge resources to protect CloudFront distributions with Basic Authentication.

The purpose of this module is to make it no-brainer to set up AWS resources required to perform Basic Authentication with AWS Lambda@Edge. If you don't want to take care of tedious jobs such as IAM role setup, this is a right module to go with.

The actual code to perform Basic Authentication is derived from lmakarov/lambda-basic-auth.js.

Usage

This is a Terraform module. You just need to include the module in one of your Terraform configuration files with some parameters and add lambda_function_association block to your aws_cloudfront_distribution resource. See examples/ for complete examples.

# If the parent module provider region is not us-east-1, define provider for us-east-1.
#
#provider "aws" {
#  alias  = "us-east-1"
#  region = "us-east-1"
#}

module "basic_auth" {
  source = "github.com/builtinnya/aws-lambda-edge-basic-auth-terraform/module"

  basic_auth_credentials = {
    user     = "your-username"
    password = "your-password"
  }

  # All Lambda@Edge functions must be put on us-east-1.
  # If the parent module provider region is not us-east-1, you have to
  # define and pass us-east-1 provider explicitly.
  # See https://www.terraform.io/docs/modules/usage.html#passing-providers-explicitly for detail.
  #
  #providers = {
  #  aws = "aws.us-east-1"
  #}
}

resource "aws_cloudfront_distribution" "your_distribution" {
  # ...

  # Add the following block to associate the Lambda function.
  lambda_function_association {
    event_type   = "viewer-request"
    lambda_arn   = "${module.basic_auth.lambda_arn}"
    include_body = false
  }
}

Inputs

Name Description Type Default Required
basic_auth_credentials Credentials for Basic Authentication. Pass a map composed of 'user' and 'password'. map n/a yes
function_name Lambda function name string "basicAuth" no

Outputs

Name Description
lambda_arn Lambda function ARN with version

Examples

Minimal

The minimal example is located at examples/minimal . It creates an S3 bucket, an S3 object (index.html), and a CloudFront distribution protected with Basic Authentication, enough to confirm that this module protects resources with Basic Authentication.

Building Resources

  1. Move to examples/minimal directory.

    $ cd examples/minimal
  2. Copy terraform.tfvars.example to terraform.tfvars and fill in the values.

    $ cp terraform.tfvars.example terraform.tfvars
    $ # Edit terraform.tfvars with your favorite editor.
    aws_access_key = "<your AWS access key>"
    aws_secret_key = "<your AWS secret key>"
    region = "<region>"
    s3_bucket_name = "<S3 bucket name to create>"
    
    basic_auth_credentials = {
      "user" = "<Basic Auth Username>"
      "password" = "<Basic Auth Password>"
    }
  3. Execute the following commands to build resources using Terraform.

    $ terraform init
    $ terraform plan
    $ terraform apply

If building succeeded, it will show messages like the following:

Outputs:

url = https://<some-random-string>.cloudfront.net

You can access to the URL and check if Basic Authentication works once the CloudFront is ready (it takes some time to be ready.)

Destroying Resources

To destroy AWS resources created by the above steps, execute the following commands in examples/minimal directory.

$ terraform destroy

NOTICE: the above command probably ends up with error. See Deleting Lambda@Edge Functions and Replicas for detail.

Development

You just need to install Docker to develop this module.

  • Terraform configurations for this module is located at module/ directory.
  • Lambda@Edge function source code is located at src/basic-auth.js. It needs to be transpiled by Babel and minified by UglifyJS before zip-compressed by Terraform.

Updating Lambda Function Code

If you update the Lambda function source code, you also need to update the function code in the module. It can be done by running:

$ ./build.sh

Deleting Generated Lambda Function Code

If you want to delete Lambda function code generated by running ./build.sh, run the following:

$ ./clean.sh

You should rarely have to use the command.

Generating Inputs and Outputs Documentation

If you add or remove inputs or outputs of this module, you have to update the documentation.

You can generate inputs and ouputs documentation of this module by running:

$ ./docs.sh

It shows markdown table of inputs and outputs, same as included in this README.

Testing

Tests for the handler is located at test/ directory and executed in build.sh.

Diagrams

Diagrams are located at diagrams/ directory. You can import and edit XML files visually using draw.io.

License

Copyright © 2019 Naoto Yokoyama

Distributed under the MIT license. See the LICENSE file for full details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].