Skip to content

Commit ac8f420

Browse files
taufortbryantbiggs
andauthored
feat: Allow specifying VPC ID used by service security group in lieu of deriving from subnets provided (#353)
* feat: Add new vpc_id input Sometimes, it happens that Terraform tries to recreate the security group of the ECS service whereas the VPC did not actually change. To avoid this issue, let's use the dependency inversion principle (described here https://developer.hashicorp.com/terraform/language/modules/develop/composition#dependency-inversion) by passing the VPC ID as an input. * fix: Update logic and variable definition --------- Co-authored-by: Bryant Biggs <bryantbiggs@gmail.com>
1 parent 9388a83 commit ac8f420

File tree

7 files changed

+13
-3
lines changed

7 files changed

+13
-3
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.100.0
3+
rev: v1.101.0
44
hooks:
55
- id: terraform_wrapper_module_for_each
66
- id: terraform_fmt

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ module "ecs" {
119119
}
120120
121121
subnet_ids = ["subnet-abcde012", "subnet-bcde012a", "subnet-fghi345a"]
122+
122123
security_group_ingress_rules = {
123124
alb_3000 = {
124125
description = "Service port"

examples/complete/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ module "ecs" {
177177
]
178178

179179
subnet_ids = module.vpc.private_subnets
180+
vpc_id = module.vpc.vpc_id
180181
availability_zone_rebalancing = "ENABLED"
181182
security_group_ingress_rules = {
182183
alb_3000 = {

modules/service/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ module "ecs_service" {
342342
| <a name="input_triggers"></a> [triggers](#input\_triggers) | Map of arbitrary keys and values that, when changed, will trigger an in-place update (redeployment). Useful with `timestamp()` | `map(string)` | `null` | no |
343343
| <a name="input_volume"></a> [volume](#input\_volume) | Configuration block for volumes that containers in your task may use | <pre>map(object({<br/> configure_at_launch = optional(bool)<br/> docker_volume_configuration = optional(object({<br/> autoprovision = optional(bool)<br/> driver = optional(string)<br/> driver_opts = optional(map(string))<br/> labels = optional(map(string))<br/> scope = optional(string)<br/> }))<br/> efs_volume_configuration = optional(object({<br/> authorization_config = optional(object({<br/> access_point_id = optional(string)<br/> iam = optional(string)<br/> }))<br/> file_system_id = string<br/> root_directory = optional(string)<br/> transit_encryption = optional(string)<br/> transit_encryption_port = optional(number)<br/> }))<br/> fsx_windows_file_server_volume_configuration = optional(object({<br/> authorization_config = optional(object({<br/> credentials_parameter = string<br/> domain = string<br/> }))<br/> file_system_id = string<br/> root_directory = string<br/> }))<br/> host_path = optional(string)<br/> name = optional(string)<br/> }))</pre> | `null` | no |
344344
| <a name="input_volume_configuration"></a> [volume\_configuration](#input\_volume\_configuration) | Configuration for a volume specified in the task definition as a volume that is configured at launch time | <pre>object({<br/> name = string<br/> managed_ebs_volume = object({<br/> encrypted = optional(bool)<br/> file_system_type = optional(string)<br/> iops = optional(number)<br/> kms_key_id = optional(string)<br/> size_in_gb = optional(number)<br/> snapshot_id = optional(string)<br/> tag_specifications = optional(list(object({<br/> propagate_tags = optional(string, "TASK_DEFINITION")<br/> resource_type = string<br/> tags = optional(map(string))<br/> })))<br/> throughput = optional(number)<br/> volume_type = optional(string)<br/> })<br/> })</pre> | `null` | no |
345+
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | The VPC ID where to deploy the task or service. If not provided, the VPC ID is derived from the subnets provided | `string` | `null` | no |
345346
| <a name="input_vpc_lattice_configurations"></a> [vpc\_lattice\_configurations](#input\_vpc\_lattice\_configurations) | The VPC Lattice configuration for your service that allows Lattice to connect, secure, and monitor your service across multiple accounts and VPCs | <pre>object({<br/> role_arn = string<br/> target_group_arn = string<br/> port_name = string<br/> })</pre> | `null` | no |
346347
| <a name="input_wait_for_steady_state"></a> [wait\_for\_steady\_state](#input\_wait\_for\_steady\_state) | If true, Terraform will wait for the service to reach a steady state before continuing. Default is `false` | `bool` | `null` | no |
347348
| <a name="input_wait_until_stable"></a> [wait\_until\_stable](#input\_wait\_until\_stable) | Whether terraform should wait until the task set has reached `STEADY_STATE` | `bool` | `null` | no |

modules/service/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,7 +1638,7 @@ locals {
16381638
}
16391639

16401640
data "aws_subnet" "this" {
1641-
count = local.create_security_group ? 1 : 0
1641+
count = local.create_security_group && var.vpc_id != null ? 1 : 0
16421642

16431643
region = var.region
16441644

@@ -1653,7 +1653,7 @@ resource "aws_security_group" "this" {
16531653
name = var.security_group_use_name_prefix ? null : local.security_group_name
16541654
name_prefix = var.security_group_use_name_prefix ? "${local.security_group_name}-" : null
16551655
description = var.security_group_description
1656-
vpc_id = data.aws_subnet.this[0].vpc_id
1656+
vpc_id = try(data.aws_subnet.this[0].vpc_id, var.vpc_id)
16571657

16581658
tags = merge(
16591659
var.tags,

modules/service/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ variable "subnet_ids" {
209209
nullable = false
210210
}
211211

212+
variable "vpc_id" {
213+
description = "The VPC ID where to deploy the task or service. If not provided, the VPC ID is derived from the subnets provided"
214+
type = string
215+
default = null
216+
}
217+
212218
variable "ordered_placement_strategy" {
213219
description = "Service level strategy rules that are taken into consideration during task placement. List from top to bottom in order of precedence"
214220
type = map(object({

wrappers/service/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ module "wrapper" {
138138
triggers = try(each.value.triggers, var.defaults.triggers, null)
139139
volume = try(each.value.volume, var.defaults.volume, null)
140140
volume_configuration = try(each.value.volume_configuration, var.defaults.volume_configuration, null)
141+
vpc_id = try(each.value.vpc_id, var.defaults.vpc_id, null)
141142
vpc_lattice_configurations = try(each.value.vpc_lattice_configurations, var.defaults.vpc_lattice_configurations, null)
142143
wait_for_steady_state = try(each.value.wait_for_steady_state, var.defaults.wait_for_steady_state, null)
143144
wait_until_stable = try(each.value.wait_until_stable, var.defaults.wait_until_stable, null)

0 commit comments

Comments
 (0)