1
1
resource "aws_ecs_cluster" "default" {
2
- name = var. ecs_cluster_name
2
+ name = " ${ var . ecs_cluster_name } "
3
3
}
4
4
5
5
resource "aws_cloudwatch_log_group" "ecs_log_group" {
@@ -12,8 +12,14 @@ resource "aws_cloudwatch_log_group" "ecs_log_group" {
12
12
}
13
13
}
14
14
15
+ data "aws_iam_role" "existing_ecs_task_execution_role" {
16
+ name = " ecsTaskExecutionRole"
17
+ }
18
+
15
19
resource "aws_iam_role" "ecs_task_execution_role" {
16
- name = " ecsTaskExecutionRole-tf"
20
+ count = length (data. aws_iam_role . existing_ecs_task_execution_role . arn ) == 0 ? 1 : 0
21
+
22
+ name = " ecsTaskExecutionRole"
17
23
18
24
assume_role_policy = jsonencode ({
19
25
Version = " 2012-10-17"
@@ -30,19 +36,21 @@ resource "aws_iam_role" "ecs_task_execution_role" {
30
36
}
31
37
32
38
resource "aws_iam_role_policy_attachment" "ecs_task_execution_role_policy" {
33
- role = aws_iam_role. ecs_task_execution_role . name
39
+ count = length (data. aws_iam_role . existing_ecs_task_execution_role . arn ) == 0 ? 1 : 0
40
+
41
+ role = aws_iam_role. ecs_task_execution_role [count . index ]. name
34
42
policy_arn = " arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
35
43
}
36
44
37
45
resource "aws_ecs_task_definition" "wordpress" {
38
- family = " wp-ecs-task-tf"
39
- container_definitions = data. template_file . wp-container . rendered
46
+ family = " wp-ecs-task-tf"
47
+ container_definitions = data. template_file . wp-container . rendered
40
48
requires_compatibilities = [" FARGATE" ]
41
- network_mode = " awsvpc"
42
- cpu = 512
43
- memory = 1024
49
+ network_mode = " awsvpc"
50
+ cpu = 512
51
+ memory = 1024
44
52
45
- execution_role_arn = aws_iam_role. ecs_task_execution_role . arn
53
+ execution_role_arn = length (data . aws_iam_role . existing_ecs_task_execution_role . arn ) == 0 ? aws_iam_role . ecs_task_execution_role [ 0 ] . arn : data . aws_iam_role . existing_ecs_task_execution_role . arn
46
54
47
55
runtime_platform {
48
56
operating_system_family = " LINUX"
@@ -51,21 +59,21 @@ resource "aws_ecs_task_definition" "wordpress" {
51
59
}
52
60
53
61
resource "aws_ecs_service" "wp-ecs-svc" {
54
- name = " wp-ecs-svc-tf"
55
- cluster = aws_ecs_cluster. default . id
56
- task_definition = aws_ecs_task_definition. wordpress . arn
57
- desired_count = 1
58
- launch_type = " FARGATE"
62
+ name = " wp-ecs-svc-tf"
63
+ cluster = " ${ aws_ecs_cluster . default . id } "
64
+ task_definition = " ${ aws_ecs_task_definition . wordpress . arn } "
65
+ desired_count = 1
66
+ launch_type = " FARGATE"
67
+
68
+ load_balancer {
69
+ target_group_arn = " ${ aws_lb_target_group . default . arn } "
70
+ container_name = " wordpress"
71
+ container_port = 80
72
+ }
59
73
60
- load_balancer {
61
- target_group_arn = aws_lb_target_group. default . arn
62
- container_name = " wordpress"
63
- container_port = 80
64
- }
65
-
66
- network_configuration {
67
- subnets = [aws_subnet . wp-public-a-tf . id , aws_subnet . wp-public-b-tf . id , aws_subnet . wp-public-c-tf . id ]
68
- security_groups = [aws_security_group . wp-alb-tf . id ]
69
- assign_public_ip = true
70
- }
74
+ network_configuration {
75
+ subnets = [" ${ aws_subnet . wp-public-a-tf . id } " , " ${ aws_subnet . wp-public-b-tf . id } " , " ${ aws_subnet . wp-public-c-tf . id } " ]
76
+ security_groups = [" ${ aws_security_group . wp-alb-tf . id } " ]
77
+ assign_public_ip = true
78
+ }
71
79
}
0 commit comments