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

WIP: Set application autoscaling policy type by the configuration used #3536

Closed
wants to merge 2 commits into from
Closed
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
195 changes: 195 additions & 0 deletions aws/resource_aws_appautoscaling_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,30 @@ func TestAccAWSAppautoScalingPolicy_nestedSchema(t *testing.T) {
})
}

func TestAccAWSAppautoScalingPolicy_targetTrackingEcs(t *testing.T) {
var policy applicationautoscaling.ScalingPolicy

randVpcThirdOctet := acctest.RandIntRange(0, 255)
randName := fmt.Sprintf("tf-acc-appautoscaling-tt-ecs-%s", acctest.RandString(10))
randLoadBalancerName := fmt.Sprintf("tf-acc-appautoscaling-tt-ecs-%s", acctest.RandString(3))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAppautoscalingPolicyDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSAppautoscalingPolicyTargetTrackingEcs(randVpcThirdOctet, randName, randName, randName, randName, randName, randLoadBalancerName, randName, randName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAppautoscalingPolicyExists("aws_appautoscaling_policy.ecs_target_track", &policy),
resource.TestCheckResourceAttr("aws_appautoscaling_policy.ecs_target_track", "name", randName),
resource.TestCheckResourceAttr("aws_appautoscaling_policy.ecs_target_track", "service_namespace", "ecs"),
),
},
},
})
}

func TestAccAWSAppautoScalingPolicy_spotFleetRequest(t *testing.T) {
var policy applicationautoscaling.ScalingPolicy

Expand Down Expand Up @@ -418,6 +442,177 @@ resource "aws_appautoscaling_policy" "foobar_simple" {
`, randClusterName, randPolicyName)
}

func testAccAWSAppautoscalingPolicyTargetTrackingEcs(
randVpcThirdOctet int,
randVpcName string,
randClusterName string,
randEcsTaskDefinitionFamily string,
randIamRoleName string,
randIamPolicyName string,
randLoadBalancerName string,
randServiceName string,
randAutoscalingPolicyName string) string {
return fmt.Sprintf(`
data "aws_availability_zones" "available" {}

resource "aws_vpc" "main" {
cidr_block = "10.42.%d.0/24"

tags {
Name = "%s"
}
}

resource "aws_subnet" "main" {
count = 2
cidr_block = "${cidrsubnet(aws_vpc.main.cidr_block, 2, count.index)}"
availability_zone = "${data.aws_availability_zones.available.names[count.index]}"
vpc_id = "${aws_vpc.main.id}"
}

resource "aws_ecs_cluster" "main" {
name = "%s"
}

resource "aws_ecs_task_definition" "task" {
family = "%s"

container_definitions = <<EOF
[
{
"cpu": 256,
"essential": true,
"image": "ghost:latest",
"memory": 512,
"name": "ghost",
"portMappings": [
{
"containerPort": 2368,
"hostPort": 8080
}
]
}
]
EOF
}

resource "aws_iam_role" "ecs_service" {
name = "%s"

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

resource "aws_iam_role_policy" "ecs_service" {
name = "%s"
role = "${aws_iam_role.ecs_service.name}"

policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ec2:Describe*",
"elasticloadbalancing:DeregisterInstancesFromLoadBalancer",
"elasticloadbalancing:DeregisterTargets",
"elasticloadbalancing:Describe*",
"elasticloadbalancing:RegisterInstancesWithLoadBalancer",
"elasticloadbalancing:RegisterTargets"
],
"Resource": "*"
}
]
}
EOF
}

resource "aws_lb" "main" {
name = "%s"
internal = true
subnets = ["${aws_subnet.main.*.id}"]
}

resource "aws_lb_target_group" "default" {
name = "${aws_lb.main.name}"
port = 80
protocol = "HTTP"
vpc_id = "${aws_vpc.main.id}"
}

resource "aws_lb_listener" "http" {
load_balancer_arn = "${aws_lb.main.id}"
port = "80"
protocol = "HTTP"

default_action {
target_group_arn = "${aws_lb_target_group.default.id}"
type = "forward"
}
}

resource "aws_ecs_service" "service" {
name = "%s"
cluster = "${aws_ecs_cluster.main.id}"
task_definition = "${aws_ecs_task_definition.task.arn}"
desired_count = 1
iam_role = "${aws_iam_role.ecs_service.name}"

load_balancer {
target_group_arn = "${aws_lb_target_group.default.id}"
container_name = "ghost"
container_port = "2368"
}

depends_on = [
"aws_iam_role_policy.ecs_service",
"aws_lb_listener.http",
]
}

resource "aws_appautoscaling_target" "target" {
service_namespace = "ecs"
resource_id = "service/${aws_ecs_cluster.main.name}/${aws_ecs_service.service.name}"
scalable_dimension = "ecs:service:DesiredCount"
min_capacity = 1
max_capacity = 4
}

resource "aws_appautoscaling_policy" "ecs_target_track" {
name = "%s"
policy_type = "TargetTrackingScaling"
service_namespace = "${aws_appautoscaling_target.target.service_namespace}"
resource_id = "${aws_appautoscaling_target.target.resource_id}"
scalable_dimension = "${aws_appautoscaling_target.target.scalable_dimension}"

target_tracking_scaling_policy_configuration {
predefined_metric_specification {
resource_label = "${aws_lb.main.arn_suffix}/${aws_lb_target_group.default.arn_suffix}"
predefined_metric_type = "ALBRequestCountPerTarget"
}

target_value = 42
scale_in_cooldown = 60
scale_out_cooldown = 60
}
}
`, randVpcThirdOctet, randVpcName, randClusterName, randEcsTaskDefinitionFamily, randIamRoleName, randIamPolicyName, randLoadBalancerName, randServiceName, randAutoscalingPolicyName)
}

func testAccAWSAppautoscalingPolicyDynamoDB(
randPolicyName string) string {
return fmt.Sprintf(`
Expand Down