Hi team,
Please suggest how I can create a helper list that will contain some controls flow and elegantly reference it in other locals to reduce repetition.
Sample:
locals {
glue_jobs_helper = [for job in local.spark_jobs :
{
full_name = "${local.name}-${job.name}-${var.default_glue_job_suffix}-${var.environment}-0000000"
}
]
glue_jobs = [for job in local.spark_jobs :
{
_current_index = index(local.spark_jobs, job)
# full_name = "${local.name}-${job.name}-${var.default_glue_job_suffix}-${var.environment}-0000000"
full_name = element(local.glue_jobs_helper, index(local.spark_jobs, job)).full_name
role_arn = "arn"
connections = job.connection == "" ? [] : [job.connection]
command_type = job.type
python_version = contains(keys(job), "python_version") && job.type == "pythonshell" ? job.python_version : var.default_glue_python_version
script_location = "s3://${var.artifact_bucket}/${job.script_location}"
max_concurrent_runs = try(job.args.max_concurrent_runs, "3")
glue_version = try(job.glue_version, try(job.sfn_args["--GlueVersion"], var.default_glue_version))
timeout = job.timeout
default_arguments = merge(var.default_glue_job_args, job.args)
# Only jobs with AWS Glue version 3.0 and above and command type glueetl will be allowed to set ExecutionClass to FLEX.
# For pythonshell null will be implicitly casted into STANDARD. Left for output compatibility.
execution_class = (job.type == "glueetl" && try(job.glue_version, try(job.sfn_args["--GlueVersion"], var.default_glue_version)) >= "3.0" ?
try(job.execution_class, var.dataproduct_glue_execution_class) : "STANDARD")
# Do not set MaxCapacity if using WorkerType and NumberOfWorkers
# Required when pythonshell is set.
# For pythonshell null implicitly casts to 1. For glueetl null works as expected
max_capacity = job.type == "pythonshell" ? try(job.max_capacity, 1) : null
# For Glue version 2.0 or later jobs, you cannot specify a Maximum capacity.
# Instead, you should specify a Worker type and the Number of workers.
# Error: InvalidInputException: Worker Type is not supported for Job Command pythonshell
worker_type = job.type == "pythonshell" ? null : try(job.worker_type, "G.1X")
# Error: InvalidInputException: Please set both Worker Type and Number of Workers
number_of_workers = job.type == "pythonshell" ? null : try(job.number_of_workers, 2)
tags = merge(
local.effective_tags,
{ "GlueJobName" = "${local.name}-${job.name}-${var.default_glue_job_suffix}-${var.environment}" },
contains(keys(job), "max_job_price") ? { MaxJobPrice = job.max_job_price } : {}
)
}
]
glue_jobs2 = [for idx in tolist(range(length(local.spark_jobs))) :
{
full_name = element(glue_jobs_helper, idx).full_name
tags = merge(
local.effective_tags,
{ "GlueJobName" = element(glue_jobs_helper, idx).full_name },
contains(keys(job), "max_job_price") ? { MaxJobPrice = job.max_job_price } : {}
)
}
]
}
resource "aws_glue_job" "this" {
count = length(local.glue_jobs)
name = local.glue_jobs[count.index].full_name
role_arn = local.glue_jobs[count.index].role_arn
connections = local.glue_jobs[count.index].connections
glue_version = local.glue_jobs[count.index].glue_version
max_capacity = local.glue_jobs[count.index].max_capacity
timeout = local.glue_jobs[count.index].timeout
default_arguments = local.glue_jobs[count.index].default_arguments
execution_class = local.glue_jobs[count.index].execution_class
worker_type = local.glue_jobs[count.index].worker_type
number_of_workers = local.glue_jobs[count.index].number_of_workers
tags = local.glue_jobs[count.index].tags
command {
name = local.glue_jobs[count.index].command_type
python_version = local.glue_jobs[count.index].python_version
script_location = local.glue_jobs[count.index].script_location
}
execution_property {
max_concurrent_runs = local.glue_jobs[count.index].max_concurrent_runs
}
}
At first iteration I’ve extracted all computations from resources to locals but left list data structure.
And at second iteration trying to extract duplicated parts from local. glue_jobs to local. glue_jobs_helper to reduce repetition.
Not ready to switch from list to map due to backward compatibility and don’t think improved readability with element function.
Thanks.