Overview
This is the second part of the tutorial. If not done already, please read the first part: Terraform Scripting Concepts: Part 1 of 2
Using functions
Another particular characteristic of the Terraform Language is that it doesn’t support user-defined functions. Instead, it provides groups of built-in functions that can be used when assigning value to variables and attributes:
- Numeric
- String
- Collection
- Encoding
- Filesystem
- Date and Time
- Hash and Crypto
- Networking
- Type conversion
Use the following template for calling functions: FUNCTION_NAME(PARAMETER1, PARAMETER2, PARAMETERN)
For example:
|
|
Managing infrastructure
Up until now, the script takes no action. In order for Terraform to actually manage the target infrastructure and make changes to reach the required end-state, the script needs providers.
Terraform defines the following block types for working with the target infrastructure:
terraform
: used to set workspace-wide run-time options.required_providers
: used to declare what providers will be usedprovider
: used to define provider-specific options.resource
: used to create a logical representation of the managed resource. Each provider will further specialize the resource with attributes and subtypes.data
: optional block that is used to query infrastructure resources managed by the provider. The query result is then made available for other blocks to use as variables.
Declare Providers
Providers are declared by adding a required_providers
nested block to the terraform
block. Each provider will be assigned to an attribute of type map:
- Map name: used to reference the provider from other blocks.
- Attribute
version
: used to define version requirements. - Attribute
source
: defines from where the provider will be downloaded.
Additional provider-specific attributes can be defined by using the provider
block. The block will use the same label as the map name in required_providers
.
The following example declares two providers: aws
and azure
:
Define Managed Resources
Once the provider has been declared, it will make available additional resource
and data
types. For example:
- Provider: AWS
- Resource Type:
aws_instance
- Data Type:
aws_ami
- Resource Type:
- Provider: Azure
- Resource Type:
azurerm_linux_virtual_machine
- Data Type:
azurerm_image
- Resource Type:
Use the following template to define blocks:
The data and resource blocks can be used together to improve automation. In the following example, the data
block is used to query AWS for a particular AMI and then the resource
block will use the resulting AMI ID to create the VM instance:
|
|
Next Steps
Explore Terraform resources:
- Source code: GitHUB Repository
- Tool documentation: Manual
Learn advanced features:
Understand provider-specific implementation details:
Copyright information
This article is licensed under a Creative Commons Attribution 4.0 International License. For copyright information on the product or products mentioned inhere refer to their respective owner.
Disclaimer
Opinions presented in this article are personal and belong solely to me, and do not represent people or organizations associated with me in a professional or personal way. All the information on this site is provided “as is” with no guarantee of completeness, accuracy or the results obtained from the use of this information.