github.com/terraform-modules-krish/terratest@v0.29.0/examples/terraform-aws-ecs-example/main.tf (about) 1 # --------------------------------------------------------------------------------------------------------------------- 2 # PIN TERRAFORM VERSION TO >= 0.12 3 # The examples have been upgraded to 0.12 syntax 4 # --------------------------------------------------------------------------------------------------------------------- 5 6 terraform { 7 required_version = ">= 0.12" 8 } 9 10 # --------------------------------------------------------------------------------------------------------------------- 11 # DEPLOY INTO THE DEFAULT VPC AND SUBNETS 12 # To keep this example simple, we are deploying into the Default VPC and its subnets. In real-world usage, you should 13 # deploy into a custom VPC and private subnets. 14 # --------------------------------------------------------------------------------------------------------------------- 15 16 data "aws_vpc" "default" { 17 default = true 18 } 19 20 data "aws_subnet_ids" "all" { 21 vpc_id = data.aws_vpc.default.id 22 } 23 24 # --------------------------------------------------------------------------------------------------------------------- 25 # CREATE THE ECS CLUSTER 26 # --------------------------------------------------------------------------------------------------------------------- 27 28 resource "aws_ecs_cluster" "example" { 29 name = var.cluster_name 30 } 31 32 # --------------------------------------------------------------------------------------------------------------------- 33 # CREATE THE ECS SERVICE AND ITS TASK DEFINITION 34 # --------------------------------------------------------------------------------------------------------------------- 35 36 resource "aws_ecs_service" "example" { 37 name = var.service_name 38 cluster = aws_ecs_cluster.example.arn 39 task_definition = aws_ecs_task_definition.example.arn 40 desired_count = 0 41 launch_type = "FARGATE" 42 43 network_configuration { 44 subnets = data.aws_subnet_ids.all.ids 45 } 46 } 47 48 resource "aws_ecs_task_definition" "example" { 49 family = "terratest" 50 network_mode = "awsvpc" 51 cpu = 256 52 memory = 512 53 requires_compatibilities = ["FARGATE"] 54 execution_role_arn = aws_iam_role.execution.arn 55 container_definitions = <<-JSON 56 [ 57 { 58 "image": "terraterst-example", 59 "name": "terratest", 60 "networkMode": "awsvpc" 61 } 62 ] 63 JSON 64 65 } 66 67 # --------------------------------------------------------------------------------------------------------------------- 68 # CREATE THE ECS TASK EXECUTION ROLE AND ATTACH APPROPRIATE AWS MANAGED POLICY 69 # --------------------------------------------------------------------------------------------------------------------- 70 71 resource "aws_iam_role" "execution" { 72 name = "${var.cluster_name}-ecs-execution" 73 assume_role_policy = data.aws_iam_policy_document.assume-execution.json 74 } 75 76 resource "aws_iam_role_policy_attachment" "execution" { 77 role = aws_iam_role.execution.id 78 policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy" 79 } 80 81 data "aws_iam_policy_document" "assume-execution" { 82 statement { 83 effect = "Allow" 84 actions = ["sts:AssumeRole"] 85 principals { 86 type = "Service" 87 identifiers = ["ecs-tasks.amazonaws.com"] 88 } 89 } 90 } 91