Creating EC2 instance with Terraform

Resources are components of your infrastructure. It can be something as complex as a complete virtual server, or something as simple as a DNS record. Each resource belongs to a provider, and the type of the resource is suffixed with the provider name. The configuration of a resource takes the following form:

resource "provider-name_resource-type" "resource-name" { 
  parameter_name = parameter_value 
} 

The combination of resource type and resource name must be unique in your template; otherwise Terraform will complain.

There are three types of things you can configure inside resource block: resource-specific parameters, meta-parameters, and provisioners. For now, let's focus on resource-specific parameters. They are unique to each resource type.

We will create an EC2 instance. The aws_instance resource is responsible for this job. To create an instance, we need to set at least two parameters: ami and instance_type. Some parameters are required, whereas others are optional, ami and instance_type being the required ones.

Note

You can always check the complete list of available parameters in the docs, on the page dedicated to the particular resource. For example, to get the list and description of all the aws_instance resource parameters, check out at https://www.terraform.io/docs/providers/aws/r/instance.html.

We'll be using official Centos 7 AMI. As we configured AWS region to be eu-central-1, we have to use an AMI with ID ami-9bf712f4. We will use the t2.micro instance type, as it's the cheapest one and is available as part of Free Tier offering.

Update the template to look as follows:

# Provider configuration 
provider "aws" { 
  region = "eu-central-1" 
} 
# Resource configuration 
resource "aws_instance" "hello-instance" { 
  ami = "ami-9bf712f4" 
  instance_type = "t2.micro" 
  tags { 
    Name = "hello-instance" 
  } 
} 

Note

You might also need to specify the subnet_id parameter, in case you don't have a default VPC. For this, you will need to create a VPC and a subnet. You can either do it now yourself or wait till the next chapter, where we will be extending our template with VPC support. Don't worry if you don't know what VPC is. We will figure it out pretty soon.

As you noted, HCL allows commenting your code using hash sign in front of the text you want to be commented.

Another thing to look at is the tags parameter. Terraform is not limited to simple string values. You can also have numbers, boolean values (true, false), lists (["elem1", "elem2", "elem3"]), and maps. The tags parameter is a map of tags for the instance.

Let's apply this template!

$> terraform apply 
aws_instance.hello-instance: Creating... 
  ami:                      "" => "ami-378f925b" 
  < ......................... > 
  instance_type:            "" => "t2.micro" 
  key_name:                 "" => "<computed>" 
  < ......................... > 
  tags.%:                   "" => "1" 
  tags.Name:                "" => "hello-instance" 
  tenancy:                  "" => "<computed>" 
  vpc_security_group_ids.#: "" => "<computed>" 
aws_instance.hello-instance: Still creating... (10s elapsed) 
aws_instance.hello-instance: Still creating... (20s elapsed) 
aws_instance.hello-instance: Still creating... (30s elapsed) 
aws_instance.hello-instance: Creation complete 
 
Apply complete! Resources: 1 added, 0 changed, 0 destroyed. 
 
The state of your infrastructure has been saved to the path 
below. This state is required to modify and destroy your 
infrastructure, so keep it safe. To inspect the complete state 
use the `terraform show` command. 
State path: terraform.tfstate

Wow, that's a lot of output for a simple command creating a single instance. Some parts of it were replaced with arrow-wrapped dots, so don't be surprised when you will see even more parameter values when you actually run the command. Before digging into the output, let's first verify that the instance was really created in AWS Management Console:

Creating EC2 instance with Terraform

With just 12 lines of code and a single Terraform command invocation, we got our EC2 instance running. So far, the result we got is not that different from using AWS CLI, though: we only created a resource. What is of more interest is how we update and destroy this instance using the same template. To understand how Terraform does it, you need to learn what the state file is.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.88.249