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

Add lattice-lambda-target-group #14

Merged
merged 1 commit into from
Sep 23, 2023
Merged
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
3 changes: 3 additions & 0 deletions .github/labeler.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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/**/*

Expand Down
3 changes: 3 additions & 0 deletions .github/labels.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
4 changes: 4 additions & 0 deletions examples/lattice-lambda-target-group/function/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def lambda_handler(event, context):
print("Hello from app1!")

return event
19 changes: 19 additions & 0 deletions examples/lattice-lambda-target-group/lambda.tf
Original file line number Diff line number Diff line change
@@ -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"
}
}
30 changes: 30 additions & 0 deletions examples/lattice-lambda-target-group/main.tf
Original file line number Diff line number Diff line change
@@ -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"
}
}
9 changes: 9 additions & 0 deletions examples/lattice-lambda-target-group/outputs.tf
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 10 additions & 0 deletions examples/lattice-lambda-target-group/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = "~> 1.5"

required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
2 changes: 1 addition & 1 deletion modules/lattice-alb-target-group/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ This module creates following resources.
| <a name="output_protocol_version"></a> [protocol\_version](#output\_protocol\_version) | The protocol version. |
| <a name="output_status"></a> [status](#output\_status) | The status of the target group. |
| <a name="output_targets"></a> [targets](#output\_targets) | The list of targets of the target group.<br> `name` - The name of the target. This value is only used internally within Terraform code.<br> `alb` - The Amazon Resource Name (ARN) of the target ALB (Application Load Balancer).<br> `port` - The port on which the target is listening. |
| <a name="output_type"></a> [type](#output\_type) | The type of target group. Always `IP`. |
| <a name="output_type"></a> [type](#output\_type) | The type of target group. Always `ALB`. |
| <a name="output_vpc"></a> [vpc](#output\_vpc) | The ID of the VPC which the target group belongs to. |
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
2 changes: 1 addition & 1 deletion modules/lattice-alb-target-group/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion modules/lattice-instance-target-group/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ This module creates following resources.
| <a name="output_protocol_version"></a> [protocol\_version](#output\_protocol\_version) | The protocol version. |
| <a name="output_status"></a> [status](#output\_status) | The status of the target group. |
| <a name="output_targets"></a> [targets](#output\_targets) | The list of targets of the target group.<br> `name` - The name of the target. This value is only used internally within Terraform code.<br> `instance` - This is the Instance ID for an instance.<br> `port` - The port on which the target is listening. |
| <a name="output_type"></a> [type](#output\_type) | The type of target group. Always `IP`. |
| <a name="output_type"></a> [type](#output\_type) | The type of target group. Always `INSTANCE`. |
| <a name="output_vpc"></a> [vpc](#output\_vpc) | The ID of the VPC which the target group belongs to. |
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
2 changes: 1 addition & 1 deletion modules/lattice-instance-target-group/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
58 changes: 58 additions & 0 deletions modules/lattice-lambda-target-group/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# lattice-lambda-target-group

This module creates following resources.

- `aws_vpclattice_target_group`
- `aws_vpclattice_target_group_attachment` (optional)

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.5 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.12 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.17.0 |

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_resource_group"></a> [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 |
|------|-------------|------|---------|:--------:|
| <a name="input_name"></a> [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 |
| <a name="input_module_tags_enabled"></a> [module\_tags\_enabled](#input\_module\_tags\_enabled) | (Optional) Whether to create AWS Resource Tags for the module informations. | `bool` | `true` | no |
| <a name="input_resource_group_description"></a> [resource\_group\_description](#input\_resource\_group\_description) | (Optional) The description of Resource Group. | `string` | `"Managed by Terraform."` | no |
| <a name="input_resource_group_enabled"></a> [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 |
| <a name="input_resource_group_name"></a> [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 |
| <a name="input_tags"></a> [tags](#input\_tags) | (Optional) A map of tags to add to all resources. | `map(string)` | `{}` | no |
| <a name="input_targets"></a> [targets](#input\_targets) | (Optional) A list of targets to add to the target group. Each value of `targets` block as defined below.<br> (Required) `name` - The name of the target. This value is only used internally within Terraform code.<br> (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. | <pre>list(object({<br> name = string<br> lambda_function = string<br> }))</pre> | `[]` | no |
| <a name="input_timeouts"></a> [timeouts](#input\_timeouts) | (Optional) How long to wait for the target group to be created/deleted. | <pre>object({<br> create = optional(string, "5m")<br> delete = optional(string, "5m")<br> })</pre> | `{}` | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_arn"></a> [arn](#output\_arn) | The ARN of the target group. |
| <a name="output_id"></a> [id](#output\_id) | The ID of the target group. |
| <a name="output_name"></a> [name](#output\_name) | The name of the target group. |
| <a name="output_status"></a> [status](#output\_status) | The status of the target group. |
| <a name="output_targets"></a> [targets](#output\_targets) | The list of targets of the target group.<br> `name` - The name of the target. This value is only used internally within Terraform code.<br> `lambda_function` - The Amazon Resource Name (ARN) of the target Lambda function. |
| <a name="output_type"></a> [type](#output\_type) | The type of target group. Always `LAMBDA`. |
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
60 changes: 60 additions & 0 deletions modules/lattice-lambda-target-group/main.tf
Original file line number Diff line number Diff line change
@@ -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
}
}
39 changes: 39 additions & 0 deletions modules/lattice-lambda-target-group/outputs.tf
Original file line number Diff line number Diff line change
@@ -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 = <<EOF
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.
EOF
value = {
for name, target in aws_vpclattice_target_group_attachment.this :
name => {
name = name
lambda_function = one(target.target[*].id)
}
}
}
31 changes: 31 additions & 0 deletions modules/lattice-lambda-target-group/resource-group.tf
Original file line number Diff line number Diff line change
@@ -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,
)
}
Loading