Creating an custom module for ec2 instance with a amazon linux 2 ami ID
--
A module is a container for multiple resources that are used together. Every Terraform configuration has at least one module, known as its root module, which consists of the resources defined in the . tf files in the main working directory.
Is this article I will be creating a module to start an ec2 instance. The very first thing we will need to do is clone a repo assigned by my coaches at Level Up In Tech AWS DevOps 24 week boot camp. After cloning we will need to make changes to the ec2.tf file located in the ROOT/ PARENT directory called, terraformec2 in order to create the Module.
What is needed to complete the objectives of this article:
- Terraform installed
- AWS account
- AWS command line installed
- Github account
- Clone the following repo using the git clone command: https://github.com/LevelUpInTech/terraformec2.git
First, I will need to fork and then clone the repo above. Below we have our ec2.tf file. This file is going to be broken down in order to create the ec2 instance module.
Above we have cut the providers from the ec2.tf file in the root directory and have pasted it into the a directory called; my-ec2 which is a child of the root directory. Next, we cut and past the resources section from ec2.tf and place it in the main.tf file in the child directory as seen below. Now the ec2.tf file should be emptied.
The ec2.tf file in the Parent Module terraformec2 is now empty. I will be turning the ec2.tf file into a module below.
I will now run a the following terraform commands to sure up the configuration.
- Terraform init.
- Terraform fmt
- Terraform Validate.
- Terraform Plan.
- Terraform apply.
After a success apply, the next step is to open the AWS Console navigate to EC2 and view instances. I should see a server called, “Edubs-Server”.
Next, I am running a terraform destroy to clean up the IaC. Below, I will now add my variables; which will create a linkage to my root module. Creating communication between the Parent and Child Modules.
We will now continue to define our variables, next is the main.tf file for the instance.
Now the I am adding the .gitignore file to make sure that no unnecessary files are sent upstream when pushing to github. This is huge because any file size 100MB or larger will not push to github.
It is now time to commit changes and push my code.
This was very interesting terraform is a great tool and this project gave a base to work from now on when setting up a module(s).