Skip to content

Commit

Permalink
Create autoscaling target before policy (#36)
Browse files Browse the repository at this point in the history
## what
* Make autoscaling policy dependent of target
  • Loading branch information
goruha authored Jul 3, 2024
1 parent 6b8f040 commit 6a2cf59
Show file tree
Hide file tree
Showing 7 changed files with 852 additions and 384 deletions.
16 changes: 8 additions & 8 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ provider "aws" {
}

module "vpc" {
source = "cloudposse/vpc/aws"
version = "0.18.0"
cidr_block = var.vpc_cidr_block
context = module.this.context
source = "cloudposse/vpc/aws"
version = "2.1.1"
ipv4_primary_cidr_block = var.vpc_cidr_block
context = module.this.context
}

module "subnets" {
source = "cloudposse/dynamic-subnets/aws"
version = "0.32.0"
version = "2.4.2"
availability_zones = var.availability_zones
vpc_id = module.vpc.vpc_id
igw_id = module.vpc.igw_id
cidr_block = module.vpc.vpc_cidr_block
igw_id = [module.vpc.igw_id]
ipv4_cidr_block = [module.vpc.vpc_cidr_block]
nat_gateway_enabled = true
nat_instance_enabled = false
context = module.this.context
Expand All @@ -28,7 +28,7 @@ resource "aws_ecs_cluster" "default" {

module "container_definition" {
source = "cloudposse/ecs-container-definition/aws"
version = "0.45.2"
version = "0.61.1"
container_name = var.container_name
container_image = var.container_image
container_memory = var.container_memory
Expand Down
2 changes: 1 addition & 1 deletion examples/complete/versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 0.13.0"
required_version = ">= 1"
required_providers {
aws = {
source = "hashicorp/aws"
Expand Down
4 changes: 4 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ resource "aws_appautoscaling_policy" "up" {
scaling_adjustment = var.scale_up_adjustment
}
}

depends_on = [aws_appautoscaling_target.default]
}

resource "aws_appautoscaling_policy" "down" {
Expand All @@ -57,4 +59,6 @@ resource "aws_appautoscaling_policy" "down" {
scaling_adjustment = var.scale_down_adjustment
}
}

depends_on = [aws_appautoscaling_target.default]
}
30 changes: 19 additions & 11 deletions test/src/examples_complete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package test

import (
"encoding/json"
"strings"
"testing"

"github.com/gruntwork-io/terratest/modules/random"
"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/stretchr/testify/assert"
)
Expand All @@ -12,12 +14,18 @@ import (
func TestExamplesComplete(t *testing.T) {
t.Parallel()

randID := strings.ToLower(random.UniqueId())
attributes := []string{randID}

terraformOptions := &terraform.Options{
// The path to where our Terraform code is located
TerraformDir: "../../examples/complete",
Upgrade: true,
// Variables to pass to our Terraform code using -var-file options
VarFiles: []string{"fixtures.us-east-2.tfvars"},
Vars: map[string]interface{}{
"attributes": attributes,
},
}

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

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

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

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

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

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

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

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

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

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

// Run `terraform output` to get the value of an output variable
scaleDownPolicyArn := terraform.Output(t, terraformOptions, "scale_down_policy_arn")
// Verify we're getting back the outputs we expect
assert.Contains(t, scaleDownPolicyArn, "eg-test-ecs-cloudwatch-autoscaling-down")
assert.Contains(t, scaleDownPolicyArn, "eg-test-ecs-cloudwatch-autoscaling-" + randID + "-down")
}
62 changes: 59 additions & 3 deletions test/src/go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,64 @@
module github.com/cloudposse/terraform-aws-ecs-cloudwatch-autoscaling

go 1.14
go 1.21

toolchain go1.21.4

require (
github.com/gruntwork-io/terratest v0.46.16
github.com/stretchr/testify v1.8.4
)

require (
github.com/gruntwork-io/terratest v0.30.23
github.com/stretchr/testify v1.6.1
cloud.google.com/go v0.110.0 // indirect
cloud.google.com/go/compute v1.19.1 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v0.13.0 // indirect
cloud.google.com/go/storage v1.29.0 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/aws/aws-sdk-go v1.44.122 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.7.1 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-getter v1.7.5 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/hcl/v2 v2.9.1 // indirect
github.com/hashicorp/terraform-json v0.13.0 // indirect
github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mattn/go-zglob v0.0.2-0.20190814121620-e3c945676326 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/tmccombs/hcl2json v0.3.3 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/zclconf/go-cty v1.9.1 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.114.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/grpc v1.56.3 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 6a2cf59

Please sign in to comment.