Skip to content

Notes on Infrastructure Script

Richard Hightower edited this page Nov 28, 2017 · 1 revision

Initial plan for terraform

/tf-aws-infrastructure/outputs.tf

output "tags" {
  description = "Tags used to tag resources like EC2 instances, ELBs, etc."
  value = {
    environment_region = "${join("-", list(var.environment,var.region))}"
    unique_name = "${var.unique_name}"
    terraform_created = "true"
    created_date = "${timestamp()}"
    maid_downtime = "${var.tag_maid_downtime_map["${var.environment}-${var.region}"]}"
    Name = "${join("-", compact(list(var.name, var.role, var.az_letter, var.type, var.environment, var.region,  var.unique_name)))}"
  }
}

output "Name" {
  value = "${join("-", compact(list(var.name, var.role, var.az_letter, var.type, var.environment, var.region, var.az_letter, var.unique_name)))}"
}

output unique_name {
  value = "${var.unique_name}"
}

output environment_region {
  value = "${join("-", list(var.environment,var.region))}"
}
output environment {
  value = "${var.environment}"
}
output user_profile {
  value = "${lookup(var.user_profile_map, join("-", list(var.environment,var.region)))}"
}
output region {
  value = "${var.region}"
}
output ami {
  value = "${lookup(var.ami_map, join("-", list(var.environment,var.region,var.os)))}"
}

output cloud_maid_tag {
  value = "${var.tag_maid_downtime_map["${var.environment}-${var.region}"]}"
}

output os {
  value = "${var.os}"
}
output instance_type {
  value = "${lookup(var.instance_type_map, join("-", list(var.environment,var.region)))}"
}

output availability_zone {
  value = "${lookup(zipmap(list("a","b","c"), split(",", lookup(var.az_csv_map, join("-", list(var.environment,var.region))))),var.az_letter)}"
}

output "az_map" {
  value = "${zipmap(list("a","b","c"), split(",", lookup(var.az_csv_map, join("-", list(var.environment,var.region)))))}"
}

output "subnets" {
  value = "${split(",", lookup(var.subnet_csv_map, join("-", list(var.environment,var.region))))}"
}

output "subnet_map" {
  value = "${zipmap(list("a","b","c"), split(",", lookup(var.subnet_csv_map, join("-", list(var.environment,var.region)))))}"
}

output "iam_role_map" {
  value = "${var.iam_role_map}"
}


output "iam_role" {
  value = "${var.iam_role_map[join("-", list(var.environment,var.region))]}"
}

output "subnet" {
  value = "${lookup(zipmap(list("a","b","c"), split(",", lookup(var.subnet_csv_map, join("-", list(var.environment,var.region))))),var.az_letter)}"
}


output "instance_security_group_list" {
  value = "${split(",", lookup(var.instance_security_group_csv_map, join("-", list(var.environment,var.region))))}"
}

output "az_csv_map" {
  value = "${var.az_csv_map}"
}


output "linux_user_map" {
  value = "${var.linux_user_map}"
}

output "linux_user" {
  value = "${var.linux_user_map[var.os]}"
}




/tf-aws-infrastructure/variables.tf

variable "name" {
  description = "Name of thing you are creating, i.e., matching-engine, shopper-tools."
}

variable "role" {
  description = "Role of thing you are creating, i.e., kafka-node0, cassandra-cluster-node1."
  default = ""
}

variable "type" {
  description = "Type of thing you are creating, i.e., ec2instance, ebsvolume, elb, alb, asg, etc.."
  default = ""
}


variable "unique_name" {
  description = "unique name for system"
}

variable "environment" {
  description = "environment only, dev, qa, prod."
}

variable "region" {
  description = "region only"
}

variable "os" {
  description = "Operating system to use: centos, redhat, AWS Linux"
}

variable "az_letter" {
  description = "AZ letter a, b, c"
}

# ================================================================================ #
# map variables, build output based on environment / region / environment_region
# ================================================================================ #
variable "user_profile_map" {
  type = "map"
  default = {
    dev-us-east-1 = "DEV_USER"
    qa-us-east-1 = "DEV_USER"
    prod-us-east-1 = "???"
    dev-us-west-2 = "DEV_USER"
    qa-us-west-2 = "DEV_USER"
    prod-us-west-2 = "???"
  }
}


variable "ami_map" {
  type = "map"
  default = {
    dev-us-east-1-amazon = "ami-12345678"
    qa-us-east-1-amazon = "ami-12345678"
    prod-us-east-1-amazon = "???"
    dev-us-west-2-amazon = "ami-12342016"
    qa-us-west-2-amazon = "ami-12342016"
    prod-us-west-2-amazon = "???"
    prod-us-east-1-centos = "ami-cde03bb7"
    qa-us-east-1-centos = "ami-97eb3aed"
    dev-us-east-1-centos = "ami-97eb3aed"
  }
}

variable "instance_type_map" {
  type = "map"
  default = {
    dev-us-east-1 = "m4.large"
    qa-us-east-1 = "m4.2xlarge"
    prod-us-east-1 = "m4.2xlarge"
    dev-us-west-2 = "m4.large"
    qa-us-west-2 = "m4.2xlarge"
    prod-us-west-2 = "m4.2xlarge"
  }
}


variable "iam_role_map" {
  type = "map"
  default = {
    dev-us-east-1 = "MainEC2-IAM-ROLE"
    qa-us-east-1 = "MainEC2-IAM-ROLE"
    prod-us-east-1 = "???"
    dev-us-west-2 = "MainEC2-IAM-ROLE"
    qa-us-west-2 = "MainEC2-IAM-ROLE"
    prod-us-west-2 = "???"
  }
}


variable "tag_maid_downtime_map" {
  type = "map"
  default = {
    dev-us-east-1 = "Offhours tz=PT"
    qa-us-east-1 = "off"
    prod-us-east-1 = "off"
    dev-us-west-2 = "Offhours tz=PT"
    qa-us-west-2 = "off"
    prod-us-west-2 = "off"
  }
}

variable "tag_sns_topic_arn_map" {
  type = "map"
  default = {
    dev-us-east-1 = "none"
    qa-us-east-1 = "none"
    prod-us-east-1 = "none"
    dev-us-west-2 = "none"
    qa-us-west-2 = "none"
    prod-us-west-2 = "none"
  }
}


variable "subnet_csv_map" {
  type = "map"
  default = {
    dev-us-east-1 = "subnet-123456a,subnet-567890b,subnet-101112c"
    qa-us-east-1 = "subnet-123456a,subnet-567890b,subnet-101112c"
    prod-us-east-1 = "???"
    dev-us-west-2 = "subnet-126758a,subnet-347898b,subnet-568907c"
    qa-us-west-2 = "subnet-126758a,subnet-347898b,subnet-568907c"
    prod-us-west-2 = "???"
  }
}

variable "instance_security_group_csv_map" {
  type = "map"
  default = {
    dev-us-east-1 = "sg-1234567"
    # CIGShoppingCart-SERVICES, CODE-NonWAFInstances, Shared-Services-QA-CODE-SharedServicesQASG-1M8XW6XJW0RYT
    qa-us-east-1 = "sg-1234567"
    # CIGShoppingCart-SERVICES, CODE-NonWAFInstances, Shared-Services-QA-CODE-SharedServicesQASG-1M8XW6XJW0RYT
    prod-us-east-1 = "???"
    dev-us-west-2 = "sg-9876543"
    # CIGShoppingCart-SERVICES, CODE-NonWAFInstances, Shared-Services-QA-CODE-SharedServicesQASG-1M8XW6XJW0RYT
    qa-us-west-2 = "sg-9876543"
    # CIGShoppingCart-SERVICES, CODE-NonWAFInstances, Shared-Services-QA-CODE-SharedServicesQASG-1M8XW6XJW0RYT
    prod-us-west-2 = "???"
  }
}


variable "az_csv_map" {
  type = "map"
  default = {
    dev-us-east-1 = "us-east-1a,us-east-1b,us-east-1c"
    qa-us-east-1 = "us-east-1a,us-east-1b,us-east-1c"
    prod-us-east-1 = "???"
    dev-us-west-2 = "us-west-2a,us-west-2b,us-west-2c"
    qa-us-west-2 = "us-west-2a,us-west-2b,us-west-2c"
    prod-us-west-2 = "???"
  }
}


variable "linux_user_map" {
  type = "map"
  default = {
    centos = "centos"
    rhel   = "ec2-user"
  }
}

/tf-aws-infrastructure/test/main.tf


module "infra" {
  //source = "git::https://github.shc.cloudurable.com/jpAzar/tf-aws-infrastructure"
  source = "../"
  region="us-east-1"
  environment="dev"
  unique_name = "abc123"
  os = "centos"
  az_letter = "a"
  name ="shopperTools"
  role = "kafkaNode0"
  type = "ec2instance"
}
//
//output "availability_zone" {
//  value = "${module.infra.availability_zone}"
//}
//
//output "subnet" {
//  value = "${module.infra.subnet}"
//}


output "tags" {
  value = "${module.infra.tags}"
}

output "Name" {
  value = "${module.infra.Name}"
}
//
//output "cm" {
//  value = "${module.infra.cloud_maid_tag}"
//}


//
//output "environment_region" {
//  value = "${module.infra.environment_region}"
//}
//
//output "user_profile" {
//  value = "${module.infra.user_profile}"
//}
//
//output "region" {
//  value = "${module.infra.region}"
//}
//
//output "ami" {
//  value = "${module.infra.ami}"
//}
//
//
//output "os" {
//  value = "${module.infra.os}"
//}
//
//output "az_map" {
//  value = "${module.infra.az_map}"
//}
//
//
//
//output "az_map_a" {
//  value = "${module.infra.az_map["a"]}"
//}
//
//
//output "az_csv_map" {
//  value = "${module.infra.az_csv_map}"
//}
//
//output "subnet_map" {
//  value = "${module.infra.subnet_map}"
//}
//
//
//
//output "unique_name" {
//  value = "${module.infra.unique_name}"
//}
//
//output "security_groups" {
//  value = "${module.infra.instance_security_group_list}"
//}
//
//
//
//output "iam_role" {
//  value = "${module.infra.iam_role}"
//}