Skip to content

Commit 4c0866b

Browse files
authored
Parameterize Terraform Child Module (#24)
* Initial parameterization refactor * Lint * default_tags padding
1 parent 864827c commit 4c0866b

File tree

5 files changed

+93
-71
lines changed

5 files changed

+93
-71
lines changed

terraform/main.tf

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ 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_iam_role" "job" {
27+
name = "${var.prefix}-batch-job-role"
28+
}
29+
30+
data "aws_iam_role" "exec" {
31+
name = "${var.prefix}-ecs-exe-task-role"
32+
}
33+
2234
locals {
2335
account_id = sensitive(data.aws_caller_identity.current.account_id)
2436
default_tags = length(var.default_tags) == 0 ? {
@@ -29,11 +41,16 @@ locals {
2941
}
3042

3143
module "confluence-input" {
32-
source = "./modules/input"
33-
api_key = var.api_key
34-
app_name = var.app_name
44+
source = "./modules/input"
45+
api_key = var.api_key
46+
app_name = var.app_name
3547
app_version = var.app_version
36-
aws_region = var.aws_region
48+
aws_region = var.aws_region
49+
efs_file_system_ids = {
50+
input = data.aws_efs_file_system.input.file_system_id
51+
}
3752
environment = var.environment
38-
prefix = var.prefix
39-
}
53+
iam_execution_role_arn = data.aws_iam_role.exec.arn
54+
iam_job_role_arn = data.aws_iam_role.job.arn
55+
prefix = var.prefix
56+
}
Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,55 @@
11
# Job Definition
22
resource "aws_batch_job_definition" "generate_batch_jd_input" {
3-
name = "${var.prefix}-input"
4-
type = "container"
5-
container_properties = <<CONTAINER_PROPERTIES
6-
{
7-
"image": "${local.account_id}.dkr.ecr.us-west-2.amazonaws.com/${var.prefix}-input",
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}"
15-
}
16-
},
17-
"resourceRequirements": [
18-
{"type": "MEMORY", "value": "1024"},
19-
{"type": "VCPU", "value": "0.5"}
20-
],
21-
"mountPoints": [
22-
{
23-
"sourceVolume": "input",
24-
"containerPath": "/mnt/data"
3+
name = "${var.prefix}-input"
4+
type = "container"
5+
platform_capabilities = ["FARGATE"]
6+
propagate_tags = true
7+
tags = { "job_definition" : "${var.prefix}-input" }
8+
9+
container_properties = jsonencode({
10+
image = "${local.account_id}.dkr.ecr.us-west-2.amazonaws.com/${var.prefix}-input:${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
2520
}
26-
],
27-
"volumes": [
28-
{
29-
"name": "input",
30-
"efsVolumeConfiguration": {
31-
"fileSystemId": "${data.aws_efs_file_system.aws_efs_input.file_system_id}",
32-
"rootDirectory": "/"
33-
}
21+
}
22+
resourceRequirements = [{
23+
type = "MEMORY"
24+
value = "1024"
25+
}, {
26+
type = "VCPU",
27+
value = "0.5"
28+
}]
29+
mountPoints = [{
30+
sourceVolume = "input",
31+
containerPath = "/mnt/data"
32+
readOnly = false
33+
}]
34+
volumes = [{
35+
name = "input"
36+
efsVolumeConfiguration = {
37+
fileSystemId = var.efs_file_system_ids["input"]
38+
rootDirectory = "/"
3439
}
35-
]
36-
}
37-
CONTAINER_PROPERTIES
38-
platform_capabilities = ["FARGATE"]
39-
propagate_tags = true
40-
tags = { "job_definition" : "${var.prefix}-input" }
40+
}]
41+
})
4142
}
4243

4344
# API key parameter
4445
resource "aws_ssm_parameter" "hydrocron_key_parameter" {
45-
name = "${var.prefix}-hydrocron-key"
46+
name = "${var.prefix}-hydrocron-key"
4647
description = "Hydrocron confluence API key"
47-
type = "SecureString"
48-
value = var.api_key
49-
overwrite = true
50-
}
48+
type = "SecureString"
49+
value = var.api_key
50+
}
51+
52+
# Log group
53+
resource "aws_cloudwatch_log_group" "cw_log_group" {
54+
name = "/aws/batch/job/${var.prefix}-input/"
55+
}

terraform/modules/input/main.tf

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +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}-input/"
6-
}
7-
8-
data "aws_efs_file_system" "aws_efs_input" {
9-
creation_token = "${var.prefix}-input"
10-
}
11-
12-
data "aws_iam_role" "job_role" {
13-
name = "${var.prefix}-batch-job-role"
14-
}
15-
16-
data "aws_iam_role" "exe_role" {
17-
name = "${var.prefix}-ecs-exe-task-role"
18-
}
19-
204
# Local variables
215
locals {
226
account_id = data.aws_caller_identity.current.account_id
23-
default_tags = length(var.default_tags) == 0 ? {
24-
application : var.app_name,
25-
environment : lower(var.environment),
26-
version : var.app_version
27-
} : var.default_tags
28-
}
7+
}

terraform/modules/input/variables.tf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,32 @@ variable "default_tags" {
2525
default = {}
2626
}
2727

28+
variable "efs_file_system_ids" {
29+
type = map(string)
30+
description = "Map of EFS file system ids to pass to the container definition"
31+
}
32+
2833
variable "environment" {
2934
type = string
3035
description = "The environment in which to deploy to"
3136
}
3237

38+
variable "iam_execution_role_arn" {
39+
type = string
40+
description = "The IAM ARN of the execution role"
41+
}
42+
43+
variable "iam_job_role_arn" {
44+
type = string
45+
description = "The IAM ARN of the job role"
46+
}
47+
48+
variable "image_tag" {
49+
type = string
50+
description = "The container image tag to utilize"
51+
default = "latest"
52+
}
53+
3354
variable "prefix" {
3455
type = string
3556
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
@@ -21,8 +21,8 @@ variable "aws_region" {
2121
}
2222

2323
variable "default_tags" {
24-
type = map(string)
25-
default = {}
24+
type = map(string)
25+
default = {}
2626
}
2727

2828
variable "environment" {

0 commit comments

Comments
 (0)