github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/examples/terraform-packer-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 RUBY WEB APP BUILT USING A PACKER TEMPLATE
    15  # See test/terraform_packer_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                    = var.ami_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