Skip to content

Commit d7e9a2e

Browse files
jen20radeksimko
authored andcommitted
provider/aws: Set aws_alb security_groups computed (hashicorp#8269)
This commit fixes hashicorp#8264 by making the security_groups attribute on aws_alb resources computed, allowing the default security group assigned by AWS to not generate perpetual plans forcing new resources.
1 parent b2a3104 commit d7e9a2e

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

builtin/providers/aws/resource_aws_alb.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func resourceAwsAlb() *schema.Resource {
3939
"security_groups": {
4040
Type: schema.TypeSet,
4141
Elem: &schema.Schema{Type: schema.TypeString},
42+
Computed: true,
4243
ForceNew: true,
4344
Optional: true,
4445
Set: schema.HashString,

builtin/providers/aws/resource_aws_alb_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,40 @@ func TestAccAWSALB_basic(t *testing.T) {
4444
})
4545
}
4646

47+
// TestAccAWSALB_noSecurityGroup regression tests the issue in #8264,
48+
// where if an ALB is created without a security group, a default one
49+
// is assigned.
50+
func TestAccAWSALB_noSecurityGroup(t *testing.T) {
51+
var conf elbv2.LoadBalancer
52+
albName := fmt.Sprintf("testaccawsalb-nosg-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
53+
54+
resource.Test(t, resource.TestCase{
55+
PreCheck: func() { testAccPreCheck(t) },
56+
IDRefreshName: "aws_alb.alb_test",
57+
Providers: testAccProviders,
58+
CheckDestroy: testAccCheckAWSALBDestroy,
59+
Steps: []resource.TestStep{
60+
{
61+
Config: testAccAWSALBConfig_nosg(albName),
62+
Check: resource.ComposeAggregateTestCheckFunc(
63+
testAccCheckAWSALBExists("aws_alb.alb_test", &conf),
64+
resource.TestCheckResourceAttr("aws_alb.alb_test", "name", albName),
65+
resource.TestCheckResourceAttr("aws_alb.alb_test", "internal", "false"),
66+
resource.TestCheckResourceAttr("aws_alb.alb_test", "subnets.#", "2"),
67+
resource.TestCheckResourceAttr("aws_alb.alb_test", "security_groups.#", "1"),
68+
resource.TestCheckResourceAttr("aws_alb.alb_test", "tags.%", "1"),
69+
resource.TestCheckResourceAttr("aws_alb.alb_test", "tags.TestName", "TestAccAWSALB_basic"),
70+
resource.TestCheckResourceAttr("aws_alb.alb_test", "enable_deletion_protection", "false"),
71+
resource.TestCheckResourceAttr("aws_alb.alb_test", "idle_timeout", "30"),
72+
resource.TestCheckResourceAttrSet("aws_alb.alb_test", "vpc_id"),
73+
resource.TestCheckResourceAttrSet("aws_alb.alb_test", "zone_id"),
74+
resource.TestCheckResourceAttrSet("aws_alb.alb_test", "dns_name"),
75+
),
76+
},
77+
},
78+
})
79+
}
80+
4781
func TestAccAWSALB_accesslogs(t *testing.T) {
4882
var conf elbv2.LoadBalancer
4983
bucketName := fmt.Sprintf("testaccawsalbaccesslogs-%s", acctest.RandStringFromCharSet(6, acctest.CharSetAlphaNum))
@@ -334,3 +368,45 @@ resource "aws_security_group" "alb_test" {
334368
}
335369
}`, albName, bucketName)
336370
}
371+
372+
func testAccAWSALBConfig_nosg(albName string) string {
373+
return fmt.Sprintf(`resource "aws_alb" "alb_test" {
374+
name = "%s"
375+
internal = false
376+
subnets = ["${aws_subnet.alb_test.*.id}"]
377+
378+
idle_timeout = 30
379+
enable_deletion_protection = false
380+
381+
tags {
382+
TestName = "TestAccAWSALB_basic"
383+
}
384+
}
385+
386+
variable "subnets" {
387+
default = ["10.0.1.0/24", "10.0.2.0/24"]
388+
type = "list"
389+
}
390+
391+
data "aws_availability_zones" "available" {}
392+
393+
resource "aws_vpc" "alb_test" {
394+
cidr_block = "10.0.0.0/16"
395+
396+
tags {
397+
TestName = "TestAccAWSALB_basic"
398+
}
399+
}
400+
401+
resource "aws_subnet" "alb_test" {
402+
count = 2
403+
vpc_id = "${aws_vpc.alb_test.id}"
404+
cidr_block = "${element(var.subnets, count.index)}"
405+
map_public_ip_on_launch = true
406+
availability_zone = "${element(data.aws_availability_zones.available.names, count.index)}"
407+
408+
tags {
409+
TestName = "TestAccAWSALB_basic"
410+
}
411+
}`, albName)
412+
}

0 commit comments

Comments
 (0)