5. Resources
Resources are the most important element in Terraform, and represent all the pieces of infrastructure you are managing within Terraform. Every resource is declared in the format:
Syntax
resource "resource_type" "resource_instance" {
# ... configuration options ...
}
resource_type
comes from your provider, e.g. akamai_property
, and can (and likely will) be reused many times. However, resource_instance
is the name or label you use for the instance of your resource type and as such must be unique. For example, a pair of property resources might look like this:
resource "akamai_property" "my-property" {
name = "my-property"
product_id = "prd_product"
contract_id = "ctr_C-0N7RAC7"
group_id = "grp_12345"
rule_format = "v2023-01-05"
}
resource "akamai_property" "my-other-property" {
name = "my-other-property"
product_id = "prd_product"
contract_id = "ctr_C-0N7RAC7"
group_id = "grp_12345"
rule_format = "v2023-01-05"
}
Your terraform files will likely be largely made up of resources, which can be often different types but it is common to have multiple instances of the same type.
Referencing Resources
When you create a resource in terraform it is common to need to reference some aspect of that resource. This is done in the following format:
<resource type>.<resource instance>.<instance attribute>
For example, if you were configuring a property resource and wanted to refer to the name of an edgehostname you had created above, you could do it like this:
resource "akamai_edge_hostname" "ehn" {
product_id = "prd_Fresca"
contract_id = data.akamai_contract.contract.id
group_id = data.akamai_group.group.id
ip_behavior = "IPV4"
edge_hostname = "www.example.com.edgesuite.net"
}
resource "akamai_property" "property" {
name = "property"
# ... other configuration ...
hostnames {
cname_from = "www.example.com"
cname_to = akamai_edge_hostname.ehn.edge_hostname
cert_provisioning_type = "CPS_MANAGED"
}
}
Here, we reference our instance of the akamai_edge_hostname
resource type whose label is ehn
. Then from that object we use the edge_hostname
attribute, and as such the whole reference becomes akamai_edge_hostname.ehn.edge_hostname
Exercises
Exercise #1
New File
Create a terraform file named security.tf with an akamai_appsec_configuration
resource and an akamai_appsec_security_policy
resource
Configure
Configure the akamai_appsec_security_policy
resource to get its config_id
from the data.akamai_appsec_configuration
.
Apply
Run a terraform apply
Reconfigure
Duplicate the policy resouce and make the necessary modifications to create a new policy.
Apply
Run another terraform apply
Destroy
Run a terraform destroy
. What happens?
Re-Apply
Re-apply your terraform code to recreate your resources as we may need them later on.
Commit
Commit your changes and push them to GitHub.
Exercise #2
New File
Create a terraform file named property.tf with the following resources:
- CP Code: use the resource
akamai_cp_code
to create a new CP code with thename
,contract_id
,group_id
,product_id
arguments - Edge Hostname: use the resource
akamai_edge_hostname
to create a new Edge Hostname using the required arguments according to the Akamai provider documentation. - Property: use the resource
akamai_property
to create a new property using the required arguments according to the Akamai provider documentation.
Configure
Observe you'll need to configure hostnames
, rules
and rule_format
for the akamai_property
too.
Hint: check the data.akamai_property_rules_builder
data source in
Techdocs.
Apply
Run another terraform apply
Commit
Commit your changes and push them to GitHub.