- Terraform
- Local Command Prompt
Let's start by creating the Directory Structure for the project.
Check that Terraform is installed and functioning properly using the terraform version command.
Create a new directory to house your Terraform code called terraform_project.
In the main project directory, create a custom directory called modules and a directory inside it called vpc. We want to keep all of our modules in one place, hence why we are making subdirectories.
Now we will write out Terraform VPC Module code.
In the VPC directory, create a new file called main.tf and add this code.
To break it down, we are letting it know to use the AWS provider and the region specified in the 'Region' variable defined in the 'variables.tf' file we will make shortly. We are spinning up a VPC within AWS and giving it a CIDR block of 10.0.0.0/16. After creating the VPC, we are creating a subnet within the VPC with a CIDR block of 10.0.1.0./24. Finally, we are using the SSM Parameter in this code to get the AMI ID of the latest Amazon Linux 2 machine image. This will query the SSM public endpoint, which houses the ID for whatever is the latest Amazon machine Linux 2 image.
Now we are going to create a new file called variables.tf and add this code.
We are setting the default region to 'us-east-1' in this code.
We are going to create another new file called outputs.tf and add the code.
The Outputs code is very important. Anything that specifies as an Output can be returned back to our Terraform main code. Here we are returning two values, first being the subnet ID, and second being the AMI ID.
So now that we are done writing our Module code, let's go over again what we just wrote. The Terraform Module is creating a VPC, a subnet, fetching the AMI ID of our image, and then returning the subnet ID and the AMI ID as outputs from the module.
Now we can write our Main Terraform Project code
In the terraform_project directory, create a new file called main.tf and add this code, which invokes the VPC module created earlier.
Then create a new file called outputs.tf and add the code below.
This code will basically return the private IP address of the EC2 instance being spun up via our main Terraform code.
Finally, we are ready to deploy and test out our new module.
Let's start by formatting the code in all of our files using the terraform fmt -recursive command, and then initialize the Terraform configuration to fetch any required providers and get the code being referenced in the module block with the terraform init command.
Validate the code using the terraform validate command. This will check for syntax against the Terraform provider and make sure we are not passing any incorrect syntax or mistyped values and wrong parameters within the resources.
Let's review the actions that will be performed when we deploy the code by using the terraform plan command and then deploy the code with the terraform apply --auto-approve command.
Everything works! We can also view the resources that were created using the terraform state command. Also, dont forget to tear down the infrastructure using the terraform destroy command.