From 456e3b15b4da131cccaf99b0fdb568f4519d6b26 Mon Sep 17 00:00:00 2001 From: Byungjin Park Date: Sat, 23 Sep 2023 23:48:01 +0900 Subject: [PATCH] Add lattice-lambda-target-group --- .github/labeler.yaml | 3 + .github/labels.yaml | 3 + README.md | 3 + .../function/index.py | 4 + .../lattice-lambda-target-group/lambda.tf | 19 +++++ examples/lattice-lambda-target-group/main.tf | 30 ++++++++ .../lattice-lambda-target-group/outputs.tf | 9 +++ .../lattice-lambda-target-group/versions.tf | 10 +++ modules/lattice-alb-target-group/README.md | 2 +- modules/lattice-alb-target-group/outputs.tf | 2 +- .../lattice-instance-target-group/README.md | 2 +- .../lattice-instance-target-group/outputs.tf | 2 +- modules/lattice-lambda-target-group/README.md | 58 +++++++++++++++ modules/lattice-lambda-target-group/main.tf | 60 +++++++++++++++ .../lattice-lambda-target-group/outputs.tf | 39 ++++++++++ .../resource-group.tf | 31 ++++++++ .../lattice-lambda-target-group/variables.tf | 74 +++++++++++++++++++ .../lattice-lambda-target-group/versions.tf | 10 +++ 18 files changed, 357 insertions(+), 4 deletions(-) create mode 100644 examples/lattice-lambda-target-group/function/index.py create mode 100644 examples/lattice-lambda-target-group/lambda.tf create mode 100644 examples/lattice-lambda-target-group/main.tf create mode 100644 examples/lattice-lambda-target-group/outputs.tf create mode 100644 examples/lattice-lambda-target-group/versions.tf create mode 100644 modules/lattice-lambda-target-group/README.md create mode 100644 modules/lattice-lambda-target-group/main.tf create mode 100644 modules/lattice-lambda-target-group/outputs.tf create mode 100644 modules/lattice-lambda-target-group/resource-group.tf create mode 100644 modules/lattice-lambda-target-group/variables.tf create mode 100644 modules/lattice-lambda-target-group/versions.tf diff --git a/.github/labeler.yaml b/.github/labeler.yaml index 9285ab9..936fa7d 100644 --- a/.github/labeler.yaml +++ b/.github/labeler.yaml @@ -17,6 +17,9 @@ ":floppy_disk: lattice-ip-target-group": - modules/lattice-ip-target-group/**/* +":floppy_disk: lattice-lambda-target-group": +- modules/lattice-lambda-target-group/**/* + ":floppy_disk: lattice-service-listener": - modules/lattice-service-listener/**/* diff --git a/.github/labels.yaml b/.github/labels.yaml index 3563d86..b2a1a4b 100644 --- a/.github/labels.yaml +++ b/.github/labels.yaml @@ -58,6 +58,9 @@ - color: "fbca04" description: "This issue or pull request is related to lattice-ip-target-group module." name: ":floppy_disk: lattice-ip-target-group" +- color: "fbca04" + description: "This issue or pull request is related to lattice-lambda-target-group module." + name: ":floppy_disk: lattice-lambda-target-group" - color: "fbca04" description: "This issue or pull request is related to lattice-service-listener module." name: ":floppy_disk: lattice-service-listener" diff --git a/README.md b/README.md index 965629d..2c8f100 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Terraform module which creates VPC Connectivity related resources (VPC Peering, - [lattice-alb-target-group](./modules/lattice-alb-target-group) - [lattice-instance-target-group](./modules/lattice-instance-target-group) - [lattice-ip-target-group](./modules/lattice-ip-target-group) +- [lattice-lambda-target-group](./modules/lattice-lambda-target-group) - [lattice-service-listener](./modules/lattice-service-listener) - [lattice-service-network](./modules/lattice-service-network) - [lattice-service](./modules/lattice-service) @@ -42,6 +43,7 @@ Terraform Modules from [this package](https://github.com/tedilabs/terraform-aws- - ALB Target Group - Instance Target Group - IP Target Group + - Lambda Target Group - **AWS DX (Direct Connect)** - Connection - Gateway @@ -61,6 +63,7 @@ Terraform Modules from [this package](https://github.com/tedilabs/terraform-aws- - [lattice-alb-target-group](./examples/lattice-alb-target-group) - [lattice-instance-target-group](./examples/lattice-instance-target-group) - [lattice-ip-target-group](./examples/lattice-ip-target-group) +- [lattice-lambda-target-group](./examples/lattice-lambda-target-group) ### Network Manager diff --git a/examples/lattice-lambda-target-group/function/index.py b/examples/lattice-lambda-target-group/function/index.py new file mode 100644 index 0000000..396c505 --- /dev/null +++ b/examples/lattice-lambda-target-group/function/index.py @@ -0,0 +1,4 @@ +def lambda_handler(event, context): + print("Hello from app1!") + + return event diff --git a/examples/lattice-lambda-target-group/lambda.tf b/examples/lattice-lambda-target-group/lambda.tf new file mode 100644 index 0000000..3dc4fd1 --- /dev/null +++ b/examples/lattice-lambda-target-group/lambda.tf @@ -0,0 +1,19 @@ +################################################### +# Lambda Function +################################################### + +module "lambda_function" { + source = "terraform-aws-modules/lambda/aws" + version = "~> 6.0.0" + + publish = true + + function_name = "lambda-function-for-vpc-lattice" + source_path = "${path.root}/function/" + handler = "index.lambda_handler" + runtime = "python3.8" + + tags = { + "project" = "terraform-aws-vpc-connectivity-examples" + } +} diff --git a/examples/lattice-lambda-target-group/main.tf b/examples/lattice-lambda-target-group/main.tf new file mode 100644 index 0000000..7a9f5bd --- /dev/null +++ b/examples/lattice-lambda-target-group/main.tf @@ -0,0 +1,30 @@ +provider "aws" { + region = "us-east-1" +} + + +################################################### +# Lambda Target Group +################################################### + +module "target_group" { + source = "../../modules/lattice-lambda-target-group" + # source = "tedilabs/vpc-connectivity/aws//modules/lattice-lambda-target-group" + # version = "~> 0.2.0" + + name = "lambda-hello" + + + ## Targets + targets = [ + { + name = "lambda-function-for-vpc-lattice" + lambda_function = module.lambda_function.lambda_function_arn + } + ] + + + tags = { + "project" = "terraform-aws-vpc-connectivity-examples" + } +} diff --git a/examples/lattice-lambda-target-group/outputs.tf b/examples/lattice-lambda-target-group/outputs.tf new file mode 100644 index 0000000..57f0dc5 --- /dev/null +++ b/examples/lattice-lambda-target-group/outputs.tf @@ -0,0 +1,9 @@ +output "target_group" { + description = "The ALB target group of VPC Lattice." + value = module.target_group +} + +output "lambda_function" { + description = "The Lambda function for VPC Lattice." + value = module.lambda_function +} diff --git a/examples/lattice-lambda-target-group/versions.tf b/examples/lattice-lambda-target-group/versions.tf new file mode 100644 index 0000000..59c42e8 --- /dev/null +++ b/examples/lattice-lambda-target-group/versions.tf @@ -0,0 +1,10 @@ +terraform { + required_version = "~> 1.5" + + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 5.0" + } + } +} diff --git a/modules/lattice-alb-target-group/README.md b/modules/lattice-alb-target-group/README.md index 33f7d77..138ba53 100644 --- a/modules/lattice-alb-target-group/README.md +++ b/modules/lattice-alb-target-group/README.md @@ -61,6 +61,6 @@ This module creates following resources. | [protocol\_version](#output\_protocol\_version) | The protocol version. | | [status](#output\_status) | The status of the target group. | | [targets](#output\_targets) | The list of targets of the target group.
`name` - The name of the target. This value is only used internally within Terraform code.
`alb` - The Amazon Resource Name (ARN) of the target ALB (Application Load Balancer).
`port` - The port on which the target is listening. | -| [type](#output\_type) | The type of target group. Always `IP`. | +| [type](#output\_type) | The type of target group. Always `ALB`. | | [vpc](#output\_vpc) | The ID of the VPC which the target group belongs to. | diff --git a/modules/lattice-alb-target-group/outputs.tf b/modules/lattice-alb-target-group/outputs.tf index 816ee05..a4f70f3 100644 --- a/modules/lattice-alb-target-group/outputs.tf +++ b/modules/lattice-alb-target-group/outputs.tf @@ -19,7 +19,7 @@ output "vpc" { } output "type" { - description = "The type of target group. Always `IP`." + description = "The type of target group. Always `ALB`." value = aws_vpclattice_target_group.this.type } diff --git a/modules/lattice-instance-target-group/README.md b/modules/lattice-instance-target-group/README.md index eab82d4..5c7f7fa 100644 --- a/modules/lattice-instance-target-group/README.md +++ b/modules/lattice-instance-target-group/README.md @@ -63,6 +63,6 @@ This module creates following resources. | [protocol\_version](#output\_protocol\_version) | The protocol version. | | [status](#output\_status) | The status of the target group. | | [targets](#output\_targets) | The list of targets of the target group.
`name` - The name of the target. This value is only used internally within Terraform code.
`instance` - This is the Instance ID for an instance.
`port` - The port on which the target is listening. | -| [type](#output\_type) | The type of target group. Always `IP`. | +| [type](#output\_type) | The type of target group. Always `INSTANCE`. | | [vpc](#output\_vpc) | The ID of the VPC which the target group belongs to. | diff --git a/modules/lattice-instance-target-group/outputs.tf b/modules/lattice-instance-target-group/outputs.tf index 97434ed..31d6d3e 100644 --- a/modules/lattice-instance-target-group/outputs.tf +++ b/modules/lattice-instance-target-group/outputs.tf @@ -19,7 +19,7 @@ output "vpc" { } output "type" { - description = "The type of target group. Always `IP`." + description = "The type of target group. Always `INSTANCE`." value = aws_vpclattice_target_group.this.type } diff --git a/modules/lattice-lambda-target-group/README.md b/modules/lattice-lambda-target-group/README.md new file mode 100644 index 0000000..5f83684 --- /dev/null +++ b/modules/lattice-lambda-target-group/README.md @@ -0,0 +1,58 @@ +# lattice-lambda-target-group + +This module creates following resources. + +- `aws_vpclattice_target_group` +- `aws_vpclattice_target_group_attachment` (optional) + + +## Requirements + +| Name | Version | +|------|---------| +| [terraform](#requirement\_terraform) | >= 1.5 | +| [aws](#requirement\_aws) | >= 5.12 | + +## Providers + +| Name | Version | +|------|---------| +| [aws](#provider\_aws) | 5.17.0 | + +## Modules + +| Name | Source | Version | +|------|--------|---------| +| [resource\_group](#module\_resource\_group) | tedilabs/misc/aws//modules/resource-group | ~> 0.10.0 | + +## Resources + +| Name | Type | +|------|------| +| [aws_vpclattice_target_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpclattice_target_group) | resource | +| [aws_vpclattice_target_group_attachment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpclattice_target_group_attachment) | resource | + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| [name](#input\_name) | (Required) The name of the target group. The name must be unique within the account. The valid characters are a-z, 0-9, and hyphens (-). You can't use a hyphen as the first or last character, or immediately after another hyphen. | `string` | n/a | yes | +| [module\_tags\_enabled](#input\_module\_tags\_enabled) | (Optional) Whether to create AWS Resource Tags for the module informations. | `bool` | `true` | no | +| [resource\_group\_description](#input\_resource\_group\_description) | (Optional) The description of Resource Group. | `string` | `"Managed by Terraform."` | no | +| [resource\_group\_enabled](#input\_resource\_group\_enabled) | (Optional) Whether to create Resource Group to find and group AWS resources which are created by this module. | `bool` | `true` | no | +| [resource\_group\_name](#input\_resource\_group\_name) | (Optional) The name of Resource Group. A Resource Group name can have a maximum of 127 characters, including letters, numbers, hyphens, dots, and underscores. The name cannot start with `AWS` or `aws`. | `string` | `""` | no | +| [tags](#input\_tags) | (Optional) A map of tags to add to all resources. | `map(string)` | `{}` | no | +| [targets](#input\_targets) | (Optional) A list of targets to add to the target group. Each value of `targets` block as defined below.
(Required) `name` - The name of the target. This value is only used internally within Terraform code.
(Required) `lambda_function` - The Amazon Resource Name (ARN) of the target Lambda function. If your ARN doesn't specify a version or alias, the latest version ($LATEST) is used by default. If the ARN specifies a version or alias, it appears as the last segment of the ARN separated by a colon. |
list(object({
name = string
lambda_function = string
}))
| `[]` | no | +| [timeouts](#input\_timeouts) | (Optional) How long to wait for the target group to be created/deleted. |
object({
create = optional(string, "5m")
delete = optional(string, "5m")
})
| `{}` | no | + +## Outputs + +| Name | Description | +|------|-------------| +| [arn](#output\_arn) | The ARN of the target group. | +| [id](#output\_id) | The ID of the target group. | +| [name](#output\_name) | The name of the target group. | +| [status](#output\_status) | The status of the target group. | +| [targets](#output\_targets) | The list of targets of the target group.
`name` - The name of the target. This value is only used internally within Terraform code.
`lambda_function` - The Amazon Resource Name (ARN) of the target Lambda function. | +| [type](#output\_type) | The type of target group. Always `LAMBDA`. | + diff --git a/modules/lattice-lambda-target-group/main.tf b/modules/lattice-lambda-target-group/main.tf new file mode 100644 index 0000000..8617045 --- /dev/null +++ b/modules/lattice-lambda-target-group/main.tf @@ -0,0 +1,60 @@ +locals { + metadata = { + package = "terraform-aws-vpc-connectivity" + version = trimspace(file("${path.module}/../../VERSION")) + module = basename(path.module) + name = var.name + } + module_tags = var.module_tags_enabled ? { + "module.terraform.io/package" = local.metadata.package + "module.terraform.io/version" = local.metadata.version + "module.terraform.io/name" = local.metadata.module + "module.terraform.io/full-name" = "${local.metadata.package}/${local.metadata.module}" + "module.terraform.io/instance" = local.metadata.name + } : {} +} + + +################################################### +# Lambda Target Group for VPC Lattice Service +################################################### + +# INFO: Not supported attributes +# - `config` +resource "aws_vpclattice_target_group" "this" { + name = var.name + type = "LAMBDA" + + timeouts { + create = var.timeouts.create + delete = var.timeouts.delete + } + + tags = merge( + { + "Name" = local.metadata.name + }, + local.module_tags, + var.tags, + ) +} + + +################################################### +# Targets for Lambda Target Group +################################################### + +# INFO: Not supported attributes +# - `target.port` +resource "aws_vpclattice_target_group_attachment" "this" { + for_each = { + for target in var.targets : + target.name => target + } + + target_group_identifier = aws_vpclattice_target_group.this.id + + target { + id = each.value.lambda_function + } +} diff --git a/modules/lattice-lambda-target-group/outputs.tf b/modules/lattice-lambda-target-group/outputs.tf new file mode 100644 index 0000000..48749c5 --- /dev/null +++ b/modules/lattice-lambda-target-group/outputs.tf @@ -0,0 +1,39 @@ +output "id" { + description = "The ID of the target group." + value = aws_vpclattice_target_group.this.id +} + +output "arn" { + description = "The ARN of the target group." + value = aws_vpclattice_target_group.this.arn +} + +output "name" { + description = "The name of the target group." + value = aws_vpclattice_target_group.this.name +} + +output "type" { + description = "The type of target group. Always `LAMBDA`." + value = aws_vpclattice_target_group.this.type +} + +output "status" { + description = "The status of the target group." + value = aws_vpclattice_target_group.this.status +} + +output "targets" { + description = < { + name = name + lambda_function = one(target.target[*].id) + } + } +} diff --git a/modules/lattice-lambda-target-group/resource-group.tf b/modules/lattice-lambda-target-group/resource-group.tf new file mode 100644 index 0000000..7487ba0 --- /dev/null +++ b/modules/lattice-lambda-target-group/resource-group.tf @@ -0,0 +1,31 @@ +locals { + resource_group_name = (var.resource_group_name != "" + ? var.resource_group_name + : join(".", [ + local.metadata.package, + local.metadata.module, + replace(local.metadata.name, "/[^a-zA-Z0-9_\\.-]/", "-"), + ]) + ) +} + + +module "resource_group" { + source = "tedilabs/misc/aws//modules/resource-group" + version = "~> 0.10.0" + + count = (var.resource_group_enabled && var.module_tags_enabled) ? 1 : 0 + + name = local.resource_group_name + description = var.resource_group_description + + query = { + resource_tags = local.module_tags + } + + module_tags_enabled = false + tags = merge( + local.module_tags, + var.tags, + ) +} diff --git a/modules/lattice-lambda-target-group/variables.tf b/modules/lattice-lambda-target-group/variables.tf new file mode 100644 index 0000000..f2c9112 --- /dev/null +++ b/modules/lattice-lambda-target-group/variables.tf @@ -0,0 +1,74 @@ +variable "name" { + description = "(Required) The name of the target group. The name must be unique within the account. The valid characters are a-z, 0-9, and hyphens (-). You can't use a hyphen as the first or last character, or immediately after another hyphen." + type = string + nullable = false +} + +variable "targets" { + description = <