Automating Infrastructure deployment

Shivangi Sharma
5 min readJun 16, 2020

--

Managing IT infrastructure as a manual process can become tedious and costly for most of the large-scale companies as many professionals would be hired to physically set up the infrastructure and configure them. Only then any applications can be deployed. To make this process easier we can use infrastructure as code.

Infrastructure as code is the process of managing and provisioning computer data centers through machine-readable definition files, rather than physical hardware configuration or interactive configuration tools.

For the process of automating infrastructure deployment on cloud various tools can be used such as terraform, it enables you to safely create, change, and improve infrastructure. It is an open source tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.

Using terraform an entire infrastructure has been created:

  • creating EC2 instance
  • creating EBS storage
  • attachment of EBS to EC2 instance
  • creating key-pair
  • creating security-group with the following inbound rules:

port 80 for HTTP

port 22 for SSH

port 8080 for Jenkins

  • creating S3 buckets and inserting objects
  • creating CloudFront distribution

With the help of terraform an EC2 instance is launched with the key-pair and security group then the EBS volume is attached to the instance and the apache web-server is launched.

Using github, the code created by the developer is cloned and saved in th/var/www/html/

Jenkins server is also created in the EC2 instance using docker and creates jobs to keep checking and updating dynamic and static data of S3 and Github.

An S3 bucket is created by terraform and object is inserted with the provision of public access.

CloudFront distribution is created which maintains and manages the static data in S3 bucket, while the dynamic data is handled by github and Jenkins

Firstly the folder/repository needs to be initialized so that the necessary plugins can be installed then the code can be applied to create the infrastructure

terraform init

terraform apply -allow-approve

Creating the key-pairs

A new key-pair is created

Creating Security Group

Creating EC2 instance

Creating EBS volume

Attaching the volume

Creating S3 bucket and object

Creating CloudFront distribution

Starting website and Jenkins server

Setting up the Jenkins server

To set up the jenkins server, you just require one-time administrator password after that we can set up our own password and username.

This password can be accessed at /var/jenkins_home/secrets/initialAdminPassword or we can also copy password from the logs with the command

sudo docker logs <container name>

We can also install the commonly used plugins now (optional)

Creating first job

  • I am creating git_job as the first job that would be triggered as soon the developer changes code in github by setting up webhooks, hence using “GitHub hook trigger” as build trigger (any other trigger can also be used)
  • under “Build” section write the command as follows which copies the code to webservers workspace ie. /var/www/html

sudo cp -rvf * /var/www/html

  • under Post-build Actions, mention the job name that would be triggered after successful build ie.s3_job

Installation of plugins-

we would be using S3 and Git jobs hence we need to install those plugins,I have already installed git plugin while setting up jenkins

  • from jenkins go to plugin manager and search for S3
  • Install the S3 publisher plugin

Configuring S3 plugin

  • go to the configuration from jenkins
  • under S3 profiles, set up your S3 profile by entering your access key and secret key provided by AWS

Creating S3_job

  • under Post-build actions, set up the source as required (I am using it for the static data such as images)
  • enter the destination bucket as the bucket which was created by terraform
  • enter the bucket region in which your bucket is located

Both the jobs have been successfully launched

Notice in S3, the image has been published through S3_job by jenkins

The following website is displayed using the S3 image as the background

--

--