Skip to content

Commit

Permalink
Support conditions for lattice-service-listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
posquit0 committed Sep 21, 2023
1 parent 22632f7 commit 5edff45
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 28 deletions.
4 changes: 2 additions & 2 deletions modules/lattice-service-listener/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ This module creates following resources.
| <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_rules"></a> [rules](#input\_rules) | (Optional) The configuration for the parameters of the default routing action. `default_action_parameters` block as defined below.<br> (Optional) `status_code` - The status code of HTTP response. Valid values are `2XX`, `4XX`, or `5XX`. Defaults to `503`. Only supported if `default_action_type` is `FIXED_RESPONSE`.<br> (Required) `targets` - A list of target configurations to route traffic. To route to a single target group, use `default_action_type` as `FORWARD`. Only supported if `default_action_type` is `WEIGHTED_FORWARD`. Each item of `targets` block as defined below.<br> (Required) `target_group` - The ARN of the target group to which to route traffic.<br> (Optional) `weight` - The weight to use routing traffic to `target_group`. Valid value is `0` to `999`. Defaults to `1`. | <pre>list(object({<br> priority = number<br> name = optional(string)<br><br> action_type = string<br> action_parameters = optional(object({<br> status_code = optional(number, 404)<br> destinations = optional(list(object({<br> target_group = string<br> weight = optional(number, 100)<br> })), [])<br> }), {})<br> }))</pre> | `[]` | no |
| <a name="input_rules"></a> [rules](#input\_rules) | (Optional) A list of rules to enable content-based routing to the target groups that make up the service. Each rule consists of a priority, one or more actions, and one or more conditions.<br> Each block of `rules` block as defined below.<br> (Required) `priority` - The priority assigned to the rule. Each rule for a specific listener must have a unique priority. The lower the priority number the higher the priority.<br> (Optional) `name` - A rule name can describe the purpose of the rule or the type of traffic it is intended to handle. Rule names can't be changed after creation. Defaults to `$(service)-$(priority)`.<br> (Required) `conditions` - The rule conditions. `conditions` block as defined below.<br> (Optional) `method` - The condition of HTTP request method. Valid values are `GET`, `HEAD`, `POST`, `PUT`, `DELETE`, `CONNECT`, `OPTIONS`, `TRACE`, `PATCH`.<br> (Required) `path` - The condition of HTTP request path. `path` block as defined below.<br> (Required) `value` - The path pattern. The pattern must start with `/`.<br> (Optional) `operator` - The operator that you want to use to determine whether an HTTP request path matches the conditions. Valid values are `EXACT`, `PREFIX`. Defaults to `PREFIX`.<br> (Optional) `case_sensitive` - Whether to match the `value` condition using a case-sensitive match. Defaults to `false`.<br> (Optional) `headers` - The condition of HTTP request headers. Each block of `headers` as defined below.<br> (Required) `name` - The name of the HTTP header field.<br> (Required) `value` - The value of the HTTP header field.<br> (Optional) `operator` - The operator that you want to use to determine whether an HTTP header matches the conditions. Valid values are `EXACT`, `PREFIX`, `CONTAINS`. Defaults to `EXACT`.<br> (Optional) `case_sensitive` - Whether to match the `value` condition using a case-sensitive match. Defaults to `false`.<br> (Required) `action_type` - The action type for the rule of the service. Valid values are `FORWARD`, `FIXED_RESPONSE`.<br> (Optional) `action_parameters` - The configuration for the parameters of the routing action. `action_parameters` block as defined below.<br> (Optional) `status_code` - Custom HTTP status code to drop client requests and return a custom HTTP response. Valid values are `404`. Only supported if `action_type` is `FIXED_RESPONSE`.<br> (Optional) `destinations` - A list of one or more target groups to route traffic. Only supported if `action_type` is `FORWARD`. Each item of `destinations` block as defined below.<br> (Required) `target_group` - The ID or ARN of the target group to which to route traffic.<br> (Optional) `weight` - The weight to use routing traffic to `target_group`. how requests are distributed to the target group. Only required if you specify multiple target groups for a forward action. For example, if you specify two target groups, one with a weight of 10 and the other with a weight of 20, the target group with a weight of 20 receives twice as many requests as the other target group. Valid value is `0` to `999`. Defaults to `100`. | <pre>list(object({<br> priority = number<br> name = optional(string)<br><br> conditions = object({<br> method = optional(string)<br> path = object({<br> value = string<br> operator = optional(string, "PREFIX")<br> case_sensitive = optional(bool, false)<br> })<br> headers = optional(list(object({<br> name = string<br> value = string<br> operator = optional(string, "EXACT")<br> case_sensitive = optional(bool, false)<br> })), [])<br> })<br><br> action_type = string<br> action_parameters = optional(object({<br> status_code = optional(number, 404)<br> destinations = optional(list(object({<br> target_group = string<br> weight = optional(number, 100)<br> })), [])<br> }), {})<br> }))</pre> | `[]` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | (Optional) A map of tags to add to all resources. | `map(string)` | `{}` | no |

## Outputs
Expand All @@ -60,7 +60,7 @@ This module creates following resources.
| <a name="output_name"></a> [name](#output\_name) | The name of the service listener. |
| <a name="output_port"></a> [port](#output\_port) | The number of port on which the listener of the service is listening. |
| <a name="output_protocol"></a> [protocol](#output\_protocol) | The protocol for the service listener. |
| <a name="output_rules"></a> [rules](#output\_rules) | The configuration for default routing action of the service listener.<br> `type` - The type of default routing action.<br> `parameters` - The configuration for the parameters of the default routing action. `default_action_parameters` block as defined below. |
| <a name="output_rules"></a> [rules](#output\_rules) | The list of rules to enable content-based routing to the target groups that make up the service.<br> `id` - Unique identifier for the listener rule.<br> `arn` - The ARN for the listener rule.<br> `priority` - The priority assigned to the listener rule.<br> `name` - The rule name to describe the purpose of the listener rule.<br> `conditions` - The rule conditions.<br> `method` - The condition of HTTP request method.<br> `path` - The condition of HTTP request path.<br> `headers` - The condition of HTTP request headers.<br> `action` - The action for the listener rule.<br> `type` - The action type for the rule of the service.<br> `parameters` - The configuration for the parameters of the routing action. |
| <a name="output_service"></a> [service](#output\_service) | The associated VPC Lattice service. |
| <a name="output_updated_at"></a> [updated\_at](#output\_updated\_at) | Date and time that the listener was last updated, specified in ISO-8601 format. |
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
25 changes: 17 additions & 8 deletions modules/lattice-service-listener/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,31 @@ resource "aws_vpclattice_listener_rule" "this" {

match {
http_match {
method = each.value.conditions.method

header_matches {
name = "example-header"
case_sensitive = false
path_match {
case_sensitive = each.value.conditions.path.case_sensitive

match {
exact = "example-contains"
exact = each.value.conditions.path.operator == "EXACT" ? each.value.conditions.path.value : null
prefix = each.value.conditions.path.operator == "PREFIX" ? each.value.conditions.path.value : null
}
}

path_match {
case_sensitive = true
match {
prefix = "/example-path"
dynamic "header_matches" {
for_each = each.value.conditions.headers

content {
name = header_matches.value.name
case_sensitive = header_matches.value.case_sensitive

match {
exact = header_matches.value.operator == "EXACT" ? header_matches.value.value : null
prefix = header_matches.value.operator == "PREFIX" ? header_matches.value.value : null
}
}
}

}
}

Expand Down
40 changes: 30 additions & 10 deletions modules/lattice-service-listener/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,20 @@ output "default_action" {
}
}

# TODO: Update Docs
# TODO: Support Match
output "rules" {
description = <<EOF
The configuration for default routing action of the service listener.
`type` - The type of default routing action.
`parameters` - The configuration for the parameters of the default routing action. `default_action_parameters` block as defined below.
The list of rules to enable content-based routing to the target groups that make up the service.
`id` - Unique identifier for the listener rule.
`arn` - The ARN for the listener rule.
`priority` - The priority assigned to the listener rule.
`name` - The rule name to describe the purpose of the listener rule.
`conditions` - The rule conditions.
`method` - The condition of HTTP request method.
`path` - The condition of HTTP request path.
`headers` - The condition of HTTP request headers.
`action` - The action for the listener rule.
`type` - The action type for the rule of the service.
`parameters` - The configuration for the parameters of the routing action.
EOF
value = {
for rule in var.rules :
Expand All @@ -64,6 +71,24 @@ output "rules" {
priority = rule.priority
name = aws_vpclattice_listener_rule.this[rule.priority].name

conditions = {
method = aws_vpclattice_listener_rule.this[rule.priority].match[0].http_match[0].method
path = {
value = aws_vpclattice_listener_rule.this[rule.priority].match[0].http_match[0].path_match[0].match[0].exact != null ? aws_vpclattice_listener_rule.this[rule.priority].match[0].http_match[0].path_match[0].match[0].exact : aws_vpclattice_listener_rule.this[rule.priority].match[0].http_match[0].path_match[0].match[0].prefix
operator = aws_vpclattice_listener_rule.this[rule.priority].match[0].http_match[0].path_match[0].match[0].exact != null ? "EXACT" : "PREFIX"
case_sensitive = aws_vpclattice_listener_rule.this[rule.priority].match[0].http_match[0].path_match[0].case_sensitive
}
headers = [
for header in aws_vpclattice_listener_rule.this[rule.priority].match[0].http_match[0].header_matches :
{
name = header.name
value = header.match[0].exact != null ? header.match[0].exact : (header.match[0].prefix != null ? header.match[0].prefix : header.match[0].contains)
operator = header.match[0].exact != null ? "EXACT" : (header.match[0].prefix != null ? "PREFIX" : "CONTAINS")
case_sensitive = header.case_sensitive
}
]
}

action = {
type = rule.action_type
parameters = rule.action_parameters
Expand All @@ -74,11 +99,6 @@ output "rules" {
}
}[rule.action_type]
}
z = {
for k, v in aws_vpclattice_listener_rule.this[rule.priority] :
k => v
if !contains(["name", "priority", "rule_id", "id", "arn", "listener_identifier", "service_identifier", "tags", "tags_all", "timeouts", "action"], k)
}
}
}
}
Expand Down
43 changes: 36 additions & 7 deletions modules/lattice-service-listener/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,49 @@ variable "default_action_parameters" {
}
}

# TODO: Update Docs
# TODO: Support Match
variable "rules" {
description = <<EOF
(Optional) The configuration for the parameters of the default routing action. `default_action_parameters` block as defined below.
(Optional) `status_code` - The status code of HTTP response. Valid values are `2XX`, `4XX`, or `5XX`. Defaults to `503`. Only supported if `default_action_type` is `FIXED_RESPONSE`.
(Required) `targets` - A list of target configurations to route traffic. To route to a single target group, use `default_action_type` as `FORWARD`. Only supported if `default_action_type` is `WEIGHTED_FORWARD`. Each item of `targets` block as defined below.
(Required) `target_group` - The ARN of the target group to which to route traffic.
(Optional) `weight` - The weight to use routing traffic to `target_group`. Valid value is `0` to `999`. Defaults to `1`.
(Optional) A list of rules to enable content-based routing to the target groups that make up the service. Each rule consists of a priority, one or more actions, and one or more conditions.
Each block of `rules` block as defined below.
(Required) `priority` - The priority assigned to the rule. Each rule for a specific listener must have a unique priority. The lower the priority number the higher the priority.
(Optional) `name` - A rule name can describe the purpose of the rule or the type of traffic it is intended to handle. Rule names can't be changed after creation. Defaults to `$(service)-$(priority)`.
(Required) `conditions` - The rule conditions. `conditions` block as defined below.
(Optional) `method` - The condition of HTTP request method. Valid values are `GET`, `HEAD`, `POST`, `PUT`, `DELETE`, `CONNECT`, `OPTIONS`, `TRACE`, `PATCH`.
(Required) `path` - The condition of HTTP request path. `path` block as defined below.
(Required) `value` - The path pattern. The pattern must start with `/`.
(Optional) `operator` - The operator that you want to use to determine whether an HTTP request path matches the conditions. Valid values are `EXACT`, `PREFIX`. Defaults to `PREFIX`.
(Optional) `case_sensitive` - Whether to match the `value` condition using a case-sensitive match. Defaults to `false`.
(Optional) `headers` - The condition of HTTP request headers. Each block of `headers` as defined below.
(Required) `name` - The name of the HTTP header field.
(Required) `value` - The value of the HTTP header field.
(Optional) `operator` - The operator that you want to use to determine whether an HTTP header matches the conditions. Valid values are `EXACT`, `PREFIX`, `CONTAINS`. Defaults to `EXACT`.
(Optional) `case_sensitive` - Whether to match the `value` condition using a case-sensitive match. Defaults to `false`.
(Required) `action_type` - The action type for the rule of the service. Valid values are `FORWARD`, `FIXED_RESPONSE`.
(Optional) `action_parameters` - The configuration for the parameters of the routing action. `action_parameters` block as defined below.
(Optional) `status_code` - Custom HTTP status code to drop client requests and return a custom HTTP response. Valid values are `404`. Only supported if `action_type` is `FIXED_RESPONSE`.
(Optional) `destinations` - A list of one or more target groups to route traffic. Only supported if `action_type` is `FORWARD`. Each item of `destinations` block as defined below.
(Required) `target_group` - The ID or ARN of the target group to which to route traffic.
(Optional) `weight` - The weight to use routing traffic to `target_group`. how requests are distributed to the target group. Only required if you specify multiple target groups for a forward action. For example, if you specify two target groups, one with a weight of 10 and the other with a weight of 20, the target group with a weight of 20 receives twice as many requests as the other target group. Valid value is `0` to `999`. Defaults to `100`.
EOF
type = list(object({
priority = number
name = optional(string)

conditions = object({
method = optional(string)
path = object({
value = string
operator = optional(string, "PREFIX")
case_sensitive = optional(bool, false)
})
headers = optional(list(object({
name = string
value = string
operator = optional(string, "EXACT")
case_sensitive = optional(bool, false)
})), [])
})

action_type = string
action_parameters = optional(object({
status_code = optional(number, 404)
Expand Down
Loading

0 comments on commit 5edff45

Please sign in to comment.