-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
220 lines (173 loc) · 5.72 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">=4.6"
}
}
required_version = ">= 1.1.0"
cloud {
organization = "nwmahoney"
workspaces {
name = "gha-tf-aws-ecs-example"
}
}
}
provider "aws" {
region = var.region
}
################################################################################
# Copied from https://github.com/terraform-aws-modules/terraform-aws-ecs/blob/v4.1.3/examples/complete/main.tf
locals {
name = "gha-tf-aws-ecs-example"
user_data = <<-EOT
#!/bin/bash
cat <<'EOF' >> /etc/ecs/ecs.config
ECS_CLUSTER=${local.name}
ECS_LOGLEVEL=debug
EOF
EOT
tags = {
Name = local.name
Repository = "https://github.com/nwmahoney/gha-tf-aws-ecs-example"
}
}
resource "aws_ecr_repository" "app_images" {
name = local.name
}
################################################################################
# ECS Module
################################################################################
module "ecs" {
source = "terraform-aws-modules/ecs/aws"
version = "4.1.3"
cluster_name = local.name
cluster_configuration = {
execute_command_configuration = {
logging = "OVERRIDE"
log_configuration = {
# You can set a simple string and ECS will create the CloudWatch log group for you
# or you can create the resource yourself as shown here to better manage retetion, tagging, etc.
# Embedding it into the module is not trivial and therefore it is externalized
cloud_watch_log_group_name = aws_cloudwatch_log_group.this.name
}
}
}
default_capacity_provider_use_fargate = false
# Capacity provider - Fargate
fargate_capacity_providers = {
FARGATE = {}
FARGATE_SPOT = {}
}
# Capacity provider - autoscaling groups
autoscaling_capacity_providers = {
one = {
auto_scaling_group_arn = module.autoscaling["one"].autoscaling_group_arn
managed_termination_protection = "ENABLED"
managed_scaling = {
maximum_scaling_step_size = 5
minimum_scaling_step_size = 1
status = "ENABLED"
target_capacity = 60
}
default_capacity_provider_strategy = {
weight = 60
base = 20
}
}
two = {
auto_scaling_group_arn = module.autoscaling["two"].autoscaling_group_arn
managed_termination_protection = "ENABLED"
managed_scaling = {
maximum_scaling_step_size = 15
minimum_scaling_step_size = 5
status = "ENABLED"
target_capacity = 90
}
default_capacity_provider_strategy = {
weight = 40
}
}
}
tags = local.tags
}
module "hello_world" {
source = "./service-hello-world"
cluster_id = module.ecs.cluster_id
app_image = "${aws_ecr_repository.app_images.repository_url}:latest"
}
################################################################################
# Supporting Resources
################################################################################
# https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-optimized_AMI.html#ecs-optimized-ami-linux
data "aws_ssm_parameter" "ecs_optimized_ami" {
name = "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended"
}
module "autoscaling" {
source = "terraform-aws-modules/autoscaling/aws"
version = "~> 6.5"
for_each = {
one = {
instance_type = "t3.micro"
}
two = {
instance_type = "t3.small"
}
}
name = "${local.name}-${each.key}"
image_id = jsondecode(data.aws_ssm_parameter.ecs_optimized_ami.value)["image_id"]
instance_type = each.value.instance_type
security_groups = [module.autoscaling_sg.security_group_id]
user_data = base64encode(local.user_data)
ignore_desired_capacity_changes = true
create_iam_instance_profile = true
iam_role_name = local.name
iam_role_description = "ECS role for ${local.name}"
iam_role_policies = {
AmazonEC2ContainerServiceforEC2Role = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
AmazonSSMManagedInstanceCore = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
vpc_zone_identifier = module.vpc.public_subnets
health_check_type = "EC2"
min_size = 0
max_size = 2
desired_capacity = 1
# https://github.com/hashicorp/terraform-provider-aws/issues/12582
autoscaling_group_tags = {
AmazonECSManaged = true
}
# Required for managed_termination_protection = "ENABLED"
protect_from_scale_in = true
tags = local.tags
}
module "autoscaling_sg" {
source = "terraform-aws-modules/security-group/aws"
version = "~> 4.0"
name = local.name
description = "Autoscaling group security group"
vpc_id = module.vpc.vpc_id
ingress_cidr_blocks = ["0.0.0.0/0"]
ingress_rules = ["http-80-tcp", "https-443-tcp"]
egress_rules = ["all-all"]
tags = local.tags
}
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 3.0"
name = local.name
cidr = "10.99.0.0/18"
azs = ["${var.region}a", "${var.region}b", "${var.region}c"]
public_subnets = ["10.99.0.0/24", "10.99.1.0/24", "10.99.2.0/24"]
private_subnets = ["10.99.3.0/24", "10.99.4.0/24", "10.99.5.0/24"]
enable_nat_gateway = true
single_nat_gateway = true
enable_dns_hostnames = true
map_public_ip_on_launch = true
tags = local.tags
}
resource "aws_cloudwatch_log_group" "this" {
name = "/aws/ecs/${local.name}"
retention_in_days = 7
tags = local.tags
}
# End section copied from https://github.com/terraform-aws-modules/terraform-aws-ecs/blob/v4.1.3/examples/complete/main.tf