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 -
- AWS account
- Terraform installed on device
- Github account
- 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 -
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.
Creating security groups - We have created security groups that allows port 80.
Lauching the EC2 instance with the key and security group which we have created and installing apache web server, git and php for deployment -
Launching the Volume (EBS) and mounting that volume into /var/www/html -
Creating S3 bucket, and copy/deploying the images into the S3 bucket and change the permission to public readable.
Creating the Cloudfront using S3 bucket(which contains images) and use the Cloudfront URL to update in code in /var/www/html -
Creating a null resource for local execution -
Our coding part is complete. Now to execute this file -
- Open command prompt and come inside the directory of the created folder.
- 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 -
- Command prompt-
2. Created instance -
3. Created key pair -
4. Created EBS Volume with attachment -
5. Created security group -
6. Created S3 storage -
7. Created CloudFront -
and now after all the Execution, our output on the google chrome appears -
For complete code and .html file, you can go to my Github Repository -
https://github.com/aditya-xclusive/multicloud.git
Thanks for reading !!
SDE Intern'25 @IBM ISL
4 年Great ??
"The Cloud Never Rests, and Neither Do I" -- Keeping Systems Running While You Sleep.
4 年Great work. Keep it up.