Skip to content

Commit

Permalink
Merge pull request #3812 from swestcott/asg-slr
Browse files Browse the repository at this point in the history
r/autoscaling_group Add support for service linked roles
  • Loading branch information
bflad authored Mar 23, 2018
2 parents 301c19c + 0a97a69 commit cba739f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
15 changes: 15 additions & 0 deletions aws/resource_aws_autoscaling_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ func resourceAwsAutoscalingGroup() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeMap},
ConflictsWith: []string{"tag"},
},

"service_linked_role_arn": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -393,6 +399,10 @@ func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{})
createOpts.TargetGroupARNs = expandStringList(v.(*schema.Set).List())
}

if v, ok := d.GetOk("service_linked_role_arn"); ok {
createOpts.ServiceLinkedRoleARN = aws.String(v.(string))
}

log.Printf("[DEBUG] AutoScaling Group create configuration: %#v", createOpts)
_, err := conn.CreateAutoScalingGroup(&createOpts)
if err != nil {
Expand Down Expand Up @@ -468,6 +478,7 @@ func resourceAwsAutoscalingGroupRead(d *schema.ResourceData, meta interface{}) e
d.Set("max_size", g.MaxSize)
d.Set("placement_group", g.PlacementGroup)
d.Set("name", g.AutoScalingGroupName)
d.Set("service_linked_role_arn", g.ServiceLinkedRoleARN)

var tagList, tagsList []*autoscaling.TagDescription
var tagOk, tagsOk bool
Expand Down Expand Up @@ -714,6 +725,10 @@ func resourceAwsAutoscalingGroupUpdate(d *schema.ResourceData, meta interface{})
}
}

if d.HasChange("service_linked_role_arn") {
opts.ServiceLinkedRoleARN = aws.String(d.Get("service_linked_role_arn").(string))
}

return resourceAwsAutoscalingGroupRead(d, meta)
}

Expand Down
54 changes: 54 additions & 0 deletions aws/resource_aws_autoscaling_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,26 @@ func TestAccAWSAutoScalingGroup_withMetrics(t *testing.T) {
})
}

func TestAccAWSAutoScalingGroup_serviceLinkedRoleARN(t *testing.T) {
var group autoscaling.Group

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSAutoScalingGroupConfig_withServiceLinkedRoleARN,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group),
resource.TestCheckResourceAttrSet(
"aws_autoscaling_group.bar", "service_linked_role_arn"),
),
},
},
})
}

func TestAccAWSAutoScalingGroup_ALB_TargetGroups(t *testing.T) {
var group autoscaling.Group
var tg elbv2.TargetGroup
Expand Down Expand Up @@ -1434,6 +1454,40 @@ resource "aws_autoscaling_group" "bar" {
`, name, name)
}

const testAccAWSAutoScalingGroupConfig_withServiceLinkedRoleARN = `
data "aws_ami" "test_ami" {
most_recent = true
filter {
name = "owner-alias"
values = ["amazon"]
}
filter {
name = "name"
values = ["amzn-ami-hvm-*-x86_64-gp2"]
}
}
data "aws_iam_role" "autoscaling_service_linked_role" {
name = "AWSServiceRoleForAutoScaling"
}
resource "aws_launch_configuration" "foobar" {
image_id = "${data.aws_ami.test_ami.id}"
instance_type = "t2.micro"
}
resource "aws_autoscaling_group" "bar" {
availability_zones = ["us-west-2a"]
desired_capacity = 0
max_size = 0
min_size = 0
launch_configuration = "${aws_launch_configuration.foobar.name}"
service_linked_role_arn = "${data.aws_iam_role.autoscaling_service_linked_role.arn}"
}
`

const testAccAWSAutoscalingMetricsCollectionConfig_allMetricsCollected = `
data "aws_ami" "test_ami" {
most_recent = true
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/autoscaling_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ Note that if you suspend either the `Launch` or `Terminate` process types, it ca
* `protect_from_scale_in` (Optional) Allows setting instance protection. The
autoscaling group will not select instances with this setting for terminination
during scale in events.
* `service_linked_role_arn` (Optional) The ARN of the service-linked role that the ASG will use to call other AWS services

Tags support the following:

Expand Down

0 comments on commit cba739f

Please sign in to comment.