The count Meta-Argument
The count
meta-argument accepts a number and creates as many instances of a resource without having to write the same configuration block multiple times. Think of it as a simple loop mechanism.
Example #1
For example, if we need to create multiple CP Codes:
resource "akamai_cp_code" "my_cp_code" {
count = 5
name = "My CP Code"
contract_id = "C-0N7RAC7"
group_id = "12345"
product_id = "prd_Object_Delivery"
}
Because count=5
, 5 new CP codes will be created on Akamai, and as far as Terraform is concerned 5 instances of CP codes were created which you can refer to in other parts of your Terraform code:
# Individual instance
akamai_cp_code.my_cp_code[0] # First instance
akamai_cp_code.my_cp_code[1] # Second instance
akamai_cp_code.my_cp_code[2] # Third instance
akamai_cp_code.my_cp_code[3] # Fourth instance
akamai_cp_code.my_cp_code[4] # Fifth instance
# All instances
akamai_cp_code.my_cp_code[*] # All instances as a list
However, all the CP codes will be named the same ("My CP Code") which is not ideal.
Example #2
Let's review another example where we need to deploy several records of the same type to EdgeDNS:
variable "hostnames" {
default = ["www.example.com", "api.example.com", "blog.example.com"]
}
resource "akamai_dns_record" "my_dns_hostnames" {
count = length(var.hostnames)
zone = "example.com"
recordtype = "CNAME"
ttl = 60
target = ["example.com.edgekey.net"]
name = var.hostnames[count.index]
}
In this case we are not explicitly giving count
a number and instead we are telling it to use the length or our hostnames
array which is 3. With this approach we can use the values in our array to fill in some of the arguments such as the name
. count.index
gives you the current iteration number (0, 1, 2, etc.).
That index is used to pick the corresponding hostname from your list
First record (index 0): www.example.com
Second record (index 1): api.example.com
Third record (index 2): blog.example.com
Similarly the created instances can be referenced as:
akamai_dns_record.my_dns_hostnames[0] # First instance
akamai_dns_record.my_dns_hostnames[1] # Second instance
akamai_dns_record.my_dns_hostnames[2] # Third instance
Exercise
-
You need an existing EdgeDNS zone where you can make changes. If you don't have an EdgeDNS zone, then create one outside of Terraform (we'll explain why in a bit). Chose any domain of your preference for the new EdgeDNS zone.
-
You should have a list of hostnames in a local variable named
apps_hostnames
from the previous exercise in module For Expressions.
New File
Create a new dns.tf file and add the akamai_dns_record
resource. Remember that the DNZ zone name
must match your already deployed zone name.
Configure
Using count
configure akamai_dns_record
to create as many CNAME records as entries in apps_hostnames
.
Plan/Apply
Run terraform plan
to confirm your changes. Run terraform apply
if all looks in order.
As you can notice, Terraform will create the DNS records in the zone even if the zone is not managed by Terraform. The DNS records and zone are independent resources. This allows to manage only specific resources as code.
Commit
Commit your change and push to GitHub.