Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix resource clash #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Yet another serverless lambda redirect. This one accepts a list of domain names
module "redirect" {
source = "../lambda-redirect"

resource_suffix = "example.com"

domain_mapping = {
"test.example.com": "https://example.com/test",
"mail.example.com": "https://mailprovider.com",
Expand Down Expand Up @@ -65,7 +67,7 @@ No modules.
| <a name="input_aws_acm_certificate"></a> [aws\_acm\_certificate](#input\_aws\_acm\_certificate) | ACM certificate to use with the source domains (must be in us-east-1!) | <pre>object({<br> arn = string<br> })</pre> | n/a | yes |
| <a name="input_domain_mapping"></a> [domain\_mapping](#input\_domain\_mapping) | A key/value map of source domains -> target redirects. For example: domain\_mapping: {"test.example.com": "https://example.com/test"} | `map(string)` | n/a | yes |
| <a name="input_http_redirect_code"></a> [http\_redirect\_code](#input\_http\_redirect\_code) | Which HTTP redirect code to use (301 or 302) | `string` | `"301"` | no |

| <a name="input_resource_suffix"></a> [resource\_suffix](#input\_resource\_suffix) | A suffix to append to resources so they don't clash | `string` | `""` | no |
## Outputs

| Name | Description |
Expand Down
11 changes: 6 additions & 5 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
locals {
domains = keys(var.domain_mapping)
resource_suffix = var.resource_suffix != "" ? "-${var.resource_suffix}" : ""
}

data "archive_file" "redirect_lambda" {
Expand All @@ -14,17 +15,17 @@ data "archive_file" "redirect_lambda" {
filename = "index.py"
}

output_path = "${path.module}/build/lambda_redirect.zip"
output_path = "${path.module}/build/lambda_redirect${local.resource_suffix}.zip"
}

resource "aws_cloudwatch_log_group" "redirect_lambda" {
name = "/aws/lambda/redirect_lambda"
name = "/aws/lambda/redirect_lambda${local.resource_suffix}"
retention_in_days = 7
}

resource "aws_lambda_function" "redirect_lambda" {
filename = data.archive_file.redirect_lambda.output_path
function_name = "redirect_lambda"
function_name = "redirect_lambda${local.resource_suffix}"
role = aws_iam_role.lambda_execution_role.arn
handler = "index.handler"
source_code_hash = data.archive_file.redirect_lambda.output_base64sha256
Expand Down Expand Up @@ -70,12 +71,12 @@ data "aws_iam_policy_document" "lambda_policy" {
}

resource "aws_iam_policy" "lambda_policy" {
name = "lambda-policy"
name = "lambda-policy${local.resource_suffix}"
policy = data.aws_iam_policy_document.lambda_policy.json
}

resource "aws_iam_role" "lambda_execution_role" {
name = "redirect-lambda-execution-role"
name = "redirect-lambda-execution-role${local.resource_suffix}"
assume_role_policy = data.aws_iam_policy_document.lambda_assume_role.json
}

Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ variable "http_redirect_code" {
type = string
description = "Which HTTP redirect code to use (301 or 302)"
default = "301"
}

variable "resource_suffix" {
type = string
description = "A suffix to append to resources so they don't clash"
default = ""
}