Skip to content

Commit

Permalink
Fix resource clash
Browse files Browse the repository at this point in the history
  • Loading branch information
marrold committed May 6, 2024
1 parent 4a3a79f commit 12f66ae
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
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 = ""
}

0 comments on commit 12f66ae

Please sign in to comment.