Last active
          March 4, 2018 12:51 
        
      - 
      
- 
        Save dalethestirling/1d7473dc8ec38f2b74a50b75e4ef59bc to your computer and use it in GitHub Desktop. 
    Decision toggles in Terraform
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | variable "decision_toggle" { default="false" } | |
| variable "val_a" { default="3" } | |
| variable "val_b" { default="3" } | |
| output "toggle_result_0" { | |
| value = "${ var.decision_toggle ? var.val_a : 0 }" | |
| } | |
| output "toggle_result_1" { | |
| value = "${ var.decision_toggle ? var.val_a : var.val_a+var.val_b }" | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | variable "dedicated_etcd" { default="false" } | |
| variable "etcd_hosts" { default="3" } | |
| variable "app_hosts" { default="3" } | |
| resource "aws_instance" "etcd" { | |
| count = "${ var.decision_toggle ? etcd_hosts : 0 }" | |
| ami = "ami-b6bb47d4" | |
| instance_type = "t2.micro" | |
| tags { | |
| is_etcd = "true" | |
| is_app = "false" | |
| } | |
| } | |
| resource "aws_instance" "app" { | |
| count = "${ var.decision_toggle ? var.app_hosts : var.app_hosts+var.etcd_hosts }" | |
| ami = "ami-b6bb47d4" | |
| instance_type = "t2.micro" | |
| tags { | |
| is_etcd = "${ var.decision_toggle ? "false" : "true" }" | |
| is_app = "true" | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | variable "dmz_subnet" { default="false" } | |
| variable "web_hosts" { default="3" } | |
| variable "api_hosts" { default="3" } | |
| data "aws_subnet" "public_network_zone" { | |
| filter { | |
| name = "tag:NetworkZone" | |
| values = ["public"] | |
| } | |
| } | |
| data "aws_subnet" "dmz_network_zone" { | |
| filter { | |
| name = "tag:NetworkZone" | |
| values = ["dmz"] | |
| } | |
| } | |
| resource "aws_instance" "web" { | |
| count = "${ var.web_hosts }" | |
| ami = "ami-b6bb47d4" | |
| instance_type = "t2.micro" | |
| subnet_id = "${element(data.aws_subnet.public_network_zone.*.id, count.index)}" | |
| tags { | |
| in_dmz = "false" | |
| } | |
| } | |
| resource "aws_instance" "dmz" { | |
| count = "${ var.dmz_subnet }" | |
| ami = "ami-b6bb47d4" | |
| instance_type = "t2.micro" | |
| subnet_id = "${var.dmz_toggle ? element(data.aws_subnet.public_network_zone.*.id, count.index) : element(data.aws_subnet.public_network_zone.*.id, count.index)}" | |
| tags { | |
| in_dmz = "${ var.dmz_subbnet ? "true" : "false" }" | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment