github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/examples/terraform-http-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 AN EC2 INSTANCE THAT RUNS A SIMPLE "HELLO, WORLD" WEB SERVER
    15  # See test/terraform_http_example.go for how to write automated tests for this code.
    16  # ---------------------------------------------------------------------------------------------------------------------
    17  
    18  provider "aws" {
    19    region = var.aws_region
    20  }
    21  
    22  # ---------------------------------------------------------------------------------------------------------------------
    23  # DEPLOY THE EC2 INSTANCE
    24  # ---------------------------------------------------------------------------------------------------------------------
    25  
    26  resource "aws_instance" "example" {
    27    ami                    = data.aws_ami.ubuntu.id
    28    instance_type          = var.instance_type
    29    user_data              = data.template_file.user_data.rendered
    30    vpc_security_group_ids = [aws_security_group.example.id]
    31  
    32    tags = {
    33      Name = var.instance_name
    34    }
    35  }
    36  
    37  # ---------------------------------------------------------------------------------------------------------------------
    38  # CREATE A SECURITY GROUP TO CONTROL WHAT REQUESTS CAN GO IN AND OUT OF THE EC2 INSTANCE
    39  # ---------------------------------------------------------------------------------------------------------------------
    40  
    41  resource "aws_security_group" "example" {
    42    name = var.instance_name
    43  
    44    ingress {
    45      from_port = var.instance_port
    46      to_port   = var.instance_port
    47      protocol  = "tcp"
    48  
    49      # To keep this example simple, we allow incoming HTTP requests from any IP. In real-world usage, you may want to
    50      # lock this down to just the IPs of trusted servers (e.g., of a load balancer).
    51      cidr_blocks = ["0.0.0.0/0"]
    52    }
    53  }
    54  
    55  # ---------------------------------------------------------------------------------------------------------------------
    56  # CREATE THE USER DATA SCRIPT THAT WILL RUN DURING BOOT ON THE EC2 INSTANCE
    57  # ---------------------------------------------------------------------------------------------------------------------
    58  
    59  data "template_file" "user_data" {
    60    template = file("${path.module}/user-data/user-data.sh")
    61  
    62    vars = {
    63      instance_text = var.instance_text
    64      instance_port = var.instance_port
    65    }
    66  }
    67  
    68  # ---------------------------------------------------------------------------------------------------------------------
    69  # LOOK UP THE LATEST UBUNTU AMI
    70  # ---------------------------------------------------------------------------------------------------------------------
    71  
    72  data "aws_ami" "ubuntu" {
    73    most_recent = true
    74    owners      = ["099720109477"] # Canonical
    75  
    76    filter {
    77      name   = "virtualization-type"
    78      values = ["hvm"]
    79    }
    80  
    81    filter {
    82      name   = "architecture"
    83      values = ["x86_64"]
    84    }
    85  
    86    filter {
    87      name   = "image-type"
    88      values = ["machine"]
    89    }
    90  
    91    filter {
    92      name   = "name"
    93      values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"]
    94    }
    95  }
    96