11resource "aws_ecs_cluster" "default" {
2- name = var. ecs_cluster_name
2+ name = " ${ var . ecs_cluster_name } "
33}
44
55resource "aws_cloudwatch_log_group" "ecs_log_group" {
@@ -12,8 +12,14 @@ resource "aws_cloudwatch_log_group" "ecs_log_group" {
1212 }
1313}
1414
15+ data "aws_iam_role" "existing_ecs_task_execution_role" {
16+ name = " ecsTaskExecutionRole"
17+ }
18+
1519resource "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"
1723
1824 assume_role_policy = jsonencode ({
1925 Version = " 2012-10-17"
@@ -30,19 +36,21 @@ resource "aws_iam_role" "ecs_task_execution_role" {
3036}
3137
3238resource "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
3442 policy_arn = " arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
3543}
3644
3745resource "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
4048 requires_compatibilities = [" FARGATE" ]
41- network_mode = " awsvpc"
42- cpu = 512
43- memory = 1024
49+ network_mode = " awsvpc"
50+ cpu = 512
51+ memory = 1024
4452
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
4654
4755 runtime_platform {
4856 operating_system_family = " LINUX"
@@ -51,21 +59,21 @@ resource "aws_ecs_task_definition" "wordpress" {
5159}
5260
5361resource "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+ }
5973
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+ }
7179}
0 commit comments