Create /Launch an application using terraform

Deepanshu Yadav
5 min readSep 19, 2020

--

Let’s first see the things we are going to do by terraform for launching a simple application

Prerequisites- Knowledge of ec2, s3, ebs, terraform and cloudfront.

1.Create the key and security group which allow the port 80.
2. Launch EC2 instance.
3. In this Ec2 instance use the key and security group which we have created in step 1.
4. Launch one Volume (EBS) and mount that volume into /var/www/html
5. Developer have uploaded the code into github repo also the repo has some images.
6. Copy the github repo code into /var/www/html
7. Create S3 bucket, and copy/deploy the images from github repo into the s3 bucket and change the permission to public readable.
8 Create a Cloudfront using s3 bucket(which contains images) and use the Cloudfront URL to update in code in /var/www/html

I AM GIVING LINK TO MY CODE FOR YOUR REFERENCE. READ THE BLOG TO UNDERSTAND HOW IT IS RUNNING. YOU CAN RUN THIS CODE AND TEST ONLY CHANGE YOUR PROFILE AND OTHER REQUIRED THINGS: https://github.com/dipuyadav/multicloud/blob/master/task1code

First you have to configure your system to run aws cli and then tell in the code to use your this account

provider “aws” {
region = “ap-south-1”
profile = “deepanshu2”

Now lets launch/configure s3 bucket. This can be used for storing your images, videos, etc that you may have mentioned in your code.

take reference/examples from here- https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket

Before giving name to your s3 bucket, confirm that the name is uique in that region otherwise it will give error. you can simply go to s3 services on aws console and manually check if name is present or not.

Now for configuring cloudfront see the below part of code

see this link of documentary of terraform cloudfront if you get any problem : https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_distribution

You can also create your seprate key for this project or can use the eisting one. i have created a new as shown below

Now for security lets create a security group. you can give any ingress and outgress rule here according to your project and need

Now i am launching an instance and also doing remote login using the private key as shown below

For creating and attaching EbS to our instance, follow the below code

Now the last thing is to format and mount the ebs volume (where our code would be stored). Below is the code for that

our task is done now. after everything worked successfully you can write some local provisioner which will show you that everything has been configured properly. Example- see below, yahoo will be launched after everything build successfully.

resource “null_resource” “nulllocal1” {
depends_on = [
null_resource.format_and_mount
]
provisioner “local-exec” {
command = “start chrome www.yahoo.com"
}
}

Now you can run your code. below are some screenshots when you run your code. use command →terraform apply < — to run the code. before this also run a command →terraform init < — to install the required plugins to run the code.

Below are some screenshots when you run the code

Now you can see that your instance is running, s3 is launched, and site also running

NOTE-

  1. For those resources for which i haven’t mentioned reference links, you can easily go to terraform official website, open the documentary and see examples there.
  2. Though you may be thinking that first ec2 instance should be launched or that code is written in unordered manner, but if you can see i have mentioned a property → default← in each resource. This will order your code properly. it means do when other are done/launched/configured.

This all I have done after the training of hybrid multi cloud by vimal daga sir. thanks to him who made all these things very easy and interesting to learn.

--

--