Creating an AWS infrastructure using Terraform

Creating an AWS infrastructure using Terraform

So before we move to the main objective, lets have some of the basic knowledge about the resources that we will use here -

Amazon Web Services : AWS (Amazon Web Services) is a comprehensive, evolving cloud computing platform provided by Amazon that includes a mixture of infrastructure as a service (IaaS), platform as a service (PaaS) and packaged software as a service (SaaS) offerings.

Terraform : Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions. Configuration files describe to Terraform the components needed to run a single application or your entire datacenter.

Github : GitHub is a Git repository hosting service, but it adds many of its own features. While Git is a command line tool, GitHub provides a Web-based graphical interface. It also provides access control and several collaboration features, such as a wikis and basic task management tools for every project.

Approach - Have to create/launch Application using Terraform

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.

4. Launch one Volume (EBS) and mount that volume into /var/www/html

5. Developer have uploaded the code into git hub repository also the repository has some images.

6. Copy the git hub repository code into /var/www/html

7. Create S3 bucket, and copy/deploy the images from git hub repository 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

Requirements -

  1. AWS account
  2. Terraform installed on device
  3. Github account
  4. AWS Command line interface (CLI) installed on device


Firstly create a folder . Inside that folder, create a notepad file with .tf extension. (terraform file extension).Now write the code inside this file.

We have to login to our AWS account. Terraform files are sharable codes so that everyone can use that code to build their own infrastructure. So instead of writing the Access key and private key , we have created a profile -

No alt text provided for this image

Creating the key - We have created the keys by RSA Algorithm. Further we used local_file resource of terraform which creates a local copy of the private key and then attached that file with our instance.

No alt text provided for this image

Creating security groups - We have created security groups that allows port 80.

No alt text provided for this image


Lauching the EC2 instance with the key and security group which we have created and installing apache web server, git and php for deployment -

No alt text provided for this image

Launching the Volume (EBS) and mounting that volume into /var/www/html -

No alt text provided for this image
No alt text provided for this image

Creating S3 bucket, and copy/deploying the images into the S3 bucket and change the permission to public readable.

No alt text provided for this image







Creating the Cloudfront using S3 bucket(which contains images) and use the Cloudfront URL to update in code in /var/www/html -

No alt text provided for this image
No alt text provided for this image





Creating a null resource for local execution -

No alt text provided for this image

Our coding part is complete. Now to execute this file -

  1. Open command prompt and come inside the directory of the created folder.
  2. Run the following commands -

(a) terraform init - This will initialize all the required resources for the execution of file.

(b) terraform validate - This will check the validation and syntax errors in the file.

(c) terraform apply - This will apply terraform execution on the .tf file.

here are the outputs -

  1. Command prompt-
No alt text provided for this image

2. Created instance -

No alt text provided for this image

3. Created key pair -

No alt text provided for this image

4. Created EBS Volume with attachment -

No alt text provided for this image

5. Created security group -

No alt text provided for this image

6. Created S3 storage -

No alt text provided for this image

7. Created CloudFront -

No alt text provided for this image

and now after all the Execution, our output on the google chrome appears -

No alt text provided for this image

For complete code and .html file, you can go to my Github Repository -

https://github.com/aditya-xclusive/multicloud.git

Thanks for reading !!







Mohit Agarwal

SDE Intern'25 @IBM ISL

4 年

Great ??

回复
Saumik Satapathy

"The Cloud Never Rests, and Neither Do I" -- Keeping Systems Running While You Sleep.

4 年

Great work. Keep it up.

回复

要查看或添加评论,请登录

Aditya Kumar Pal的更多文章

社区洞察