{"id":19304071,"url":"https://github.com/builtinnya/aws-lambda-edge-basic-auth-terraform","last_synced_at":"2025-04-22T11:32:25.941Z","repository":{"id":43315828,"uuid":"168544693","full_name":"builtinnya/aws-lambda-edge-basic-auth-terraform","owner":"builtinnya","description":"A Terraform module that creates AWS Lambda@Edge resources to protect CloudFront distributions with Basic Authentication.","archived":false,"fork":false,"pushed_at":"2023-01-04T02:31:36.000Z","size":381,"stargazers_count":22,"open_issues_count":15,"forks_count":18,"subscribers_count":2,"default_branch":"master","last_synced_at":"2023-05-05T12:07:03.179Z","etag":null,"topics":["aws","aws-cloudfront","aws-lambda","aws-lambda-edge","basic-auth","basic-authentication","cloudfront","lambda","lambda-edge","terraform","terraform-modules"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/builtinnya.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-01-31T15:08:32.000Z","updated_at":"2023-04-15T21:37:29.000Z","dependencies_parsed_at":"2023-02-01T18:00:51.148Z","dependency_job_id":null,"html_url":"https://github.com/builtinnya/aws-lambda-edge-basic-auth-terraform","commit_stats":null,"previous_names":[],"tags_count":null,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/builtinnya%2Faws-lambda-edge-basic-auth-terraform","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/builtinnya%2Faws-lambda-edge-basic-auth-terraform/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/builtinnya%2Faws-lambda-edge-basic-auth-terraform/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/builtinnya%2Faws-lambda-edge-basic-auth-terraform/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/builtinnya","download_url":"https://codeload.github.com/builtinnya/aws-lambda-edge-basic-auth-terraform/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223896054,"owners_count":17221370,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","aws-cloudfront","aws-lambda","aws-lambda-edge","basic-auth","basic-authentication","cloudfront","lambda","lambda-edge","terraform","terraform-modules"],"created_at":"2024-11-09T23:28:33.420Z","updated_at":"2024-11-09T23:28:34.082Z","avatar_url":"https://github.com/builtinnya.png","language":"JavaScript","readme":"# aws-lambda-edge-basic-auth-terraform\n\n![Basic Authenticate CloudFront with Lambda@Edge](diagrams/aws-cloudfront-lambda-edge-basic-auth.png)\n\nThis is a [Terraform](https://www.terraform.io/) module that creates [AWS Lambda@Edge](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-at-the-edge.html) resources to protect [CloudFront](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Introduction.html) distributions with [Basic Authentication](https://en.wikipedia.org/wiki/Basic_access_authentication).\n\nThe 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.\n\nThe actual code to perform Basic Authentication is derived from [lmakarov/lambda-basic-auth.js](https://gist.github.com/lmakarov/e5984ec16a76548ff2b278c06027f1a4#file-lambda-basic-auth-js).\n\n## Usage\n\nThis is a [Terraform module](https://www.terraform.io/docs/modules/index.html). You just need to include the module in one of your Terraform configuration files with some parameters and add [`lambda_function_association` block](https://www.terraform.io/docs/providers/aws/r/cloudfront_distribution.html#lambda-function-association) to your [`aws_cloudfront_distribution` resource](https://www.terraform.io/docs/providers/aws/r/cloudfront_distribution.html). See [examples/](/examples) for complete examples.\n\n```hcl\n# If the parent module provider region is not us-east-1, define provider for us-east-1.\n#\n#provider \"aws\" {\n#  alias  = \"us-east-1\"\n#  region = \"us-east-1\"\n#}\n\nmodule \"basic_auth\" {\n  source = \"github.com/builtinnya/aws-lambda-edge-basic-auth-terraform/module\"\n\n  basic_auth_credentials = {\n    user     = \"your-username\"\n    password = \"your-password\"\n  }\n\n  # All Lambda@Edge functions must be put on us-east-1.\n  # If the parent module provider region is not us-east-1, you have to\n  # define and pass us-east-1 provider explicitly.\n  # See https://www.terraform.io/docs/modules/usage.html#passing-providers-explicitly for detail.\n  #\n  #providers = {\n  #  aws = \"aws.us-east-1\"\n  #}\n}\n\nresource \"aws_cloudfront_distribution\" \"your_distribution\" {\n  # ...\n\n  # Add the following block to associate the Lambda function.\n  lambda_function_association {\n    event_type   = \"viewer-request\"\n    lambda_arn   = \"${module.basic_auth.lambda_arn}\"\n    include_body = false\n  }\n}\n```\n\n## Inputs\n\n| Name | Description | Type | Default | Required |\n|------|-------------|:----:|:-----:|:-----:|\n| basic\\_auth\\_credentials | Credentials for Basic Authentication. Pass a map composed of 'user' and 'password'. | map | n/a | yes |\n| function\\_name | Lambda function name | string | `\"basicAuth\"` | no |\n\n## Outputs\n\n| Name | Description |\n|------|-------------|\n| lambda\\_arn | Lambda function ARN with version |\n\n## Examples\n\n### Minimal\n\nThe minimal example is located at [examples/minimal](examples/minimal) . It creates an S3 bucket, an S3 object ([`index.html`](examples/minimal/index.html)), and a CloudFront distribution protected with Basic Authentication, enough to confirm that this module protects resources with Basic Authentication.\n\n#### Building Resources\n\n1. Move to [examples/minimal](examples/minimal) directory.\n\n    ```bash\n    $ cd examples/minimal\n    ```\n\n2. Copy `terraform.tfvars.example` to `terraform.tfvars` and fill in the values.\n\n    ```bash\n    $ cp terraform.tfvars.example terraform.tfvars\n    $ # Edit terraform.tfvars with your favorite editor.\n    ```\n\n    ```hcl\n    aws_access_key = \"\u003cyour AWS access key\u003e\"\n    aws_secret_key = \"\u003cyour AWS secret key\u003e\"\n    region = \"\u003cregion\u003e\"\n    s3_bucket_name = \"\u003cS3 bucket name to create\u003e\"\n\n    basic_auth_credentials = {\n      \"user\" = \"\u003cBasic Auth Username\u003e\"\n      \"password\" = \"\u003cBasic Auth Password\u003e\"\n    }\n    ```\n\n3. Execute the following commands to build resources using Terraform.\n\n    ```bash\n    $ terraform init\n    $ terraform plan\n    $ terraform apply\n    ```\n\nIf building succeeded, it will show messages like the following:\n\n```bash\nOutputs:\n\nurl = https://\u003csome-random-string\u003e.cloudfront.net\n```\n\nYou can access to the URL and check if Basic Authentication works once the CloudFront is ready (it takes some time to be ready.)\n\n#### Destroying Resources\n\nTo destroy AWS resources created by the above steps, execute the following commands in [examples/minimal](examples/minimal) directory.\n\n```bash\n$ terraform destroy\n```\n\n**NOTICE: the above command probably ends up with error. See [Deleting Lambda@Edge Functions and Replicas](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-edge-delete-replicas.html) for detail.**\n\n## Development\n\nYou just need to install [Docker](https://www.docker.com/) to develop this module.\n\n- Terraform configurations for this module is located at [module/](module/) directory.\n- Lambda@Edge function source code is located at [src/basic-auth.js](src/basic-auth.js). It needs to be transpiled by [Babel](https://babeljs.io/) and minified by [UglifyJS](http://lisperator.net/uglifyjs/) before zip-compressed by Terraform.\n\n### Updating Lambda Function Code\n\nIf you update the Lambda function source code, you also need to update the function code in the module. It can be done by running:\n\n```bash\n$ ./build.sh\n```\n\n### Deleting Generated Lambda Function Code\n\nIf you want to delete Lambda function code generated by running `./build.sh`, run the following:\n\n```bash\n$ ./clean.sh\n```\n\nYou should rarely have to use the command.\n\n### Generating Inputs and Outputs Documentation\n\nIf you add or remove inputs or outputs of this module, you have to update the documentation.\n\nYou can generate inputs and ouputs documentation of this module by running:\n\n```bash\n$ ./docs.sh\n```\n\nIt shows markdown table of inputs and outputs, same as included in this README.\n\n### Testing\n\nTests for the handler is located at [`test/`](test/) directory and executed in `build.sh`.\n\n### Diagrams\n\nDiagrams are located at [`diagrams/`](diagrams/) directory. You can import and edit XML files visually using [draw.io](https://www.draw.io/).\n\n# License\n\nCopyright © 2019 Naoto Yokoyama\n\nDistributed under the MIT license. See the [LICENSE](./LICENSE) file for full details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbuiltinnya%2Faws-lambda-edge-basic-auth-terraform","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbuiltinnya%2Faws-lambda-edge-basic-auth-terraform","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbuiltinnya%2Faws-lambda-edge-basic-auth-terraform/lists"}