Terraform - Advanced Usage

Loops

Use meta-parameter count. Here we use this example to create 3 s3 bucket with different name.

  1. Create config file

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    # variable
    variable "bucket_names" {
    description = "Create s3 bucket with these names"
    type = "list"
    default = ["hulu-1", "hulu-2", "hulu-3"]
    }

    provider "aws" {
    region = "ap-northeast-1"
    }

    # S3
    # you can use count.index to get the index of each “iteration” in the “loop”.
    # you can accomplish the same thing by using count and two interpolation functions, element and length:
    # "${element(LIST, INDEX)}"
    # "${length(LIST)}"
    resource "aws_s3_bucket" "terraform_loop" {
    count = "${length(var.bucket_names)}"
    bucket = "${element(var.bucket_names, count.index)}"

    versioning {
    enabled = true
    }
    }

    # output
    # which bucket name you want to return by specifying its index in the list:
    # ${TYPE.NAME.INDEX.ATTRIBUTE}"
    # when you use the '*' character, you get back a list, so you need to wrap the output variable with brackets
    output "1st_bucket_names" {
    value = ["${aws_s3_bucket.terraform_loop.0.bucket}"]
    }
    output "bucket_names" {
    value = ["${aws_s3_bucket.terraform_loop.*.bucket}"]
    }
  2. Run terraform init, terraform plan and terraform apply commands

    Note that since the ‘*’ syntax returns a list, you can combine it with other
    interpolation functions, such as element.

If-statements

You accomplish the same thing by using the count parameter and taking advantage of two properties:

  1. In Terraform, if you set a variable to a boolean true (that is, the word true without any quotes around it), it will be coerced into a 1, and if you set it to a boolean false, it will be coerced into a 0.
  2. If you set count to 1 on a resource, you get one copy of that resource; if you set count to 0, that resource is not created at all.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    # variable
    variable "enable_s3" {
    description = "Create s3 bucket with these names"
    }

    # S3
    resource "aws_s3_bucket" "terraform_loop" {
    count = "${var.enable_s3)}"
    bucket = "if condition"

    versioning {
    enabled = true
    }
    }

    apply:

    1
    2
    3
    4
    5
    6
    7
    8
    provider "aws" {
    region = "ap-northeast-1"
    }

    module "module-test" {
    source = "../../module/s3"
    bucket_name = true
    }

To handle more complicated cases, you can again use the count parameter, but this time, rather than setting it to a boolean variable, you set it to the value returned by a conditional. Conditionals in Terraform use the same ternary syntax available in many programming
languages:
"${CONDITION ? TRUEVAL : FALSEVAL}"
For example, a more verbose way to do the simple if-statement from the previous section is as follows:
count = “${var.enable_s3 ? 1 : 0}”

If-else-statements

You can also use count to implement this:

1
2
3
count = "${var.enable_s3)}"

count = "${1 - var.enable_s3)}"

you can take advantage of the concat interpolation function to implement complicated If-Else-Statements:
"${concat(LIST1, LIST2, ...)}"

Zero-downtime deployment

This is exactly what the create_before_destroy lifecycle setting does!

As an added bonus, if something went wrong during the deployment, Terraform will automatically roll back!

唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!