Skip to content

Commit f8303d5

Browse files
committed
tests(examples): do not use anymore default aws vpc
Previously, example tests assumed the presence of a default AWS VPC in every account, which led to intermittent failures when that VPC was deleted, disabled, or locked down with restrictive security rules. This commit create full aws network stack for each examples.
1 parent 5178910 commit f8303d5

File tree

15 files changed

+207
-149
lines changed

15 files changed

+207
-149
lines changed

examples/autoscaling-scheduler-terminate-instances/main.tf

-9
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,6 @@ data "aws_ami" "ubuntu" {
1717
owners = ["099720109477"] # Canonical
1818
}
1919

20-
resource "aws_vpc" "this" {
21-
cidr_block = "10.0.0.0/16"
22-
}
23-
24-
resource "aws_subnet" "this" {
25-
vpc_id = aws_vpc.this.id
26-
cidr_block = "10.0.1.0/24"
27-
}
28-
2920
resource "aws_launch_template" "this" {
3021
name_prefix = "web_config"
3122
image_id = data.aws_ami.ubuntu.id
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
resource "aws_vpc" "this" {
2+
cidr_block = "10.0.0.0/16"
3+
}
4+
5+
resource "aws_subnet" "this" {
6+
vpc_id = aws_vpc.this.id
7+
cidr_block = "10.0.1.0/24"
8+
}

examples/autoscaling-scheduler/main.tf

-9
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,6 @@ data "aws_ami" "ubuntu" {
1717
owners = ["099720109477"] # Canonical
1818
}
1919

20-
resource "aws_vpc" "this" {
21-
cidr_block = "10.0.0.0/16"
22-
}
23-
24-
resource "aws_subnet" "this" {
25-
vpc_id = aws_vpc.this.id
26-
cidr_block = "10.0.1.0/24"
27-
}
28-
2920
resource "aws_launch_template" "this" {
3021
name_prefix = "web_config"
3122
image_id = data.aws_ami.ubuntu.id

examples/autoscaling-scheduler/terraform.tftest.hcl

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ run "create_test_infrastructure" {
3333
}
3434

3535
assert {
36-
condition = module.test-execution.asg_instance_scheduled_state == "stopped"
36+
condition = module.test-execution.asg_instance_scheduled_state == "stopped" || module.test-execution.asg_instance_scheduled_state == "stopping"
3737
error_message = "Autoscaling group instance should be stopped"
3838
}
3939

examples/autoscaling-scheduler/vpc.tf

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
resource "aws_vpc" "this" {
2+
cidr_block = "10.0.0.0/16"
3+
}
4+
5+
resource "aws_subnet" "this" {
6+
vpc_id = aws_vpc.this.id
7+
cidr_block = "10.0.1.0/24"
8+
}

examples/ecs-scheduler/cloudwatch_alarm.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ resource "aws_cloudwatch_metric_alarm" "service_count" {
99
threshold = "2"
1010
alarm_description = "Less than 2 Running Service on cluster"
1111
dimensions = {
12-
ClusterName = aws_ecs_cluster.hello.id
12+
ClusterName = aws_ecs_cluster.this.id
1313
}
1414

1515
tags = {

examples/ecs-scheduler/ecs.tf

+21-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
resource "aws_ecs_cluster" "hello" {
2-
name = "ecs-scheduler-test-cluster"
1+
resource "aws_ecs_cluster" "this" {
2+
name = "test-ecs-cluster-${random_pet.suffix.id}"
33

44
setting {
55
name = "containerInsights"
66
value = "disabled"
77
}
88
}
99

10-
resource "aws_ecs_service" "hello" {
10+
resource "aws_ecs_service" "to_scheduled" {
1111
name = "test-to-stop-${random_pet.suffix.id}"
12-
cluster = aws_ecs_cluster.hello.id
13-
task_definition = aws_ecs_task_definition.hello.arn
12+
cluster = aws_ecs_cluster.this.id
13+
task_definition = aws_ecs_task_definition.this.arn
1414
desired_count = 1
1515
launch_type = "FARGATE"
1616

@@ -29,10 +29,10 @@ resource "aws_ecs_service" "hello" {
2929
}
3030
}
3131

32-
resource "aws_ecs_service" "hello-false" {
32+
resource "aws_ecs_service" "not_to_scheduled" {
3333
name = "test-not-to-stop-${random_pet.suffix.id}"
34-
cluster = aws_ecs_cluster.hello.id
35-
task_definition = aws_ecs_task_definition.hello.arn
34+
cluster = aws_ecs_cluster.this.id
35+
task_definition = aws_ecs_task_definition.this.arn
3636
desired_count = 1
3737
launch_type = "FARGATE"
3838

@@ -51,8 +51,8 @@ resource "aws_ecs_service" "hello-false" {
5151
}
5252
}
5353

54-
resource "aws_ecs_task_definition" "hello" {
55-
family = "test--${random_pet.suffix.id}"
54+
resource "aws_ecs_task_definition" "this" {
55+
family = "test-${random_pet.suffix.id}"
5656

5757
# Refer to https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-cpu-memory-error.html
5858
# for cpu and memory values
@@ -68,8 +68,17 @@ resource "aws_ecs_task_definition" "hello" {
6868
container_definitions = jsonencode([
6969
{
7070
name = "hello-world-rest"
71-
image = "public.ecr.aws/docker/library/busybox:latest"
71+
image = "docker.io/library/nginx:alpine"
72+
cpu = 10
73+
memory = 128
7274
essential = true
75+
portMappings = [
76+
{
77+
containerPort = 80
78+
hostPort = 80
79+
protocol = "tcp"
80+
}
81+
]
7382
}
7483
])
75-
}
84+
}

examples/instance-scheduler/main.tf

+10-6
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,23 @@ data "aws_ami" "ubuntu" {
1717
}
1818

1919
resource "aws_instance" "scheduled" {
20-
count = "3"
21-
ami = data.aws_ami.ubuntu.id
22-
instance_type = "t2.micro"
20+
count = "3"
21+
ami = data.aws_ami.ubuntu.id
22+
instance_type = "t2.micro"
23+
subnet_id = aws_subnet.public.id
24+
vpc_security_group_ids = [aws_security_group.instance.id]
2325
tags = {
2426
tostop = "true"
2527
Name = "ec2-to-scheduled-${random_pet.suffix.id}-${count.index}"
2628
}
2729
}
2830

2931
resource "aws_instance" "not_scheduled" {
30-
count = "2"
31-
ami = data.aws_ami.ubuntu.id
32-
instance_type = "t2.micro"
32+
count = "2"
33+
ami = data.aws_ami.ubuntu.id
34+
instance_type = "t2.micro"
35+
subnet_id = aws_subnet.public.id
36+
vpc_security_group_ids = [aws_security_group.instance.id]
3337
tags = {
3438
tostop = "false"
3539
Name = "ec2-not-to-scheduled-${random_pet.suffix.id}-${count.index}"

examples/instance-scheduler/terraform.tftest.hcl

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ run "create_test_infrastructure" {
1212
}
1313

1414
assert {
15-
condition = module.test-execution.instance_1_scheduled_state == "stopped"
15+
condition = module.test-execution.instance_1_scheduled_state == "stopped" || module.test-execution.instance_1_scheduled_state == "stopping"
1616
error_message = "Virtual machine 1 to stop is not stopped"
1717
}
1818

1919
assert {
20-
condition = module.test-execution.instance_2_scheduled_state == "stopped"
20+
condition = module.test-execution.instance_2_scheduled_state == "stopped" || module.test-execution.instance_2_scheduled_state == "stopping"
2121
error_message = "Virtual machine 2 to stop is not stopped"
2222
}
2323

2424
assert {
25-
condition = module.test-execution.instance_3_scheduled_state == "stopped"
25+
condition = module.test-execution.instance_3_scheduled_state == "stopped" || module.test-execution.instance_3_scheduled_state == "stopping"
2626
error_message = "Virtual machine 3 to stop is not stopped"
2727
}
2828

examples/instance-scheduler/test-execution/main.tf

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,38 @@ resource "aws_lambda_invocation" "this" {
1313
depends_on = [time_sleep.before_stop_wait_30_seconds]
1414
}
1515

16-
resource "time_sleep" "after_stop_wait_60_seconds" {
17-
create_duration = "60s"
16+
resource "time_sleep" "after_stop_wait_30_seconds" {
17+
create_duration = "30s"
1818

1919
depends_on = [aws_lambda_invocation.this]
2020
}
2121

2222
data "aws_instance" "instance_1_to_scheduled_id" {
2323
instance_id = var.instance_1_to_scheduled_id
2424

25-
depends_on = [time_sleep.after_stop_wait_60_seconds]
25+
depends_on = [time_sleep.after_stop_wait_30_seconds]
2626
}
2727

2828
data "aws_instance" "instance_2_to_scheduled_id" {
2929
instance_id = var.instance_2_to_scheduled_id
3030

31-
depends_on = [time_sleep.after_stop_wait_60_seconds]
31+
depends_on = [time_sleep.after_stop_wait_30_seconds]
3232
}
3333

3434
data "aws_instance" "instance_3_to_scheduled_id" {
3535
instance_id = var.instance_3_to_scheduled_id
3636

37-
depends_on = [time_sleep.after_stop_wait_60_seconds]
37+
depends_on = [time_sleep.after_stop_wait_30_seconds]
3838
}
3939

4040
data "aws_instance" "instance_1_not_to_scheduled_id" {
4141
instance_id = var.instance_1_not_to_scheduled_id
4242

43-
depends_on = [time_sleep.after_stop_wait_60_seconds]
43+
depends_on = [time_sleep.after_stop_wait_30_seconds]
4444
}
4545

4646
data "aws_instance" "instance_2_not_to_scheduled_id" {
4747
instance_id = var.instance_2_not_to_scheduled_id
4848

49-
depends_on = [time_sleep.after_stop_wait_60_seconds]
49+
depends_on = [time_sleep.after_stop_wait_30_seconds]
5050
}

examples/instance-scheduler/vpc.tf

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
resource "aws_vpc" "main" {
2+
cidr_block = "10.0.0.0/16"
3+
enable_dns_hostnames = true
4+
enable_dns_support = true
5+
}
6+
7+
resource "aws_subnet" "public" {
8+
vpc_id = aws_vpc.main.id
9+
cidr_block = "10.0.1.0/24"
10+
map_public_ip_on_launch = true
11+
availability_zone = "${data.aws_region.current.name}a"
12+
}
13+
14+
resource "aws_internet_gateway" "main" {
15+
vpc_id = aws_vpc.main.id
16+
}
17+
18+
resource "aws_route_table" "public" {
19+
vpc_id = aws_vpc.main.id
20+
21+
route {
22+
cidr_block = "0.0.0.0/0"
23+
gateway_id = aws_internet_gateway.main.id
24+
}
25+
}
26+
27+
resource "aws_route_table_association" "public" {
28+
subnet_id = aws_subnet.public.id
29+
route_table_id = aws_route_table.public.id
30+
}
31+
32+
resource "aws_security_group" "instance" {
33+
name = "scheduler-test-instance-sg"
34+
description = "Security group for test instances"
35+
vpc_id = aws_vpc.main.id
36+
37+
ingress {
38+
from_port = 22
39+
to_port = 22
40+
protocol = "tcp"
41+
cidr_blocks = ["0.0.0.0/0"]
42+
}
43+
44+
egress {
45+
from_port = 0
46+
to_port = 0
47+
protocol = "-1"
48+
cidr_blocks = ["0.0.0.0/0"]
49+
}
50+
}

examples/rds-scheduler/main.tf

-24
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,6 @@
11
# Terraform rds with lambda scheduler
22
resource "random_pet" "suffix" {}
33

4-
# Get aws availability zones
5-
data "aws_availability_zones" "available" {}
6-
7-
resource "aws_vpc" "this" {
8-
cidr_block = "10.103.0.0/16"
9-
}
10-
11-
resource "aws_subnet" "primary" {
12-
availability_zone = data.aws_availability_zones.available.names[0]
13-
vpc_id = aws_vpc.this.id
14-
cidr_block = "10.103.98.0/24"
15-
}
16-
17-
resource "aws_subnet" "secondary" {
18-
availability_zone = data.aws_availability_zones.available.names[1]
19-
vpc_id = aws_vpc.this.id
20-
cidr_block = "10.103.99.0/24"
21-
}
22-
23-
resource "aws_db_subnet_group" "aurora" {
24-
name = "aurora-subnet-${random_pet.suffix.id}"
25-
subnet_ids = [aws_subnet.primary.id, aws_subnet.secondary.id]
26-
}
27-
284
resource "aws_rds_cluster" "aurora_scheduled" {
295
cluster_identifier = "test-to-stop-aurora-cluster-${random_pet.suffix.id}"
306
engine = "aurora-mysql"

examples/rds-scheduler/vpc.tf

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
data "aws_availability_zones" "available" {}
2+
3+
resource "aws_vpc" "this" {
4+
cidr_block = "10.103.0.0/16"
5+
}
6+
7+
resource "aws_subnet" "primary" {
8+
availability_zone = data.aws_availability_zones.available.names[0]
9+
vpc_id = aws_vpc.this.id
10+
cidr_block = "10.103.98.0/24"
11+
}
12+
13+
resource "aws_subnet" "secondary" {
14+
availability_zone = data.aws_availability_zones.available.names[1]
15+
vpc_id = aws_vpc.this.id
16+
cidr_block = "10.103.99.0/24"
17+
}
18+
19+
resource "aws_db_subnet_group" "aurora" {
20+
name = "aurora-subnet-${random_pet.suffix.id}"
21+
subnet_ids = [aws_subnet.primary.id, aws_subnet.secondary.id]
22+
}

0 commit comments

Comments
 (0)