-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.tf
150 lines (130 loc) · 3.73 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Specify the provider and access details
provider "aws" {
region = "eu-central-1"
profile = var.profile
# Make it faster by skipping something (don't use it in production)
skip_get_ec2_platforms = true
skip_metadata_api_check = true
skip_region_validation = true
skip_credentials_validation = true
skip_requesting_account_id = true
}
resource "random_pet" "random-tag" {
length = 2
}
# Terraform autoscaling group with lambda scheduler
data "aws_ami" "ami" {
most_recent = true
owners = ["137112412989"]
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2",]
}
}
resource "aws_vpc" "vpc" {
cidr_block = "10.0.0.0/16"
}
resource "aws_subnet" "subnet" {
vpc_id = aws_vpc.vpc.id
cidr_block = "10.0.1.0/24"
}
resource "aws_launch_configuration" "launch" {
name = "web_config"
image_id = data.aws_ami.ami.id
instance_type = "t3a.nano"
}
# Create autoscaling group with tag
resource "aws_autoscaling_group" "scheduled" {
count = 3
name = "bar-with-tag-${count.index}"
max_size = 5
min_size = 1
health_check_grace_period = 300
health_check_type = "EC2"
desired_capacity = 1
force_delete = true
launch_configuration = aws_launch_configuration.launch.name
vpc_zone_identifier = [aws_subnet.subnet.id]
tags = [
{
key = "ToStop"
value = "true"
propagate_at_launch = true
},
{
key = "TestTag"
value = random_pet.random-tag.id
propagate_at_launch = true
},
]
}
# Create autoscaling group without tag
resource "aws_autoscaling_group" "not_scheduled" {
count = 2
name = "foo-without-tag-${count.index}"
max_size = 5
min_size = 1
health_check_grace_period = 300
health_check_type = "EC2"
desired_capacity = 1
force_delete = true
launch_configuration = aws_launch_configuration.launch.name
vpc_zone_identifier = [aws_subnet.subnet.id]
tags = [
{
key = "ToStop"
value = "false"
propagate_at_launch = true
},
{
key = "TestTag"
value = random_pet.random-tag.id
propagate_at_launch = true
},
]
}
# Terraform Lambda Scheduler Module
module "infra-stop-nightly" {
source = "../../"
name = "stop-autoscaling"
aws_regions = ["eu-central-1"]
cloudwatch_schedule_expression = "cron(0 17 ? * MON-SUN *)" # UTC
schedule_action = "stop"
spot_schedule = false
ec2_schedule = false
rds_schedule = false
autoscaling_schedule = true
cloudwatch_alarm_schedule = false
resource_tags = [
{
Key = "ToStop"
Value = "true"
},
{
Key = "TestTag"
Value = random_pet.random-tag.id
}
]
}
module "infra-start-daily" {
source = "../../"
name = "start-autoscaling"
aws_regions = ["eu-central-1"]
cloudwatch_schedule_expression = "cron(0 07 ? * MON-SUN *)" # UTC
schedule_action = "start"
spot_schedule = false
ec2_schedule = false
rds_schedule = false
autoscaling_schedule = true
cloudwatch_alarm_schedule = false
resource_tags = [
{
Key = "ToStop"
Value = "true"
},
{
Key = "TestTag"
Value = random_pet.random-tag.id
}
]
}