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