Skip to content

Commit 232c533

Browse files
joshgardenikki-t
andauthored
Parameterize Terraform Child Module (#15)
* Initial parameterization refactor * default_tags padding --------- Co-authored-by: Nikki <17799906+nikki-t@users.noreply.github.com>
1 parent 238643b commit 232c533

File tree

5 files changed

+106
-85
lines changed

5 files changed

+106
-85
lines changed

terraform/main.tf

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ provider "aws" {
1919

2020
data "aws_caller_identity" "current" {}
2121

22+
data "aws_efs_file_system" "input" {
23+
creation_token = "${var.prefix}-input"
24+
}
25+
26+
data "aws_efs_file_system" "flpe" {
27+
creation_token = "${var.prefix}-flpe"
28+
}
29+
30+
data "aws_iam_role" "job" {
31+
name = "${var.prefix}-batch-job-role"
32+
}
33+
34+
data "aws_iam_role" "execution" {
35+
name = "${var.prefix}-ecs-exe-task-role"
36+
}
37+
2238
locals {
2339
account_id = sensitive(data.aws_caller_identity.current.account_id)
2440
default_tags = length(var.default_tags) == 0 ? {
@@ -29,10 +45,16 @@ locals {
2945
}
3046

3147
module "confluence-metroman" {
32-
source = "./modules/metroman"
33-
app_name = var.app_name
34-
app_version = var.app_version
35-
aws_region = var.aws_region
36-
environment = var.environment
37-
prefix = var.prefix
38-
}
48+
source = "./modules/metroman"
49+
app_name = var.app_name
50+
app_version = var.app_version
51+
aws_region = var.aws_region
52+
efs_file_system_ids = {
53+
input = data.aws_efs_file_system.input.file_system_id
54+
flpe = data.aws_efs_file_system.flpe.file_system_id
55+
}
56+
environment = var.environment
57+
iam_execution_role_arn = data.aws_iam_role.execution.arn
58+
iam_job_role_arn = data.aws_iam_role.job.arn
59+
prefix = var.prefix
60+
}
Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,57 @@
11
# Job Definition
22
resource "aws_batch_job_definition" "generate_batch_jd_metroman" {
3-
name = "${var.prefix}-metroman"
4-
type = "container"
5-
container_properties = <<CONTAINER_PROPERTIES
6-
{
7-
"image": "${local.account_id}.dkr.ecr.us-west-2.amazonaws.com/${var.prefix}-metroman",
8-
"executionRoleArn": "${data.aws_iam_role.exe_role.arn}",
9-
"jobRoleArn": "${data.aws_iam_role.job_role.arn}",
10-
"fargatePlatformConfiguration": { "platformVersion": "LATEST" },
11-
"logConfiguration": {
12-
"logDriver" : "awslogs",
13-
"options": {
14-
"awslogs-group" : "${data.aws_cloudwatch_log_group.cw_log_group.name}"
3+
name = "${var.prefix}-metroman"
4+
type = "container"
5+
platform_capabilities = ["FARGATE"]
6+
propagate_tags = true
7+
tags = { "job_definition": "${var.prefix}-metroman" }
8+
9+
container_properties = jsonencode({
10+
image = "${local.account_id}.dkr.ecr.us-west-2.amazonaws.com/${var.prefix}-metroman:${var.image_tag}"
11+
executionRoleArn = var.iam_execution_role_arn
12+
jobRoleArn = var.iam_job_role_arn
13+
fargatePlatformConfiguration = {
14+
platformVersion = "LATEST"
15+
}
16+
logConfiguration = {
17+
logDriver = "awslogs"
18+
options = {
19+
awslogs-group = aws_cloudwatch_log_group.cw_log_group.name
1520
}
16-
},
17-
"resourceRequirements": [
18-
{"type": "MEMORY", "value": "8192"},
19-
{"type": "VCPU", "value": "4"}
20-
],
21-
"mountPoints": [
22-
{
23-
"sourceVolume": "input",
24-
"containerPath": "/mnt/data/input",
25-
"readOnly": true
26-
},
27-
{
28-
"sourceVolume": "flpe",
29-
"containerPath": "/mnt/data/output",
30-
"readOnly": false
21+
}
22+
resourceRequirements = [{
23+
type = "MEMORY"
24+
value = "8192"
25+
}, {
26+
type = "VCPU",
27+
value = "4"
28+
}]
29+
mountPoints = [{
30+
sourceVolume = "input",
31+
containerPath = "/mnt/data/input"
32+
readOnly = false
33+
}, {
34+
sourceVolume = "flpe"
35+
containerPath = "/mnt/data/output"
36+
readOnly = false
37+
}]
38+
volumes = [{
39+
name = "input"
40+
efsVolumeConfiguration = {
41+
fileSystemId = var.efs_file_system_ids["input"]
42+
rootDirectory = "/"
3143
}
32-
],
33-
"volumes": [
34-
{
35-
"name": "input",
36-
"efsVolumeConfiguration": {
37-
"fileSystemId": "${data.aws_efs_file_system.aws_efs_input.file_system_id}",
38-
"rootDirectory": "/"
39-
}
40-
},
41-
{
42-
"name": "flpe",
43-
"efsVolumeConfiguration": {
44-
"fileSystemId": "${data.aws_efs_file_system.aws_efs_flpe.file_system_id}",
45-
"rootDirectory": "/metroman"
46-
}
44+
}, {
45+
name = "flpe"
46+
efsVolumeConfiguration = {
47+
fileSystemId = var.efs_file_system_ids["flpe"]
48+
rootDirectory = "/metroman"
4749
}
48-
]
49-
}
50-
CONTAINER_PROPERTIES
51-
platform_capabilities = ["FARGATE"]
52-
propagate_tags = true
53-
tags = { "job_definition": "${var.prefix}-metroman" }
50+
}]
51+
})
52+
}
53+
54+
# Log group
55+
resource "aws_cloudwatch_log_group" "cw_log_group" {
56+
name = "/aws/batch/job/${var.prefix}-metroman/"
5457
}

terraform/modules/metroman/main.tf

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,7 @@
11
# Data sources
22
data "aws_caller_identity" "current" {}
33

4-
data "aws_cloudwatch_log_group" "cw_log_group" {
5-
name = "/aws/batch/job/${var.prefix}-metroman/"
6-
}
7-
8-
data "aws_efs_file_system" "aws_efs_input" {
9-
creation_token = "${var.prefix}-input"
10-
}
11-
12-
data "aws_efs_file_system" "aws_efs_flpe" {
13-
creation_token = "${var.prefix}-flpe"
14-
}
15-
16-
data "aws_iam_role" "job_role" {
17-
name = "${var.prefix}-batch-job-role"
18-
}
19-
20-
data "aws_iam_role" "exe_role" {
21-
name = "${var.prefix}-ecs-exe-task-role"
22-
}
23-
244
# Local variables
255
locals {
266
account_id = data.aws_caller_identity.current.account_id
27-
default_tags = length(var.default_tags) == 0 ? {
28-
application : var.app_name,
29-
environment : lower(var.environment),
30-
version : var.app_version
31-
} : var.default_tags
32-
}
7+
}

terraform/modules/metroman/variables.tf

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,36 @@ variable "aws_region" {
1616
}
1717

1818
variable "default_tags" {
19-
type = map(string)
20-
default = {}
19+
type = map(string)
20+
default = {}
21+
}
22+
23+
variable "efs_file_system_ids" {
24+
type = map(string)
25+
description = "Map of EFS file system ids to pass to the container definition"
2126
}
2227

2328
variable "environment" {
2429
type = string
2530
description = "The environment in which to deploy to"
2631
}
2732

33+
variable "iam_execution_role_arn" {
34+
type = string
35+
description = "The IAM ARN of the execution role"
36+
}
37+
38+
variable "iam_job_role_arn" {
39+
type = string
40+
description = "The IAM ARN of the job role"
41+
}
42+
43+
variable "image_tag" {
44+
type = string
45+
description = "The container image tag to utilize"
46+
default = "latest"
47+
}
48+
2849
variable "prefix" {
2950
type = string
3051
description = "Prefix to add to all AWS resources as a unique identifier"

terraform/variables.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ variable "aws_region" {
1616
}
1717

1818
variable "default_tags" {
19-
type = map(string)
20-
default = {}
19+
type = map(string)
20+
default = {}
2121
}
2222

2323
variable "environment" {

0 commit comments

Comments
 (0)