Provisioning Servers in the Cloud with Terraform

Provisioning Servers in the Cloud with Terraform

A software architect gives a quick tutorial on how to launch an instance of Terraform and host it an AWS provided cloud instance.

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.

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 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.

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:

Then go to uses:

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

Add the user to a group.

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

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. Thanks for reading

If you liked this post, share it with all of your programming buddies!

Originally published on https://dzone.com

cloud aws web-development devops

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire AWS Developer

Looking to Hire Professional AWS Developers? The technology inventions have demanded all businesses to use and manage cloud-based computing services and Amazon is dominating the cloud computing services provider in the world. **[Hire AWS...

Hire Dedicated DevOps Developers

Hire our Dedicated DevOps Developers who have in-depth skills and expertise to develop an interactive and secure web application. Get custom DevOps solutions for your project.

Hire Dedicated AWS Developer

Want to Hire AWS Developer for cloud computing services? At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, we leverage maximum benefits from the AWS platform ensuring prominent Solutions for business requirements....

Hire DevOps Developer

Looking to hire top DevOps developers at affordable prices? **[Hire DevOps Developer](https://hourlydeveloper.io/hire-dedicated-devops-developer/ "Hire DevOps Developer")** from **[HourlyDeveloper.io](https://hourlydeveloper.io/...

Multi-cloud Spending: 8 Tips To Lower Cost

Mismanagement of multi-cloud expense costs an arm and leg to business and its management has become a major pain point. Here we break down some crucial tips to take some of the management challenges off your plate and help you optimize your cloud spend.