Skip to content

Commit 7c5fd5a

Browse files
authored
Merge pull request #23786 from kevinscholz/fix-recreate-aws_ecs_service
r/aws_ecs_service: fix `load_balancer` & `service_registries` to update without destroy & recreate
2 parents 265a59e + 676b2fa commit 7c5fd5a

File tree

3 files changed

+50
-43
lines changed

3 files changed

+50
-43
lines changed

.changelog/23786.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
resource/aws_ecs_service: Ensure that `load_balancer` and `service_registries` can be updated in-place
3+
```

internal/service/ecs/service.go

+22-31
Original file line numberDiff line numberDiff line change
@@ -182,31 +182,24 @@ func ResourceService() *schema.Resource {
182182
Optional: true,
183183
Elem: &schema.Resource{
184184
Schema: map[string]*schema.Schema{
185-
"elb_name": {
186-
Type: schema.TypeString,
187-
Optional: true,
188-
ForceNew: true,
189-
},
190-
191-
"target_group_arn": {
192-
Type: schema.TypeString,
193-
Optional: true,
194-
ForceNew: true,
195-
ValidateFunc: verify.ValidARN,
196-
},
197-
198185
"container_name": {
199186
Type: schema.TypeString,
200187
Required: true,
201-
ForceNew: true,
202188
},
203-
204189
"container_port": {
205190
Type: schema.TypeInt,
206191
Required: true,
207-
ForceNew: true,
208192
ValidateFunc: validation.IntBetween(0, 65536),
209193
},
194+
"elb_name": {
195+
Type: schema.TypeString,
196+
Optional: true,
197+
},
198+
"target_group_arn": {
199+
Type: schema.TypeString,
200+
Optional: true,
201+
ValidateFunc: verify.ValidARN,
202+
},
210203
},
211204
},
212205
Set: resourceLoadBalancerHash,
@@ -222,6 +215,11 @@ func ResourceService() *schema.Resource {
222215
MaxItems: 1,
223216
Elem: &schema.Resource{
224217
Schema: map[string]*schema.Schema{
218+
"assign_public_ip": {
219+
Type: schema.TypeBool,
220+
Optional: true,
221+
Default: false,
222+
},
225223
"security_groups": {
226224
Type: schema.TypeSet,
227225
Optional: true,
@@ -234,11 +232,6 @@ func ResourceService() *schema.Resource {
234232
Elem: &schema.Schema{Type: schema.TypeString},
235233
Set: schema.HashString,
236234
},
237-
"assign_public_ip": {
238-
Type: schema.TypeBool,
239-
Optional: true,
240-
Default: false,
241-
},
242235
},
243236
},
244237
},
@@ -248,11 +241,6 @@ func ResourceService() *schema.Resource {
248241
MaxItems: 5,
249242
Elem: &schema.Resource{
250243
Schema: map[string]*schema.Schema{
251-
"type": {
252-
Type: schema.TypeString,
253-
Required: true,
254-
ValidateFunc: validation.StringInSlice(ecs.PlacementStrategyType_Values(), false),
255-
},
256244
"field": {
257245
Type: schema.TypeString,
258246
Optional: true,
@@ -267,6 +255,11 @@ func ResourceService() *schema.Resource {
267255
return strings.EqualFold(old, new)
268256
},
269257
},
258+
"type": {
259+
Type: schema.TypeString,
260+
Required: true,
261+
ValidateFunc: validation.StringInSlice(ecs.PlacementStrategyType_Values(), false),
262+
},
270263
},
271264
},
272265
},
@@ -319,24 +312,20 @@ func ResourceService() *schema.Resource {
319312
Schema: map[string]*schema.Schema{
320313
"container_name": {
321314
Type: schema.TypeString,
322-
ForceNew: true,
323315
Optional: true,
324316
},
325317
"container_port": {
326318
Type: schema.TypeInt,
327-
ForceNew: true,
328319
Optional: true,
329320
ValidateFunc: validation.IntBetween(0, 65536),
330321
},
331322
"port": {
332323
Type: schema.TypeInt,
333-
ForceNew: true,
334324
Optional: true,
335325
ValidateFunc: validation.IntBetween(0, 65536),
336326
},
337327
"registry_arn": {
338328
Type: schema.TypeString,
339-
ForceNew: true,
340329
Required: true,
341330
ValidateFunc: verify.ValidARN,
342331
},
@@ -1081,7 +1070,9 @@ func resourceServiceUpdate(d *schema.ResourceData, meta interface{}) error {
10811070
}
10821071

10831072
if d.HasChange("load_balancer") {
1084-
input.LoadBalancers = expandLoadBalancers(d.Get("load_balancer").([]interface{}))
1073+
if v, ok := d.Get("load_balancer").(*schema.Set); ok && v != nil {
1074+
input.LoadBalancers = expandLoadBalancers(v.List())
1075+
}
10851076
}
10861077

10871078
if d.HasChange("propagate_tags") {

internal/service/ecs/service_test.go

+25-12
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ func TestAccECSService_deploymentCircuitBreaker(t *testing.T) {
557557

558558
// Regression for https://github.com/hashicorp/terraform/issues/3444
559559
func TestAccECSService_loadBalancerChanges(t *testing.T) {
560-
var service ecs.Service
560+
var s1, s2 ecs.Service
561561
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
562562
resourceName := "aws_ecs_service.test"
563563

@@ -570,13 +570,14 @@ func TestAccECSService_loadBalancerChanges(t *testing.T) {
570570
{
571571
Config: testAccServiceLBChangesConfig(rName),
572572
Check: resource.ComposeTestCheckFunc(
573-
testAccCheckServiceExists(resourceName, &service),
573+
testAccCheckServiceExists(resourceName, &s1),
574574
),
575575
},
576576
{
577577
Config: testAccServiceLBChangesModifiedConfig(rName),
578578
Check: resource.ComposeTestCheckFunc(
579-
testAccCheckServiceExists(resourceName, &service),
579+
testAccCheckServiceExists(resourceName, &s2),
580+
testAccCheckServiceNotRecreated(&s2, &s1),
580581
),
581582
},
582583
},
@@ -2789,15 +2790,27 @@ resource "aws_iam_role_policy" "ecs_service" {
27892790
EOF
27902791
}
27912792
2792-
resource "aws_elb" "test" {
2793+
resource "aws_lb_target_group" "test" {
2794+
name = aws_lb.test.name
2795+
port = %[5]d
2796+
protocol = "HTTP"
2797+
vpc_id = aws_vpc.test.id
2798+
}
2799+
2800+
resource "aws_lb" "test" {
2801+
name = %[1]q
27932802
internal = true
27942803
subnets = aws_subnet.test[*].id
2804+
}
27952805
2796-
listener {
2797-
instance_port = %[5]d
2798-
instance_protocol = "http"
2799-
lb_port = 80
2800-
lb_protocol = "http"
2806+
resource "aws_lb_listener" "front_end" {
2807+
load_balancer_arn = aws_lb.test.id
2808+
port = "80"
2809+
protocol = "HTTP"
2810+
2811+
default_action {
2812+
target_group_arn = aws_lb_target_group.test.id
2813+
type = "forward"
28012814
}
28022815
}
28032816
@@ -2809,9 +2822,9 @@ resource "aws_ecs_service" "test" {
28092822
iam_role = aws_iam_role.ecs_service.name
28102823
28112824
load_balancer {
2812-
elb_name = aws_elb.test.id
2813-
container_name = %[3]q
2814-
container_port = %[4]d
2825+
target_group_arn = aws_lb_target_group.test.id
2826+
container_name = %[3]q
2827+
container_port = %[4]d
28152828
}
28162829
28172830
depends_on = [aws_iam_role_policy.ecs_service]

0 commit comments

Comments
 (0)