Skip to content

Commit 6a2cf59

Browse files
authored
Create autoscaling target before policy (#36)
## what * Make autoscaling policy dependent of target
1 parent 6b8f040 commit 6a2cf59

File tree

7 files changed

+852
-384
lines changed

7 files changed

+852
-384
lines changed

examples/complete/main.tf

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ provider "aws" {
33
}
44

55
module "vpc" {
6-
source = "cloudposse/vpc/aws"
7-
version = "0.18.0"
8-
cidr_block = var.vpc_cidr_block
9-
context = module.this.context
6+
source = "cloudposse/vpc/aws"
7+
version = "2.1.1"
8+
ipv4_primary_cidr_block = var.vpc_cidr_block
9+
context = module.this.context
1010
}
1111

1212
module "subnets" {
1313
source = "cloudposse/dynamic-subnets/aws"
14-
version = "0.32.0"
14+
version = "2.4.2"
1515
availability_zones = var.availability_zones
1616
vpc_id = module.vpc.vpc_id
17-
igw_id = module.vpc.igw_id
18-
cidr_block = module.vpc.vpc_cidr_block
17+
igw_id = [module.vpc.igw_id]
18+
ipv4_cidr_block = [module.vpc.vpc_cidr_block]
1919
nat_gateway_enabled = true
2020
nat_instance_enabled = false
2121
context = module.this.context
@@ -28,7 +28,7 @@ resource "aws_ecs_cluster" "default" {
2828

2929
module "container_definition" {
3030
source = "cloudposse/ecs-container-definition/aws"
31-
version = "0.45.2"
31+
version = "0.61.1"
3232
container_name = var.container_name
3333
container_image = var.container_image
3434
container_memory = var.container_memory

examples/complete/versions.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
terraform {
2-
required_version = ">= 0.13.0"
2+
required_version = ">= 1"
33
required_providers {
44
aws = {
55
source = "hashicorp/aws"

main.tf

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ resource "aws_appautoscaling_policy" "up" {
3838
scaling_adjustment = var.scale_up_adjustment
3939
}
4040
}
41+
42+
depends_on = [aws_appautoscaling_target.default]
4143
}
4244

4345
resource "aws_appautoscaling_policy" "down" {
@@ -57,4 +59,6 @@ resource "aws_appautoscaling_policy" "down" {
5759
scaling_adjustment = var.scale_down_adjustment
5860
}
5961
}
62+
63+
depends_on = [aws_appautoscaling_target.default]
6064
}

test/src/examples_complete_test.go

+19-11
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package test
22

33
import (
44
"encoding/json"
5+
"strings"
56
"testing"
67

8+
"github.com/gruntwork-io/terratest/modules/random"
79
"github.com/gruntwork-io/terratest/modules/terraform"
810
"github.com/stretchr/testify/assert"
911
)
@@ -12,12 +14,18 @@ import (
1214
func TestExamplesComplete(t *testing.T) {
1315
t.Parallel()
1416

17+
randID := strings.ToLower(random.UniqueId())
18+
attributes := []string{randID}
19+
1520
terraformOptions := &terraform.Options{
1621
// The path to where our Terraform code is located
1722
TerraformDir: "../../examples/complete",
1823
Upgrade: true,
1924
// Variables to pass to our Terraform code using -var-file options
2025
VarFiles: []string{"fixtures.us-east-2.tfvars"},
26+
Vars: map[string]interface{}{
27+
"attributes": attributes,
28+
},
2129
}
2230

2331
// At the end of the test, run `terraform destroy` to clean up any resources that were created
@@ -58,22 +66,22 @@ func TestExamplesComplete(t *testing.T) {
5866
// Run `terraform output` to get the value of an output variable
5967
ecsClusterId := terraform.Output(t, terraformOptions, "ecs_cluster_id")
6068
// Verify we're getting back the outputs we expect
61-
assert.Equal(t, "arn:aws:ecs:us-east-2:126450723953:cluster/eg-test-ecs-cloudwatch-autoscaling", ecsClusterId)
69+
assert.Equal(t, "arn:aws:ecs:us-east-2:126450723953:cluster/eg-test-ecs-cloudwatch-autoscaling-" + randID, ecsClusterId)
6270

6371
// Run `terraform output` to get the value of an output variable
6472
ecsClusterArn := terraform.Output(t, terraformOptions, "ecs_cluster_arn")
6573
// Verify we're getting back the outputs we expect
66-
assert.Equal(t, "arn:aws:ecs:us-east-2:126450723953:cluster/eg-test-ecs-cloudwatch-autoscaling", ecsClusterArn)
74+
assert.Equal(t, "arn:aws:ecs:us-east-2:126450723953:cluster/eg-test-ecs-cloudwatch-autoscaling-" + randID, ecsClusterArn)
6775

6876
// Run `terraform output` to get the value of an output variable
6977
ecsExecRolePolicyName := terraform.Output(t, terraformOptions, "ecs_exec_role_policy_name")
7078
// Verify we're getting back the outputs we expect
71-
assert.Equal(t, "eg-test-ecs-cloudwatch-autoscaling-exec", ecsExecRolePolicyName)
79+
assert.Equal(t, "eg-test-ecs-cloudwatch-autoscaling-exec-" + randID, ecsExecRolePolicyName)
7280

7381
// Run `terraform output` to get the value of an output variable
7482
serviceName := terraform.Output(t, terraformOptions, "service_name")
7583
// Verify we're getting back the outputs we expect
76-
assert.Equal(t, "eg-test-ecs-cloudwatch-autoscaling", serviceName)
84+
assert.Equal(t, "eg-test-ecs-cloudwatch-autoscaling-" + randID, serviceName)
7785

7886
// Run `terraform output` to get the value of an output variable
7987
serviceRoleArn := terraform.Output(t, terraformOptions, "service_role_arn")
@@ -85,35 +93,35 @@ func TestExamplesComplete(t *testing.T) {
8593
// Run `terraform output` to get the value of an output variable
8694
taskDefinitionFamily := terraform.Output(t, terraformOptions, "task_definition_family")
8795
// Verify we're getting back the outputs we expect
88-
assert.Equal(t, "eg-test-ecs-cloudwatch-autoscaling", taskDefinitionFamily)
96+
assert.Equal(t, "eg-test-ecs-cloudwatch-autoscaling-" + randID, taskDefinitionFamily)
8997

9098
// Run `terraform output` to get the value of an output variable
9199
taskExecRoleName := terraform.Output(t, terraformOptions, "task_exec_role_name")
92100
// Verify we're getting back the outputs we expect
93-
assert.Equal(t, "eg-test-ecs-cloudwatch-autoscaling-exec", taskExecRoleName)
101+
assert.Equal(t, "eg-test-ecs-cloudwatch-autoscaling-exec-" + randID, taskExecRoleName)
94102

95103
// Run `terraform output` to get the value of an output variable
96104
taskExecRoleArn := terraform.Output(t, terraformOptions, "task_exec_role_arn")
97105
// Verify we're getting back the outputs we expect
98-
assert.Equal(t, "arn:aws:iam::126450723953:role/eg-test-ecs-cloudwatch-autoscaling-exec", taskExecRoleArn)
106+
assert.Equal(t, "arn:aws:iam::126450723953:role/eg-test-ecs-cloudwatch-autoscaling-exec-" + randID, taskExecRoleArn)
99107

100108
// Run `terraform output` to get the value of an output variable
101109
taskRoleName := terraform.Output(t, terraformOptions, "task_role_name")
102110
// Verify we're getting back the outputs we expect
103-
assert.Equal(t, "eg-test-ecs-cloudwatch-autoscaling-task", taskRoleName)
111+
assert.Equal(t, "eg-test-ecs-cloudwatch-autoscaling-task-" + randID, taskRoleName)
104112

105113
// Run `terraform output` to get the value of an output variable
106114
taskRoleArn := terraform.Output(t, terraformOptions, "task_role_arn")
107115
// Verify we're getting back the outputs we expect
108-
assert.Equal(t, "arn:aws:iam::126450723953:role/eg-test-ecs-cloudwatch-autoscaling-task", taskRoleArn)
116+
assert.Equal(t, "arn:aws:iam::126450723953:role/eg-test-ecs-cloudwatch-autoscaling-task-" + randID, taskRoleArn)
109117

110118
// Run `terraform output` to get the value of an output variable
111119
scaleUpPolicyArn := terraform.Output(t, terraformOptions, "scale_up_policy_arn")
112120
// Verify we're getting back the outputs we expect
113-
assert.Contains(t, scaleUpPolicyArn, "eg-test-ecs-cloudwatch-autoscaling-up")
121+
assert.Contains(t, scaleUpPolicyArn, "eg-test-ecs-cloudwatch-autoscaling-" + randID + "-up")
114122

115123
// Run `terraform output` to get the value of an output variable
116124
scaleDownPolicyArn := terraform.Output(t, terraformOptions, "scale_down_policy_arn")
117125
// Verify we're getting back the outputs we expect
118-
assert.Contains(t, scaleDownPolicyArn, "eg-test-ecs-cloudwatch-autoscaling-down")
126+
assert.Contains(t, scaleDownPolicyArn, "eg-test-ecs-cloudwatch-autoscaling-" + randID + "-down")
119127
}

test/src/go.mod

+59-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,64 @@
11
module github.com/cloudposse/terraform-aws-ecs-cloudwatch-autoscaling
22

3-
go 1.14
3+
go 1.21
4+
5+
toolchain go1.21.4
6+
7+
require (
8+
github.com/gruntwork-io/terratest v0.46.16
9+
github.com/stretchr/testify v1.8.4
10+
)
411

512
require (
6-
github.com/gruntwork-io/terratest v0.30.23
7-
github.com/stretchr/testify v1.6.1
13+
cloud.google.com/go v0.110.0 // indirect
14+
cloud.google.com/go/compute v1.19.1 // indirect
15+
cloud.google.com/go/compute/metadata v0.2.3 // indirect
16+
cloud.google.com/go/iam v0.13.0 // indirect
17+
cloud.google.com/go/storage v1.29.0 // indirect
18+
github.com/agext/levenshtein v1.2.3 // indirect
19+
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
20+
github.com/aws/aws-sdk-go v1.44.122 // indirect
21+
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
22+
github.com/davecgh/go-spew v1.1.1 // indirect
23+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
24+
github.com/golang/protobuf v1.5.3 // indirect
25+
github.com/google/go-cmp v0.5.9 // indirect
26+
github.com/google/uuid v1.3.0 // indirect
27+
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
28+
github.com/googleapis/gax-go/v2 v2.7.1 // indirect
29+
github.com/hashicorp/errwrap v1.0.0 // indirect
30+
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
31+
github.com/hashicorp/go-getter v1.7.5 // indirect
32+
github.com/hashicorp/go-multierror v1.1.0 // indirect
33+
github.com/hashicorp/go-safetemp v1.0.0 // indirect
34+
github.com/hashicorp/go-version v1.6.0 // indirect
35+
github.com/hashicorp/hcl/v2 v2.9.1 // indirect
36+
github.com/hashicorp/terraform-json v0.13.0 // indirect
37+
github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a // indirect
38+
github.com/jmespath/go-jmespath v0.4.0 // indirect
39+
github.com/klauspost/compress v1.15.11 // indirect
40+
github.com/kr/pretty v0.3.1 // indirect
41+
github.com/mattn/go-zglob v0.0.2-0.20190814121620-e3c945676326 // indirect
42+
github.com/mitchellh/go-homedir v1.1.0 // indirect
43+
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
44+
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
45+
github.com/pmezard/go-difflib v1.0.0 // indirect
46+
github.com/rogpeppe/go-internal v1.10.0 // indirect
47+
github.com/tmccombs/hcl2json v0.3.3 // indirect
48+
github.com/ulikunitz/xz v0.5.10 // indirect
49+
github.com/zclconf/go-cty v1.9.1 // indirect
50+
go.opencensus.io v0.24.0 // indirect
51+
golang.org/x/crypto v0.21.0 // indirect
52+
golang.org/x/net v0.23.0 // indirect
53+
golang.org/x/oauth2 v0.8.0 // indirect
54+
golang.org/x/sys v0.18.0 // indirect
55+
golang.org/x/text v0.14.0 // indirect
56+
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
57+
google.golang.org/api v0.114.0 // indirect
58+
google.golang.org/appengine v1.6.7 // indirect
59+
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
60+
google.golang.org/grpc v1.56.3 // indirect
61+
google.golang.org/protobuf v1.33.0 // indirect
62+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
63+
gopkg.in/yaml.v3 v3.0.1 // indirect
864
)

0 commit comments

Comments
 (0)