github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/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    # This module is now only being tested with Terraform 0.13.x. However, to make upgrading easier, we are setting
     8    # 0.12.26 as the minimum version, as that version added support for required_providers with source URLs, making it
     9    # forwards compatible with 0.13.x code.
    10    required_version = ">= 0.12.26"
    11  }
    12  
    13  # ---------------------------------------------------------------------------------------------------------------------
    14  # DEPLOY INTO THE DEFAULT VPC AND SUBNETS
    15  # To keep this example simple, we are deploying into the Default VPC and its subnets. In real-world usage, you should
    16  # deploy into a custom VPC and private subnets.
    17  # ---------------------------------------------------------------------------------------------------------------------
    18  
    19  data "aws_vpc" "default" {
    20    default = true
    21  }
    22  
    23  data "aws_subnet_ids" "all" {
    24    vpc_id = data.aws_vpc.default.id
    25  }
    26  
    27  # ---------------------------------------------------------------------------------------------------------------------
    28  # CREATE THE ECS CLUSTER
    29  # ---------------------------------------------------------------------------------------------------------------------
    30  
    31  resource "aws_ecs_cluster" "example" {
    32    name = var.cluster_name
    33  }
    34  
    35  # ---------------------------------------------------------------------------------------------------------------------
    36  # CREATE THE ECS SERVICE AND ITS TASK DEFINITION
    37  # ---------------------------------------------------------------------------------------------------------------------
    38  
    39  resource "aws_ecs_service" "example" {
    40    name            = var.service_name
    41    cluster         = aws_ecs_cluster.example.arn
    42    task_definition = aws_ecs_task_definition.example.arn
    43    desired_count   = 0
    44    launch_type     = "FARGATE"
    45  
    46    network_configuration {
    47      subnets = data.aws_subnet_ids.all.ids
    48    }
    49  }
    50  
    51  resource "aws_ecs_task_definition" "example" {
    52    family                   = "terratest"
    53    network_mode             = "awsvpc"
    54    cpu                      = 256
    55    memory                   = 512
    56    requires_compatibilities = ["FARGATE"]
    57    execution_role_arn       = aws_iam_role.execution.arn
    58    container_definitions    = <<-JSON
    59      [
    60        {
    61          "image": "terraterst-example",
    62          "name": "terratest",
    63          "networkMode": "awsvpc"
    64        }
    65      ]
    66  JSON
    67  
    68  }
    69  
    70  # ---------------------------------------------------------------------------------------------------------------------
    71  # CREATE THE ECS TASK EXECUTION ROLE AND ATTACH APPROPRIATE AWS MANAGED POLICY
    72  # ---------------------------------------------------------------------------------------------------------------------
    73  
    74  resource "aws_iam_role" "execution" {
    75    name               = "${var.cluster_name}-ecs-execution"
    76    assume_role_policy = data.aws_iam_policy_document.assume-execution.json
    77  }
    78  
    79  resource "aws_iam_role_policy_attachment" "execution" {
    80    role       = aws_iam_role.execution.id
    81    policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
    82  }
    83  
    84  data "aws_iam_policy_document" "assume-execution" {
    85    statement {
    86      effect  = "Allow"
    87      actions = ["sts:AssumeRole"]
    88      principals {
    89        type        = "Service"
    90        identifiers = ["ecs-tasks.amazonaws.com"]
    91      }
    92    }
    93  }
    94