Skip to content

Commit

Permalink
feat: implement compute environment for aws batch
Browse files Browse the repository at this point in the history
  • Loading branch information
james.richardson committed Aug 17, 2023
1 parent ea809fc commit 3e93431
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 23 deletions.
68 changes: 68 additions & 0 deletions terraform/compute/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,71 @@ resource "aws_iam_role_policy_attachment" "policy_attachment" {
role = aws_iam_role.batch_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSBatchServiceRole"
}

resource "aws_iam_instance_profile" "batch_instance_profile" {
name = "BatchInstanceProfile" # Replace with your desired instance profile name

role = aws_iam_role.batch_role.id # Replace with the ID of the IAM role you want to associate
}



# Extract the IAM role ARN
output "batch_role_arn" {
value = aws_iam_role.batch_role.arn
}


# Define the AWS Batch job definition
resource "aws_batch_job_definition" "my_job_definition" {
name = "my-batch-job" # Replace with your desired job definition name
type = "container"

container_properties = jsonencode({
image = var.ecr_repository_url
resourceRequirements = [
{
type = "VCPU"
value = "4"
},
{
type = "MEMORY"
value = "8192"
}
]
})
}

# Create an AWS Batch compute environment
resource "aws_batch_compute_environment" "my_compute_environment" {
service_role = aws_iam_role.batch_role.arn # Replace with the actual Batch service role ARN
compute_environment_name = "my-compute-env" # Replace with your desired compute environment name
type = "MANAGED"

compute_resources {
type = "EC2"
instance_role = aws_iam_instance_profile.batch_instance_profile.arn # Replace with the actual instance role ARN
instance_type = ["m5.large"]
min_vcpus = 0
max_vcpus = 4
subnets = [var.subnet_id]
security_group_ids = [var.security_group_id]
}
}

# # Create an AWS Batch job queue
# resource "aws_batch_job_queue" "my_job_queue" {
# name = "my-job-queue" # Replace with your desired job queue name
# priority = 1
# compute_environment_order {
# order = 1
# compute_environment = aws_batch_compute_environment.my_compute_environment.arn
# }
# }

# # Create an AWS Batch job
# resource "aws_batch_job" "my_batch_job" {
# name = "my-batch-job-run" # Replace with your desired job name
# job_queue = aws_batch_job_queue.my_job_queue.arn
# job_definition = aws_batch_job_definition.my_job_definition.arn
# }
12 changes: 12 additions & 0 deletions terraform/compute/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
variable "ecr_repository_url" {
type = string
description = "Image URL for AWS Batch job"
}
variable "subnet_id" {
type = string
description = "Subnet ID for AWS Batch job"
}
variable "security_group_id" {
type = string
description = "Security group ID for AWS Batch job"
}
26 changes: 3 additions & 23 deletions terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -55,40 +55,20 @@ resource "aws_security_group" "batch_security_group" {

}

# Create a security group for EFS mount targets
resource "aws_security_group" "efs_security_group" {
name_prefix = "EFSSG-"
vpc_id = aws_vpc.my_vpc.id

# Allow inbound traffic from the Batch security group
ingress {
from_port = 2049
to_port = 2049
protocol = "tcp"
security_groups = [aws_security_group.batch_security_group.id]
}
tags = {
project = "ppp"
}

}

# Output the security group IDs
output "batch_security_group_id" {
value = aws_security_group.batch_security_group.id
}

output "efs_security_group_id" {
value = aws_security_group.efs_security_group.id
}




module storage {
source = "./storage"
}

module compute {
source = "./compute"
ecr_repository_url = module.storage.ecr_repository_url
subnet_id = aws_subnet.my_subnet.id
security_group_id = aws_security_group.batch_security_group.id
}
4 changes: 4 additions & 0 deletions terraform/storage/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ resource "aws_ecr_repository" "my_repository" {
]
}
}

output "ecr_repository_url" {
value = aws_ecr_repository.my_repository.repository_url
}

0 comments on commit 3e93431

Please sign in to comment.