Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/autoscaling_group Add support for service linked roles #3812

Merged
merged 3 commits into from
Mar 23, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions aws/resource_aws_autoscaling_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ func resourceAwsAutoscalingGroup() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeMap},
ConflictsWith: []string{"tag"},
},

"service_linked_role_arn": {
Type: schema.TypeString,
Optional: true,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you'll need to add Computed: true, for this new attribute as well since I'm getting all test failures with:

=== RUN   TestAccAWSAutoScalingGroup_ALB_TargetGroups
--- FAIL: TestAccAWSAutoScalingGroup_ALB_TargetGroups (78.41s)
    testing.go:518: Step 0 error: After applying this step, the plan was not empty:
        
        DIFF:
        
        UPDATE: aws_autoscaling_group.bar
          service_linked_role_arn: "arn:aws:iam::*******:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling" => ""

=== RUN   TestAccAWSAutoScalingGroup_ALB_TargetGroups_ELBCapacity
--- FAIL: TestAccAWSAutoScalingGroup_ALB_TargetGroups_ELBCapacity (336.79s)
    testing.go:518: Step 0 error: After applying this step, the plan was not empty:
        
        DIFF:
        
        UPDATE: aws_autoscaling_group.bar
          service_linked_role_arn: "arn:aws:iam::*******:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling" => ""

...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, interesting the tests pass for me both with and without Computed: true... I'll make the amendment in the moment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be related to if an AWS account has already had the service linked role automatically created by AWS. Running the acceptance tests again!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ha, here it is:

For existing EC2 Auto Scaling customers, EC2 Auto Scaling will add a default EC2 Auto Scaling service-linked role in your account and your existing Auto Scaling groups will be updated to use this role over the next few weeks.

https://aws.amazon.com/about-aws/whats-new/2018/02/amazon-ec2-auto-scaling-adds-support-for-service-linked-roles/

},
}
}
Expand Down Expand Up @@ -393,6 +398,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 +477,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 +724,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 @@ -172,6 +172,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