You must be logged in to view saved presets
Creates an AWS Batch compute environment using EC2 instances with necessary roles and security settings.
data "aws_iam_policy_document" "batch_assume_role" {
statement {
actions = ["sts:AssumeRole"]
effect = "Allow"
principals {
identifiers = ["batch.amazonaws.com"]
type = "Service"
}
}
}
data "aws_iam_policy_document" "ec2_assume_role" {
statement {
actions = ["sts:AssumeRole"]
effect = "Allow"
principals {
identifiers = ["ec2.amazonaws.com"]
type = "Service"
}
}
}
resource "aws_batch_compute_environment" "sample" {
compute_environment_name = "sample"
compute_resources {
instance_role = aws_iam_instance_profile.ecs_instance_role.arn
instance_type = ["c4.large"]
max_vcpus = 16
min_vcpus = 0
placement_group = aws_placement_group.sample.name
security_group_ids = [aws_security_group.sample.id]
subnets = [aws_subnet.sample.id]
type = "EC2"
}
depends_on = ["aws_iam_role_policy_attachment.aws_batch_service_role"]
service_role = aws_iam_role.aws_batch_service_role.arn
type = "MANAGED"
}
resource "aws_iam_instance_profile" "ecs_instance_role" {
name = "ecs_instance_role"
role = aws_iam_role.ecs_instance_role.name
}
resource "aws_iam_role" "aws_batch_service_role" {
assume_role_policy = data.aws_iam_policy_document.batch_assume_role.json
name = "aws_batch_service_role"
}
resource "aws_iam_role" "ecs_instance_role" {
assume_role_policy = data.aws_iam_policy_document.ec2_assume_role.json
name = "ecs_instance_role"
}
resource "aws_iam_role_policy_attachment" "aws_batch_service_role" {
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSBatchServiceRole"
role = aws_iam_role.aws_batch_service_role.name
}
resource "aws_iam_role_policy_attachment" "ecs_instance_role" {
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
role = aws_iam_role.ecs_instance_role.name
}
resource "aws_placement_group" "sample" {
name = "sample"
strategy = "cluster"
}
resource "aws_security_group" "sample" {
egress {
cidr_blocks = ["0.0.0.0/0"]
from_port = 0
protocol = "-1"
to_port = 0
}
name = "aws_batch_compute_environment_security_group"
}
resource "aws_subnet" "sample" {
cidr_block = "10.1.1.0/24"
vpc_id = aws_vpc.sample.id
}
resource "aws_vpc" "sample" {
cidr_block = "10.1.0.0/16"
}