Skip to content

Commit 9dff194

Browse files
role already exist fix
1 parent 1d14f02 commit 9dff194

File tree

3 files changed

+41
-48
lines changed

3 files changed

+41
-48
lines changed
+33-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
resource "aws_ecs_cluster" "default" {
2-
name = var.ecs_cluster_name
2+
name = "${var.ecs_cluster_name}"
33
}
44

55
resource "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+
1519
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"
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

3238
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
3442
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
3543
}
3644

3745
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
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

5361
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+
}
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
}

ecs-fargate-wordpress-rds-terraform/variables.tf

+5-20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
variable "region" {
2+
description = "AWS Region"
3+
default = "your-region"
4+
}
5+
16
variable "aws_access_key" {
27
description = "AWS access key"
38
default = "your-access-key"
@@ -8,26 +13,6 @@ variable "aws_secret_key" {
813
default = "your-secret-key"
914
}
1015

11-
variable "region" {
12-
description = "AWS Region"
13-
default = "us-east-1"
14-
}
15-
16-
variable "zone1" {
17-
description = "AWS Zone 1"
18-
default = "us-east-1a"
19-
}
20-
21-
variable "zone2" {
22-
description = "AWS Zone 2"
23-
default = "us-east-1b"
24-
}
25-
26-
variable "zone3" {
27-
description = "AWS Zone 3"
28-
default = "us-east-1c"
29-
}
30-
3116
// VPC
3217
variable "vpc_cidr_block" {
3318
description = "VPC network"

ecs-fargate-wordpress-rds-terraform/vpc.tf

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ resource "aws_internet_gateway" "default" {
2121
resource "aws_subnet" "wp-public-a-tf" {
2222
vpc_id = "${aws_vpc.default.id}"
2323
cidr_block = "${var.public_subnet_a_cidr_block}"
24-
availability_zone = "${var.zone1}"
24+
availability_zone = "${var.region}a"
2525

2626
tags = {
2727
Name = "wp-public-a-tf"
@@ -31,7 +31,7 @@ resource "aws_subnet" "wp-public-a-tf" {
3131
resource "aws_subnet" "wp-public-b-tf" {
3232
vpc_id = "${aws_vpc.default.id}"
3333
cidr_block = "${var.public_subnet_b_cidr_block}"
34-
availability_zone = "${var.zone2}"
34+
availability_zone = "${var.region}b"
3535

3636
tags = {
3737
Name = "wp-public-b-tf"
@@ -41,7 +41,7 @@ resource "aws_subnet" "wp-public-b-tf" {
4141
resource "aws_subnet" "wp-public-c-tf" {
4242
vpc_id = "${aws_vpc.default.id}"
4343
cidr_block = "${var.public_subnet_c_cidr_block}"
44-
availability_zone = "${var.zone3}"
44+
availability_zone = "${var.region}c"
4545

4646
tags = {
4747
Name = "wp-public-c-tf"

0 commit comments

Comments
 (0)