DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Provisioning Servers in the Cloud With Terraform

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.

Marcelo Oliveira user avatar by
Marcelo Oliveira
·
Jul. 03, 19 · Tutorial
Like (6)
Save
Tweet
Share
14.45K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Terraform (software) Cloud Command (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Secure Your CI/CD Pipeline
  • Real-Time Stream Processing With Hazelcast and StreamNative
  • Upgrade Guide To Spring Data Elasticsearch 5.0
  • An Introduction to Data Mesh

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: