@@ -23,6 +23,7 @@ resource "aws_ecs_task_definition" "service" {
2323 image = var.docker_image
2424 essential = true
2525 environment = concat (var. container_env_vars , [{ name = " DD_SERVICE" , value = var.container_family }])
26+ entrypoint = var.entrypoint
2627 portMappings = [
2728 {
2829 containerPort = var.container_port
@@ -103,7 +104,16 @@ resource "aws_ecs_service" "service" {
103104
104105 network_configuration {
105106 security_groups = var. service_security_groups
106- subnets = var. lb_subnets
107+ subnets = var. task_subnets
108+ }
109+
110+ dynamic "load_balancer" {
111+ for_each = var. create_alb ? [1 ] : []
112+ content {
113+ target_group_arn = aws_alb_target_group. front_end [0 ]. arn
114+ container_name = var. container_family
115+ container_port = var. container_port
116+ }
107117 }
108118
109119 service_registries {
@@ -119,6 +129,104 @@ resource "aws_ecs_service" "service" {
119129 lifecycle {
120130 create_before_destroy = true
121131 }
132+
133+ depends_on = [
134+ aws_cloudwatch_log_group . service ,
135+ aws_alb . lb ,
136+ aws_alb_target_group . front_end
137+ ]
138+ }
139+
140+ resource "aws_alb" "lb" {
141+ count = var. create_alb ? 1 : 0
142+ name = " ${ var . container_family } -${ var . environment } -${ var . stage } "
143+ internal = var. internal_lb
144+ security_groups = [aws_security_group . lb [0 ]. id ]
145+ subnets = var. lb_subnets
146+ enable_deletion_protection = false
147+ idle_timeout = var. timeout
148+
149+ tags = {
150+ Name = " ${ var . container_family } -${ var . environment } -${ var . stage } "
151+ Environment = var.environment
152+ Stage = var.stage
153+ Domain = var.domain
154+ }
155+ }
156+
157+ resource "aws_alb_target_group" "front_end" {
158+ count = var. create_alb ? 1 : 0
159+ name = " ${ var . container_family } -${ var . environment } -${ var . stage } "
160+ port = var. loadbalancer_port
161+ protocol = " HTTP"
162+ vpc_id = var. vpc_id
163+ target_type = " ip"
164+
165+ health_check {
166+ path = var. health_check_settings . path
167+ matcher = var. health_check_settings . matcher
168+ interval = var. health_check_settings . interval
169+ timeout = var. health_check_settings . timeout
170+ healthy_threshold = var. health_check_settings . healthy_threshold
171+ unhealthy_threshold = var. health_check_settings . unhealthy_threshold
172+ }
173+
174+ lifecycle {
175+ create_before_destroy = true
176+ }
177+
178+ depends_on = [aws_alb . lb ]
179+ }
180+
181+ resource "aws_lb_listener" "https" {
182+ count = var. create_alb ? 1 : 0
183+ load_balancer_arn = aws_alb. lb [0 ]. arn
184+ port = " 443"
185+ protocol = " HTTPS"
186+ ssl_policy = " ELBSecurityPolicy-2016-08"
187+ certificate_arn = var. cert_arn
188+
189+ default_action {
190+ type = " forward"
191+ target_group_arn = aws_alb_target_group. front_end [0 ]. arn
192+ }
193+
194+ depends_on = [aws_alb . lb , aws_alb_target_group . front_end ]
195+ }
196+
197+ resource "aws_security_group" "lb" {
198+ count = var. create_alb ? 1 : 0
199+ name = " ${ var . container_family } -alb-${ var . environment } -${ var . stage } "
200+ description = " Controls access to the ALB"
201+ vpc_id = var. vpc_id
202+
203+ # Allow all egress
204+ egress {
205+ from_port = 0
206+ to_port = 0
207+ protocol = " -1"
208+ cidr_blocks = [" 0.0.0.0/0" ]
209+ }
210+
211+ tags = {
212+ Name = " ${ var . container_family } -alb-${ var . environment } -${ var . stage } "
213+ Environment = var.environment
214+ Stage = var.stage
215+ Domain = var.domain
216+ }
217+ }
218+
219+ resource "aws_route53_record" "alb" {
220+ count = var. create_alb ? 1 : 0
221+ zone_id = var. zone_id
222+ name = " ${ var . container_family } .${ var . domain } "
223+ type = " A"
224+
225+ alias {
226+ name = aws_alb. lb [0 ]. dns_name
227+ zone_id = aws_alb. lb [0 ]. zone_id
228+ evaluate_target_health = true
229+ }
122230}
123231
124232data "aws_service_discovery_dns_namespace" "namespace" {
@@ -146,3 +254,36 @@ resource "aws_service_discovery_service" "service" {
146254 create_before_destroy = true
147255 }
148256}
257+
258+ resource "aws_security_group_rule" "alb_https" {
259+ count = var. create_alb ? 1 : 0
260+ type = " ingress"
261+ from_port = 443
262+ to_port = 443
263+ protocol = " tcp"
264+ cidr_blocks = var. ingress_cdir_blocks
265+ security_group_id = aws_security_group. lb [0 ]. id
266+ description = " Allow HTTPS inbound traffic"
267+ }
268+
269+ resource "aws_security_group_rule" "alb_to_container" {
270+ count = var. create_alb ? 1 : 0
271+ type = " egress"
272+ from_port = var. container_port
273+ to_port = var. container_port
274+ protocol = " tcp"
275+ source_security_group_id = var. service_security_groups [0 ]
276+ security_group_id = aws_security_group. lb [0 ]. id
277+ description = " Allow outbound traffic to container"
278+ }
279+
280+ resource "aws_security_group_rule" "container_from_alb" {
281+ count = var. create_alb ? 1 : 0
282+ type = " ingress"
283+ from_port = var. container_port
284+ to_port = var. container_port
285+ protocol = " tcp"
286+ source_security_group_id = aws_security_group. lb [0 ]. id
287+ security_group_id = var. service_security_groups [0 ]
288+ description = " Allow inbound traffic from ALB"
289+ }
0 commit comments