Provisioning deployable infrastructure over Cloud (AWS + TERRAFORM)

Saurabh Dimri
5 min readJun 14, 2020

We all know what is a Cloud…

Cloud or Cloud computing is the delivery of different services through the Internet. These resources include tools and applications like data storage, servers, databases, networking, and software.

Rather than keeping files on a proprietary hard drive or local storage device, cloud-based storage makes it possible to save them to a remote database. As long as an electronic device has access to the web, it has access to the data and the software programs to run it.

Cloud computing is a popular option for people and businesses for a number of reasons including cost savings, increased productivity, speed and efficiency, performance, and security.

Now What is Terraform…
Terraform
is an opensource infrastructure as a code software tool created by HashiCorp. It enables users to define and provision a data center infrastructure using a high-level configuration language known as Hashicorp Configuration Language (HCL), or optionally JSON. Terraform supports a number of cloud infrastructure providers such as Amazon Web Services, IBM Cloud (formerly Bluemix), Google Cloud Platform, DigitalOcean, Linode, Microsoft Azure, Oracle Cloud Infrastructure, OVH , Scaleway, VMware vSphere or Open Telekom Cloud as well as OpenNebula and OpenStack.

Advantages that Terraform provides us are :

  • Immutable infrastructure
  • Declarative, not procedural code
  • Client-only architecture
  • Orchestration, not merely configuration.

Blog's Target:

At the end of the Blog it will help you to have some basic understanding for the AWS as well as Terraform. How to do tasks over terraform, Intelligence of terraform and why it is beneficial to use the same.
Finally after learning several pieces we will launch a complete deployable infrastructure for a website over Apache-HTTP server using several features of AWS which will be explained.

Let’s Try out some scripts in Terraform to orchestrate AWS Cloud…

Creating a Security Group:
A security group acts as a virtual firewall for your instance to control incoming and outgoing traffic. Inbound rules control the incoming traffic to your instance, and outbound rules control the outgoing traffic from your instance. When you launch an instance, you can specify one or more security groups.
Let’s see the script:

We have to define a provider for whichever cloud we want to use.
Security Groups allowing SSH and HTTP

Creating Key-value Pairs using Terraform:
Amazon EC2 uses public key cryptography to encrypt and decrypt login information. Public key cryptography uses a public key to encrypt a piece of data, and then the recipient uses the private key to decrypt the data. The public and private keys are known as a key pair.

Creating Key-Pair for an EC2 instance

Creating an EBS:
Amazon Elastic Block Store (EBS) is an easy to use, high performance block storage service designed for use with Amazon Elastic Compute Cloud (EC2) for both throughput and transaction intensive workloads at any scale. A broad range of workloads, such as relational and non-relational databases, enterprise applications, containerized applications, big data analytics engines, file systems, and media workflows are widely deployed on Amazon EBS.

Creating an EBS in AWS.

Launching a complete AWS EC2 Instance using Terraform:
Amazon EC2 provides a wide selection of instance types optimized to fit different use cases. Instance types comprise varying combinations of CPU, memory, storage, and networking capacity and give you the flexibility to choose the appropriate mix of resources for your applications. Each instance type includes one or more instance sizes, allowing you to scale your resources to the requirements of your target workload.

To launch an EC2 instance over AWS, we will use all the above explained steps and inculcate them in one for form a proper infrastructure over Cloud.

EC2 instance runner script

Going Advance…

After covering over some of the basic use cases of the AWS cloud and it’s basic and most common services, let’s quickly try and spin up some more advanced AWS services which will help us to configure our infrastructure better and faster.

Spinning up an AWS S3 Bucket:
Amazon Simple Storage Service (Amazon S3) is an object storage service that offers industry-leading scalability, data availability, security, and performance. This means customers of all sizes and industries can use it to store and protect any amount of data for a range of use cases, such as websites, mobile applications, backup and restore, archive, enterprise applications, IoT devices, and big data analytics.

Launching an S3 bucket with public read access

Setting up Cloud Front Distribution:
Amazon CloudFront is a fast content delivery network (CDN) service that securely delivers data, videos, applications, and APIs to customers globally with low latency, high transfer speeds, all within a developer-friendly environment. CloudFront is integrated with AWS — both physical locations that are directly connected to the AWS global infrastructure, as well as other AWS services.

Launch the CDN for the S3 bucket

Now Let’s Put all the pieces together..

Let’s add all the different pieces together and launch a full fledged deployable infrastructure for our Web-App which we have created.
How the task works:
1. Create a security group and key for the instance
2. Launch an EC2 instance and launch another EBS to store the data.
3. Connect the EBS to the EC2.
4. Pull the data pushed by dev on github and mount it on the EBS.
5. Load the images in the S3 bucket and create a CDN system using cloud front.
A complete deployment infrastructure is created with faster connectivity and a greater data security…

The Full Source code link:
https://github.com/Saurabhdimri06/Terraform/tree/master/OrchastratingInfrastrucutre

To run the complete code just run the command

terraform apply

CODE RESULT

EC2 instance launched
EBS are launched and one is attached to the instance
Security Group with desired rules are created
The S3 bucket is also launched
And lastly the CDN is also created
And the profile is publically accessible

And the best part is we can take down the complete infrastructure we created just by using the command

terraform destroy

………………………………………………………………………………………

No computer is ever going to ask a new, reasonable question. It takes trained people to do that

— Grace Hopper

--

--

Saurabh Dimri

DevOps Enthusiast | Full Stack WebDeveloper | DevSecOps | Cloud Computing