Skip to content

Commit

Permalink
feat(terraform): vol5504 add single batch job (#158)
Browse files Browse the repository at this point in the history
* feat(terraform): vol5504 add single batch job

* docs: update Terraform docs

* attempt to translate between environment variables without the need for TFVars to manage this

* feat(terraform): vol5504 add single batch job

* feat(terraform): vol5504 add single batch job

* feat(terraform): vol5504 add single batch job

* docs: update Terraform docs

* feat(terraform): vol5504 add single batch job

* resolving terraform fmt errors

* docs: update Terraform docs

* feat(terraform): vol5504 add single batch job

* docs: update Terraform docs

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Laurence <[email protected]>
  • Loading branch information
3 people authored Jul 5, 2024
1 parent 66ec908 commit 5c8ee05
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 1 deletion.
9 changes: 9 additions & 0 deletions infra/terraform/environments/dev/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,13 @@ module "service" {
listener_rule_host_header = "ssweb.*"
}
}

job_definitions = {
"processQueue" = {
job_name = "processQueue",
command = ["/var/www/html/vendor/bin/laminas --container=/var/www/html/config/container-cli.php", "queue:process-queue"],
image = "054614622558.dkr.ecr.eu-west-1.amazonaws.com/vol-app/cli:latest",
memory = "2048",
}
}
}
4 changes: 3 additions & 1 deletion infra/terraform/modules/service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
| Name | Source | Version |
|------|--------|---------|
| <a name="module_acm"></a> [acm](#module\_acm) | terraform-aws-modules/acm/aws | ~> 5.0 |
| <a name="module_batch"></a> [batch](#module\_batch) | terraform-aws-modules/batch/aws | ~> 2.0.0 |
| <a name="module_cloudfront"></a> [cloudfront](#module\_cloudfront) | terraform-aws-modules/cloudfront/aws | ~> 3.4 |
| <a name="module_ecs_cluster"></a> [ecs\_cluster](#module\_ecs\_cluster) | terraform-aws-modules/ecs/aws//modules/cluster | ~> 5.10 |
| <a name="module_ecs_service"></a> [ecs\_service](#module\_ecs\_service) | terraform-aws-modules/ecs/aws//modules/service | ~> 5.10 |
| <a name="module_log_bucket"></a> [log\_bucket](#module\_log\_bucket) | terraform-aws-modules/s3-bucket/aws | ~> 4.0 |
| <a name="module_records"></a> [records](#module\_records) | terraform-aws-modules/route53/aws//modules/records | ~> 2.0 |
| <a name="module_records"></a> [records](#module\_records) | terraform-aws-modules/route53/aws//modules/records | ~> 3.1 |
| <a name="module_route53_records"></a> [route53\_records](#module\_route53\_records) | terraform-aws-modules/acm/aws | ~> 5.0 |

## Resources
Expand All @@ -45,6 +46,7 @@
| <a name="input_assets_version"></a> [assets\_version](#input\_assets\_version) | The version of the assets | `string` | n/a | yes |
| <a name="input_domain_name"></a> [domain\_name](#input\_domain\_name) | The domain name for the environment | `string` | n/a | yes |
| <a name="input_environment"></a> [environment](#input\_environment) | The environment to deploy to | `string` | n/a | yes |
| <a name="input_job_definitions"></a> [job\_definitions](#input\_job\_definitions) | The batch job defintion settings | <pre>map(object({<br> job_name = string<br> command = list(string)<br> image = string<br> memory = string<br> }))</pre> | `{}` | no |
| <a name="input_services"></a> [services](#input\_services) | The services to deploy | <pre>map(object({<br> version = string<br> repository = string<br> cpu = number<br> memory = number<br> task_iam_role_statements = list(object({<br> effect = string<br> actions = list(string)<br> resources = list(string)<br> }))<br> add_cdn_url_to_env = optional(bool, false)<br> lb_listener_arn = string<br> listener_rule_priority = optional(number, 10)<br> listener_rule_host_header = optional(string, "*")<br> security_group_ids = list(string)<br> subnet_ids = list(string)<br> vpc_id = optional(string, null)<br> }))</pre> | `{}` | no |
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | The VPC ID | `string` | n/a | yes |

Expand Down
134 changes: 134 additions & 0 deletions infra/terraform/modules/service/batch.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@

module "batch" {
source = "terraform-aws-modules/batch/aws"
version = "~> 2.0.0"

instance_iam_role_name = "${var.environment}-batch-app-ecs-instance-role"
instance_iam_role_path = "/batch/"
instance_iam_role_description = "IAM instance role/profile for AWS Batch ECS instance(s)"
instance_iam_role_tags = {
ModuleCreatedRole = "Yes"
}

service_iam_role_name = "${var.environment}-batch-app-batch-role"
service_iam_role_path = "/batch/"
service_iam_role_description = "IAM service role for AWS Batch"
service_iam_role_tags = {
ModuleCreatedRole = "Yes"
}

create_spot_fleet_iam_role = false
spot_fleet_iam_role_name = "${var.environment}-batch-app-spot-role"
spot_fleet_iam_role_path = "/batch/"
spot_fleet_iam_role_description = "IAM spot fleet role for AWS Batch"
spot_fleet_iam_role_tags = {
ModuleCreatedRole = "Yes"
}

compute_environments = {
a_fargate = {
name_prefix = "batch-app-fargate"

compute_resources = {
type = "FARGATE"
max_vcpus = 4

security_group_ids = "${var.environment} =\"dev\" ? format(\"%s/%s\",\"DEV/APP\",upper(var.environment)) : upper(var.environment)}-OLCS-PRI-API-SG"
subnet_ids = ["${var.environment} =\"dev\" ? format(\"%s/%s\",\"DEV/APP\",upper(var.environment)) : upper(var.environment)}-OLCS-PRI-BATCH-1A", "${var.environment} =\"dev\" ? format(\"%s/%s\",\"DEV/APP\",upper(var.environment)) : upper(var.environment)}-OLCS-PRI-BATCH-1B", "${var.environment} =\"dev\" ? format(\"%s/%s\",\"DEV/APP\",upper(var.environment)) : upper(var.environment)}-OLCS-PRI-BATCH-1C"]

# `tags = {}` here is not applicable for spot
}
}

b_fargate_spot = {
name_prefix = "batch-app-fargate_spot"

compute_resources = {
type = "FARGATE_SPOT"
max_vcpus = 4

security_group_ids = "${var.environment} =\"dev\" ? format(\"%s/%s\",\"DEV/APP\",upper(var.environment)) : upper(var.environment)}-OLCS-PRI-API-SG"
subnet_ids = ["${var.environment} =\"dev\" ? format(\"%s/%s\",\"DEV/APP\",upper(var.environment)) : upper(var.environment)}-OLCS-PRI-BATCH-1A", "${var.environment} =\"dev\" ? format(\"%s/%s\",\"DEV/APP\",upper(var.environment)) : upper(var.environment)}-OLCS-PRI-BATCH-1B", "${var.environment} =\"dev\" ? format(\"%s/%s\",\"DEV/APP\",upper(var.environment)) : upper(var.environment)}-OLCS-PRI-BATCH-1C"]
# `tags = {}` here is not applicable for spot
}
}
}

# Job queus and scheduling policies
job_queues = {
low_priority = {
name = "BatchTestLowPriorityFargate"
state = "ENABLED"
priority = 1

tags = {
JobQueue = "Low priority job queue"
}
}

high_priority = {
name = "BatchTestHighPriorityFargate"
state = "ENABLED"
priority = 99

fair_share_policy = {
compute_reservation = 1
share_decay_seconds = 3600

share_distribution = [{
share_identifier = "A1*"
weight_factor = 0.1
}, {
share_identifier = "A2"
weight_factor = 0.2
}]
}

tags = {
JobQueue = "High priority job queue"
}
}
}

job_definitions = {
job_configuration = {
name = "${var.job_definitions["processQueue"]["job_name"]}-job"
type = "container"
propagate_tags = true
platform_capabilities = ["FARGATE", ]

container_properties = jsonencode({
command = var.job_definitions["processQueue"]["command"],
image = var.job_definitions["processQueue"]["image"],
fargatePlatformConfiguration = {
platformVersion = "LATEST"
},
resourceRequirements = [
{ type = "VCPU", value = "1" },
{ type = "MEMORY", value = var.job_definitions["processQueue"]["memory"] },
],
jobRoleArn = "arn:aws:iam::054614622558:role/vol-app-dev-api-service-20240418150301367500000003"
})

attempt_duration_seconds = 60
retry_strategy = {
attempts = 3
evaluate_on_exit = {
retry_error = {
action = "RETRY"
on_exit_code = 1
}
exit_success = {
action = "EXIT"
on_exit_code = 0
}
}
}

tags = {
JobDefinition = "BatchTest"
}
}
// tags = local.default_tags
}
}
11 changes: 11 additions & 0 deletions infra/terraform/modules/service/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,14 @@ variable "services" {
description = "The services to deploy"
default = {}
}

variable "job_definitions" {
type = map(object({
job_name = string
command = list(string)
image = string
memory = string
}))
description = "The batch job defintion settings"
default = {}
}

0 comments on commit 5c8ee05

Please sign in to comment.