Skip to content

Commit e962732

Browse files
authoredFeb 23, 2024··
Executor Improvements Part 01 (#71)
* generate random UUID for prefix variable * Update CHANGELOG.md
1 parent cd8a6b8 commit e962732

File tree

5 files changed

+50
-32
lines changed

5 files changed

+50
-32
lines changed
 

‎CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [UNRELEASED]
99

10+
### Added
11+
12+
- Generate random UUID for prefix variable to avoid name conflicting deployed resources
13+
1014
## [0.33.0] - 2023-11-21
1115

1216
### Changed

‎covalent_ecs_plugin/assets/infra/iam.tf

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ data "aws_iam_policy_document" "ecs_tasks_execution_role" {
1010
}
1111

1212
resource "aws_iam_role" "ecs_tasks_execution_role" {
13-
name = "${var.prefix}-task-execution-role"
13+
name = "${local.prefix}-task-execution-role"
1414
assume_role_policy = data.aws_iam_policy_document.ecs_tasks_execution_role.json
1515
}
1616

@@ -20,7 +20,7 @@ resource "aws_iam_role_policy_attachment" "ecs_tasks_execution_role" {
2020
}
2121

2222
resource "aws_iam_role_policy" "task_policy" {
23-
name = "${var.prefix}-task-policy"
23+
name = "${local.prefix}-task-policy"
2424
role = aws_iam_role.task_role.id
2525

2626
policy = jsonencode({
@@ -49,7 +49,7 @@ resource "aws_iam_role_policy" "task_policy" {
4949
}
5050

5151
resource "aws_iam_role" "task_role" {
52-
name = "${var.prefix}-task-role"
52+
name = "${local.prefix}-task-role"
5353

5454
assume_role_policy = jsonencode({
5555
"Version" : "2012-10-17",

‎covalent_ecs_plugin/assets/infra/main.tf

+40-26
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,26 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
provider "aws" {
18-
region = var.aws_region
17+
provider "aws" {}
18+
19+
data "aws_region" "current" {}
20+
21+
resource "random_string" "default_prefix" {
22+
length = 9
23+
upper = false
24+
special = false
25+
}
26+
27+
locals {
28+
prefix = var.prefix == "" ? random_string.default_prefix.result : var.prefix
29+
subnet_id = var.subnet_id == "" ? aws_default_subnet.default.id : var.subnet_id
30+
credentials = var.credentials == "" ? pathexpand("~/.aws/credentials") : var.credentials
31+
profile = var.profile == "" ? "default" : var.profile
32+
region = var.region == "" ? data.aws_region.current.name : var.region
1933
}
2034

2135
resource "aws_s3_bucket" "bucket" {
22-
bucket = "${var.prefix}-bucket"
36+
bucket = "${local.prefix}-bucket"
2337
force_destroy = true
2438
}
2539

@@ -38,7 +52,7 @@ resource "aws_s3_bucket_acl" "bucket_acl" {
3852
}
3953

4054
resource "aws_ecr_repository" "ecr_repository" {
41-
name = "${var.prefix}-ecr-repo"
55+
name = "${local.prefix}-ecr-repo"
4256
image_tag_mutability = "IMMUTABLE"
4357

4458
force_delete = true
@@ -49,45 +63,45 @@ resource "aws_ecr_repository" "ecr_repository" {
4963
}
5064

5165
resource "aws_cloudwatch_log_group" "log_group" {
52-
name = "${var.prefix}-log-group"
66+
name = "${local.prefix}-log-group"
5367
}
5468

5569
resource "aws_ecs_cluster" "ecs_cluster" {
56-
name = "${var.prefix}-ecs-cluster"
70+
name = "${local.prefix}-ecs-cluster"
5771

5872
configuration {
5973
execute_command_configuration {
60-
logging = "OVERRIDE"
74+
logging = "OVERRIDE"
6175
log_configuration {
62-
cloud_watch_log_group_name = aws_cloudwatch_log_group.log_group.name
76+
cloud_watch_log_group_name = aws_cloudwatch_log_group.log_group.name
6377
}
6478
}
6579
}
6680
}
6781

6882
# Executor Covalent config section
69-
data template_file executor_config {
70-
template = "${file("${path.module}/ecs.conf.tftpl")}"
83+
data "template_file" "executor_config" {
84+
template = file("${path.module}/ecs.conf.tftpl")
7185

7286
vars = {
73-
credentials=var.credentials
74-
profile=var.profile
75-
region=var.aws_region
76-
s3_bucket_name=aws_s3_bucket.bucket.id
77-
ecs_cluster_name=aws_ecs_cluster.ecs_cluster.name
78-
ecs_task_execution_role_name=aws_iam_role.ecs_tasks_execution_role.name
79-
ecs_task_role_name=aws_iam_role.task_role.name
80-
ecs_task_subnet_id=module.vpc.public_subnets[0]
81-
ecs_task_security_group_id=aws_security_group.sg.id
82-
ecs_task_log_group_name=aws_cloudwatch_log_group.log_group.name
83-
vcpu=var.vcpus
84-
memory=var.memory
85-
cache_dir=var.cache_dir
86-
poll_freq=var.poll_freq
87+
credentials = var.credentials
88+
profile = var.profile
89+
region = var.region
90+
s3_bucket_name = aws_s3_bucket.bucket.id
91+
ecs_cluster_name = aws_ecs_cluster.ecs_cluster.name
92+
ecs_task_execution_role_name = aws_iam_role.ecs_tasks_execution_role.name
93+
ecs_task_role_name = aws_iam_role.task_role.name
94+
ecs_task_subnet_id = module.vpc.public_subnets[0]
95+
ecs_task_security_group_id = aws_security_group.sg.id
96+
ecs_task_log_group_name = aws_cloudwatch_log_group.log_group.name
97+
vcpu = var.vcpus
98+
memory = var.memory
99+
cache_dir = var.cache_dir
100+
poll_freq = var.poll_freq
87101
}
88102
}
89103

90-
resource local_file executor_config {
91-
content = data.template_file.executor_config.rendered
104+
resource "local_file" "executor_config" {
105+
content = data.template_file.executor_config.rendered
92106
filename = "${path.module}/ecs.conf"
93107
}

‎covalent_ecs_plugin/assets/infra/networking.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module "vpc" {
2222
name = "${var.prefix}-vpc"
2323
cidr = var.vpc_cidr
2424

25-
azs = ["${var.aws_region}a"]
25+
azs = ["${var.region}a"]
2626

2727
public_subnets = [
2828
cidrsubnet(var.vpc_cidr, 0, 0)

‎covalent_ecs_plugin/assets/infra/variables.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
# limitations under the License.
1616

1717
variable "prefix" {
18-
default = "covalent-ecs-ft"
18+
default = ""
1919
description = "Name used to prefix AWS resources"
2020
}
2121

22-
variable "aws_region" {
22+
variable "region" {
2323
default = "us-east-1"
2424
description = "Region in which Covalent is deployed"
2525
}

0 commit comments

Comments
 (0)
Please sign in to comment.