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

Switch aws_appautoscaling_target's role_arn to optional #2889

Merged
merged 3 commits into from
Jan 8, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions aws/resource_aws_appautoscaling_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func resourceAwsAppautoscalingTarget() *schema.Resource {
},
"role_arn": {
Type: schema.TypeString,
Required: true,
Optional: true,
Computed: true,
ForceNew: true,
},
"scalable_dimension": {
Expand All @@ -64,10 +65,13 @@ func resourceAwsAppautoscalingTargetCreate(d *schema.ResourceData, meta interfac
targetOpts.MaxCapacity = aws.Int64(int64(d.Get("max_capacity").(int)))
targetOpts.MinCapacity = aws.Int64(int64(d.Get("min_capacity").(int)))
targetOpts.ResourceId = aws.String(d.Get("resource_id").(string))
targetOpts.RoleARN = aws.String(d.Get("role_arn").(string))
targetOpts.ScalableDimension = aws.String(d.Get("scalable_dimension").(string))
targetOpts.ServiceNamespace = aws.String(d.Get("service_namespace").(string))

if roleArn, exists := d.GetOk("role_arn"); exists {
targetOpts.RoleARN = aws.String(roleArn.(string))
}

log.Printf("[DEBUG] Application autoscaling target create configuration %#v", targetOpts)
var err error
err = resource.Retry(1*time.Minute, func() *resource.RetryError {
Expand Down
229 changes: 50 additions & 179 deletions aws/resource_aws_appautoscaling_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"regexp"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -122,6 +123,30 @@ func TestAccAWSAppautoScalingTarget_multipleTargets(t *testing.T) {
})
}

func TestAccAWSAppautoScalingTarget_optionalRoleArn(t *testing.T) {
var readTarget applicationautoscaling.ScalableTarget

rInt := acctest.RandInt()
tableName := fmt.Sprintf("tf_acc_test_table_%d", rInt)

r, _ := regexp.Compile("arn:aws:iam::.*:role/aws-service-role/dynamodb.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_DynamoDBTable")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAppautoscalingTargetDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSAppautoscalingTarget_optionalRoleArn(tableName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAppautoscalingTargetExists("aws_appautoscaling_target.read", &readTarget),
resource.TestMatchResourceAttr("aws_appautoscaling_target.read", "role_arn", r),
),
},
},
})
}

func testAccCheckAWSAppautoscalingTargetDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).appautoscalingconn

Expand Down Expand Up @@ -192,58 +217,6 @@ func testAccCheckAWSAppautoscalingTargetExists(n string, target *applicationauto
func testAccAWSAppautoscalingTargetConfig(
randClusterName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "autoscale_role" {
Copy link
Member

Choose a reason for hiding this comment

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

It's great to have a test which tests the new behaviour (i.e. default role assigned by AWS), but we should keep existing tests as is - setting custom role should still be working and be tested the same way as before.

name = "autoscalerole%s"
path = "/"

assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "application-autoscaling.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}

resource "aws_iam_role_policy" "autoscale_role_policy" {
name = "autoscalepolicy%s"
role = "${aws_iam_role.autoscale_role.id}"

policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ecs:DescribeServices",
"ecs:UpdateService"
],
"Resource": [
"*"
]
},
{
"Effect": "Allow",
"Action": [
"cloudwatch:DescribeAlarms"
],
"Resource": [
"*"
]
}
]
}
EOF
}

resource "aws_ecs_cluster" "foo" {
name = "%s"
}
Expand Down Expand Up @@ -277,68 +250,15 @@ resource "aws_appautoscaling_target" "bar" {
service_namespace = "ecs"
resource_id = "service/${aws_ecs_cluster.foo.name}/${aws_ecs_service.service.name}"
scalable_dimension = "ecs:service:DesiredCount"
role_arn = "${aws_iam_role.autoscale_role.arn}"
min_capacity = 1
max_capacity = 3
}
`, randClusterName, randClusterName, randClusterName)
`, randClusterName)
}

func testAccAWSAppautoscalingTargetConfigUpdate(
randClusterName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "autoscale_role" {
name = "autoscalerole%s"
path = "/"

assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "application-autoscaling.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}

resource "aws_iam_role_policy" "autoscale_role_policy" {
name = "autoscalepolicy%s"
role = "${aws_iam_role.autoscale_role.id}"

policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ecs:DescribeServices",
"ecs:UpdateService"
],
"Resource": [
"*"
]
},
{
"Effect": "Allow",
"Action": [
"cloudwatch:DescribeAlarms"
],
"Resource": [
"*"
]
}
]
}
EOF
}

resource "aws_ecs_cluster" "foo" {
name = "%s"
}
Expand Down Expand Up @@ -372,11 +292,10 @@ resource "aws_appautoscaling_target" "bar" {
service_namespace = "ecs"
resource_id = "service/${aws_ecs_cluster.foo.name}/${aws_ecs_service.service.name}"
scalable_dimension = "ecs:service:DesiredCount"
role_arn = "${aws_iam_role.autoscale_role.arn}"
min_capacity = 2
max_capacity = 8
}
`, randClusterName, randClusterName, randClusterName)
`, randClusterName)
}

func testAccAWSAppautoscalingTargetEmrClusterConfig(rInt int) string {
Expand Down Expand Up @@ -729,38 +648,10 @@ resource "aws_spot_fleet_request" "test" {
}
}

resource "aws_iam_role" "autoscale_role" {
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "application-autoscaling.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}

resource "aws_iam_role_policy_attachment" "autoscale_role_policy_a" {
role = "${aws_iam_role.autoscale_role.name}"
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole"
}

resource "aws_iam_role_policy_attachment" "autoscale_role_policy_b" {
role = "${aws_iam_role.autoscale_role.name}"
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetAutoscaleRole"
}

resource "aws_appautoscaling_target" "test" {
service_namespace = "ec2"
resource_id = "spot-fleet-request/${aws_spot_fleet_request.test.id}"
scalable_dimension = "ec2:spot-fleet-request:TargetCapacity"
role_arn = "${aws_iam_role.autoscale_role.arn}"
min_capacity = 1
max_capacity = 3
}
Expand All @@ -779,63 +670,43 @@ resource "aws_dynamodb_table" "dynamodb_table_test" {
}
}

resource "aws_iam_role" "autoscale_role" {
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "application-autoscaling.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}

resource "aws_iam_role_policy" "p" {
role = "${aws_iam_role.autoscale_role.name}"
policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"dynamodb:DescribeTable",
"dynamodb:UpdateTable",
"cloudwatch:PutMetricAlarm",
"cloudwatch:DescribeAlarms",
"cloudwatch:DeleteAlarms"
],
"Resource": "*"
}
]
}
POLICY
}

resource "aws_appautoscaling_target" "write" {
service_namespace = "dynamodb"
resource_id = "table/${aws_dynamodb_table.dynamodb_table_test.name}"
scalable_dimension = "dynamodb:table:WriteCapacityUnits"
role_arn = "${aws_iam_role.autoscale_role.arn}"
min_capacity = 1
max_capacity = 10
depends_on = ["aws_iam_role_policy.p"]
}

resource "aws_appautoscaling_target" "read" {
service_namespace = "dynamodb"
resource_id = "table/${aws_dynamodb_table.dynamodb_table_test.name}"
scalable_dimension = "dynamodb:table:ReadCapacityUnits"
role_arn = "${aws_iam_role.autoscale_role.arn}"
min_capacity = 2
max_capacity = 15
depends_on = ["aws_iam_role_policy.p"]
}
`, tableName)
}

func testAccAWSAppautoscalingTarget_optionalRoleArn(tableName string) string {
return fmt.Sprintf(`
resource "aws_dynamodb_table" "dynamodb_table_test" {
name = "%s"
read_capacity = 5
write_capacity = 5
hash_key = "FooKey"
attribute {
name = "FooKey"
type = "S"
}
}

resource "aws_appautoscaling_target" "read" {
service_namespace = "dynamodb"
resource_id = "table/${aws_dynamodb_table.dynamodb_table_test.name}"
scalable_dimension = "dynamodb:table:ReadCapacityUnits"
min_capacity = 2
max_capacity = 15
}
`, tableName)
}
2 changes: 1 addition & 1 deletion website/docs/r/appautoscaling_target.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ The following arguments are supported:
* `max_capacity` - (Required) The max capacity of the scalable target.
* `min_capacity` - (Required) The min capacity of the scalable target.
* `resource_id` - (Required) The resource type and unique identifier string for the resource associated with the scaling policy. Documentation can be found in the `ResourceId` parameter at: [AWS Application Auto Scaling API Reference](http://docs.aws.amazon.com/ApplicationAutoScaling/latest/APIReference/API_RegisterScalableTarget.html#API_RegisterScalableTarget_RequestParameters)
* `role_arn` - (Required) The ARN of the IAM role that allows Application
* `role_arn` - (Optional) The ARN of the IAM role that allows Application
AutoScaling to modify your scalable target on your behalf.
* `scalable_dimension` - (Required) The scalable dimension of the scalable target. Documentation can be found in the `ScalableDimension` parameter at: [AWS Application Auto Scaling API Reference](http://docs.aws.amazon.com/ApplicationAutoScaling/latest/APIReference/API_RegisterScalableTarget.html#API_RegisterScalableTarget_RequestParameters)
* `service_namespace` - (Required) The AWS service namespace of the scalable target. Documentation can be found in the `ServiceNamespace` parameter at: [AWS Application Auto Scaling API Reference](http://docs.aws.amazon.com/ApplicationAutoScaling/latest/APIReference/API_RegisterScalableTarget.html#API_RegisterScalableTarget_RequestParameters)