AUTOMATING AWS USING TERRAFORM
AWS is a powerful cloud platform and automating it with the help of Terraform is yet more powerful.
I have created a complete infrastructure of automatically launching a website running on the AWS cloud using Terraform which is a tool used for building, changing, and versioning infrastructure safely and efficiently.
Inorder to use AWS Services in Terraform we first need to download the plugins associated with it which can be done by terraform init command. While the configuration file holds the resources as following.
Now we can start creating our infrastructure.
1. Create the key - Inorder to get inside our instance we need to authenticate, hence we create a key and save it in our system for future use in pem format.
The key has been successfully created and is downloaded in the folder where the code is running.
2. Create a security group which allows the port 80 for http services and port 22 for ssh services.
3. Launch EC2 instance - Now we will launch an EC2- instance with the created keypair and security-groups.
The instance it running successfully.
Next we use a null resource to connect to the launched instance and then use a remote provisioner to download the required softwares for launching our website i.e. HTTPD and GIT.
Terraform downloading the softwares.
Let's check if the softwares are successfully installed in the instance or not ,using systemctl status httpd and yum install git .
We also save the ip -address of the launched instance in a file and print it using the output block.
4. Launch one Volume (EBS) and mount that volume into /var/www/html folder inside the remote O.S. Then download the code from the Github repository and save it inside the /var/www/html folder .
The EBS Volume is successfully attached can be checked using fdisk -l command of Linux.
Also the contents of the Github repository have been successfully copied to the /var/www/html folder using the ls command inside the folder.
The same contents are present in the folder as are uploaded in the Github repo.
5. Create a S3 bucket, and copy/deploy the images into the S3 bucket and change the permission to public readable.
The image is uploaded in the S3 bucket and has public access.
9. Create a Cloudfront Distribution using S3 bucket (which contains images) and use the Cloudfront URL in the html code of the website.
Then we use the local provisioner to run our website on chrome by getting the ip-address of the launched instance.
After running all these commands successfully in Terraform , it displays the ip address.
And launches the website on chrome.
Get the entire code on Github.