HashiCorp Configuration Language
The syntax used for declaring Terraform configurations is called HashiCorp Configuration Language (HCL). Terraform uses configuration files written in HCL that are named with .tf
extension.
HashiCorp Configuration Language Syntax
Single line comments start with
#
.Multiline comments are wrappend with
/*
and*/
.Values are assigned with the syntac of
key = value
.Strings are in double quotes.
Strings can interpolate other values using syntax wrapped in
${}
, for example${var.foo}
.Numbers are assumed to be base 10.
true
andfalse
respresents boolean value.List of primitives can be create by using
[ ]
, for example["foo", "bar", "baz"]
.Maps can be made with
{ }
, for exampe{ "foo": "bar", "bar": "baz" }
.
Also, when it comes to common coding styles with HCL, it prefers indenting with two spaces for each nesting level and aligning multiple arguments with their equal sign.
Example
In this example I'm trying to deploy a Docker container.
Setup the environment:
Next let's create the Terraform program with main.tf
file,
main.tf
Validate main.tf
,
Terraform plan,
Apply the changes,
List the Docker containers,
Reset the environment,
Example
This example provides a couple of simple examples of different use cases with variables.
Create a faile called main.tf
.
In here, we first define a variable named myvar
of type string
and it's default value is hello terraform
. Next it we declare a variable of type map
with the name mymap
. In maps we can specify the type of value it is going to keep like this map(TYPE)
. In a similar approach, we can specify lists. Next, we have declared a variable of type list
with name myname
and it's default value is 1, 2, 3
, an array of integers.
These can be verified by invoking the Terraform console,
Check the value of myvar
,
Check the value of mymap
,
Check the value of mylist
,
Create a file called resources.tf
,
This file uses the AWS Terraform module. First it declares a variable to specify the AWS region with the default value eu-west-1
. Then it spefies an AMIS resource, and finally an EC2 instance. It is important to notice here that machine image (ami) is specifed by refering to the AMIS and AWS REGION variables that we declared.
Last updated