Hostname: $(hostname)
Internal IP: $INT_IP
External IP: $EXT_IP
A Packer built, Terraform deployed, GCP Auto Scalling, Managed Instance Group with Load Balancer serving Apache Web Server
92 | 93 | 94 | SCRIPT 95 | } 96 | 97 | network_interface { 98 | network = "${var.network}" 99 | # 100 | # Give a Public IP to instance(s) 101 | #access_config { 102 | # // Ephemeral IP 103 | #} 104 | } 105 | 106 | service_account { 107 | scopes = ["userinfo-email", "compute-ro", "storage-ro"] 108 | } 109 | 110 | lifecycle { 111 | create_before_destroy = true 112 | } 113 | } 114 | # 115 | # Compute Healthcheck 116 | resource "google_compute_health_check" "default" { 117 | name = "${var.hc_name}" 118 | check_interval_sec = 1 119 | timeout_sec = 1 120 | 121 | tcp_health_check { 122 | port = "${var.hc_port}" 123 | } 124 | } 125 | # 126 | # Regional MIG AutoScaler 127 | resource "google_compute_region_autoscaler" "cras" { 128 | name = "${var.rmig_as_name}" 129 | region = "${var.region}" 130 | target = "${google_compute_region_instance_group_manager.rmig.self_link}" 131 | 132 | autoscaling_policy = { 133 | max_replicas = 5 134 | min_replicas = 3 135 | cooldown_period = 60 136 | cpu_utilization { 137 | target = 0.5 138 | } 139 | } 140 | } 141 | # 142 | # Global Forwarding Rule 143 | resource "google_compute_global_forwarding_rule" "gfr" { 144 | name = "${var.gfr_name}" 145 | target = "${google_compute_target_http_proxy.thp.self_link}" 146 | port_range = "${var.gfr_portrange}" 147 | } 148 | resource "google_compute_target_http_proxy" "thp" { 149 | name = "${var.thp_name}" 150 | url_map = "${google_compute_url_map.urlmap.self_link}" 151 | } 152 | resource "google_compute_url_map" "urlmap" { 153 | name = "${var.urlmap_name}" 154 | default_service = "${google_compute_backend_service.rbs.self_link}" 155 | } 156 | # 157 | # Firewall rules for specific Tags 158 | resource "google_compute_firewall" "default" { 159 | name = "${var.network}-${var.fwr_name}" 160 | network = "${var.network}" 161 | project = "${var.project}" 162 | 163 | allow { 164 | protocol = "tcp" 165 | ports = ["80", "443"] 166 | } 167 | } 168 | -------------------------------------------------------------------------------- /MIG/apache-mig-glb/variables.tf: -------------------------------------------------------------------------------- 1 | # Creds and default location 2 | variable "credentials" { default = "your_account.json" } // Change with you service account .json file 3 | variable "project" { default = "your_project_id" } // Your GCP Project ID 4 | variable "region" { default = "northamerica-northeast1" } 5 | variable "zone" { default = "northamerica-northeast1-a" } 6 | # 7 | # Instance Template 8 | variable "prefix" { default = "apache-" } 9 | variable "desc" { default = "This template is used to create Apache server instances." } 10 | variable "tags" { default = "webserver" } 11 | variable "desc_inst" { default = "Apache Web server instance" } 12 | variable "machine_type" { default = "n1-standard-1" } 13 | variable "source_image" { default = "apache" } //This is the family tag used when building the Golden Image with Packer. 14 | variable "network" { default = "default" } 15 | # 16 | # Managed Instace Group 17 | variable "rmig_name" { default = "apache-rmig" } 18 | variable "base_instance_name" { default = "apache" } 19 | variable "target_size" { default = "3" } 20 | # 21 | # Healthcheck 22 | variable "hc_name" { default = "apache-healthcheck" } 23 | variable "hc_port" { default = "80" } 24 | # 25 | # Backend 26 | variable "be_name" { default = "http-backend" } 27 | variable "be_protocol" { default = "HTTP" } 28 | variable "be_port_name" { default = "http" } 29 | variable "be_timeout" { default = "10" } 30 | variable "be_session_affinity" { default = "NONE" } 31 | # 32 | # RMIG Autoscaler 33 | variable "rmig_as_name" { default = "rmig-as" } 34 | # 35 | # Global Forwarding Rule 36 | variable "gfr_name" { default = "website-forwarding-rule" } 37 | variable "gfr_portrange" { default = "80" } 38 | variable "thp_name" { default = "http-proxy" } 39 | variable "urlmap_name" { default = "http-lb-url-map" } 40 | # 41 | # Firewall Rules 42 | variable "fwr_name" { default = "allow-http-https" } 43 | -------------------------------------------------------------------------------- /MIG/apache/README.md: -------------------------------------------------------------------------------- 1 | # gcp/MIG/apache 2 | Terraform script to create a an instance template from a Packer built image followed by the creation of an instance group with a minimum of 3 Apache Server with a burst to 5 instances running. It also opens port 80 and 443 in firewall rules for default network for target with the tag webserver. 3 | 4 | Scripts uses a GCP service account and a JSON file with your account token and VARS defined in variables.tf 5 | 6 | The Golden Image used for template creation can be created with Packer and this script: https://github.com/sveronneau/gcp/blob/master/packer/apache.json 7 | -------------------------------------------------------------------------------- /MIG/apache/instance_template_and_mig_apache.tf: -------------------------------------------------------------------------------- 1 | provider "google" { 2 | credentials = "${var.credentials}" 3 | project = "${var.project}" 4 | region = "${var.region}" 5 | } 6 | # 7 | # Template creation 8 | resource "google_compute_instance_template" "instance_template" { 9 | name_prefix = "${var.prefix}" 10 | description = "${var.desc}" 11 | project = "${var.project}" 12 | region = "${var.region}" 13 | tags = ["${var.tags}"] 14 | instance_description = "${var.desc_inst}" 15 | machine_type = "${var.machine_type}" 16 | can_ip_forward = false // Whether to allow sending and receiving of packets with non-matching source or destination IPs. This defaults to false. 17 | 18 | scheduling { 19 | automatic_restart = true 20 | on_host_maintenance = "MIGRATE" 21 | } 22 | 23 | // Create a new boot disk from an image (Lets use one created by Packer) 24 | disk { 25 | source_image = "${var.source_image}" 26 | auto_delete = true 27 | boot = true 28 | } 29 | 30 | metadata { 31 | startup-script = <