{{announcement.body}}
{{announcement.title}}

Provisioning Servers in the Cloud With Terraform

DZone 's Guide to

Provisioning Servers in the Cloud With Terraform

The main idea of DevOps is automation and making software delivery vastly more efficient. Thinking about these requirements, let’s analyze one of the main tools for providing servers in the cloud.

· Cloud Zone ·
Free Resource

Today there are many tools for the provisioning of infrastructure: Vagrant, CloudFormation, OpenStack Heat, and many others. This article speaks about Terraform: the best software tool for provisioning in the cloud under various important aspects.

Terraform is an open source infrastructure as code tool created by Hashicorp and written in Go. With Terraform, you can describe your infrastructure as code, define the provider,  and deploy and update your app. It is important to mention that the Terraform is not a configuration management tool.

Terraform

Terraform can be used to create servers, databases, and load balancers.

Let's dive into some hands-on work.

The first mission is to deploy a server in AWS. For this, we can write the following script in a main.tf file:

provider "aws" {  
  region = "us-east-1"
}
resource "aws_instance" "example" 
{  
  ami = "ami-0c6b1d09930fac512"  
  instance_type = "t2.micro"  
  count = 2  
  tags 
  {    
    Name = "example"  
  }
}

In the first line of the code block, usingprovider, we define the name of the cloud provider: AWS, Google, Azure, etc. Inside this code, we also describe our properties. For this exampl,e let’s use only the property region property.

In the next bit of code, we define the resources for the provider. AWS has the following:

  • ami: ami stands for 'Amazon Machine Image.' It can based on Linux, Ubuntu, CentOS or any other available image.
  • instance_type: In Amazon AWS, each instance type offers different compute, memory, and storage capabilities.
  • count: the number of instances.
  • tags: AWS allows you to associate tags with your instance.

There are many other properties possible for AWS providers using Terraform. You can check here.

Before you run the commands to process the script, it is necessary to prepare the account in Amazon, so that Terraform can do make the changes it needs in order to create a user with an access key and a secret access key. Let’s go!

First, let’s access the IAM menu:

IAM Menu

Then go to uses:

IAM Menu

Give a name and check the “Programmatic access” option:

Add the user to a group.

Image title

If the group still does not exist, you can create a group with the following permissions (this depends on your need):

Image title

This is step is finished with the following screen:

Now we can go to the command line to do the Terraform work for us. First, let’s set the credentials:

$ export AWS_ACCESS_KEY_ID=<your-access-key>
$ export AWS_SECRET_ACCESS_KEY=<your-secret-access-key>

Then, run the following commands lines in that order:

$ terraform init
$ terraform plan
$ terraform apply

The first command, init, initializes Terraform. The second command, plan , creates an execution plan. And finally, the apply, builds or changes the infrastructure.

After the last command, we will be asked if we want to perform the commands. Type “yes” to continue.

Plan: 2 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
 Terraform will perform the actions described above.
 Only ‘yes’ will be accepted to approve.

Enter a value: yes

If all goes well the last message showed will be:

aws_instance.example.1: Still creating… (10s elapsed)
aws_instance.example.0: Still creating… (10s elapsed)
aws_instance.example.0: Still creating… (20s elapsed)
aws_instance.example.1: Still creating… (20s elapsed)
aws_instance.example.1: Still creating… (30s elapsed)
aws_instance.example.0: Still creating… (30s elapsed)
aws_instance.example.1: Still creating… (40s elapsed)
aws_instance.example.0: Still creating… (40s elapsed)
aws_instance.example[1]: Creation complete after 50s (ID: i-0992c0f764296372e)
aws_instance.example[0]: Creation complete after 50s (ID: i-06cc71f7583de10d7)
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

On the dashboard of our AWS console, we will be shown the following:

To delete for the servers created type the following command:

$ terraform destroy

If successful, you will see the following messages:

Plan: 0 to add, 0 to change, 2 to destroy.

Do you really want to destroy?
 Terraform will destroy all your managed infrastructure, as shown above.
 There is no undo. Only ‘yes’ will be accepted to confirm.

Enter a value: yes

aws_instance.example[1]: Destroying… (ID: i-0992c0f764296372e)
aws_instance.example[0]: Destroying… (ID: i-06cc71f7583de10d7)
aws_instance.example.0: Still destroying… (ID: i-06cc71f7583de10d7, 10s elapsed)
aws_instance.example.1: Still destroying… (ID: i-0992c0f764296372e, 10s elapsed)
aws_instance.example.0: Still destroying… (ID: i-06cc71f7583de10d7, 20s elapsed)
aws_instance.example.1: Still destroying… (ID: i-0992c0f764296372e, 20s elapsed)
aws_instance.example[1]: Destruction complete after 26s
aws_instance.example[0]: Destruction complete after 26s

Destroy complete! Resources: 2 destroyed.

Now the dashboard is updated and we should see the following screen:

This was the last step of this tutorial. 

The script of the example this article is available on my GitHub repo.

Topics:
terraform tutorial ,devops ,infrastructure as a code ,cloud ,aws

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}