3.2. Creating Infrastructure With Terraform
3.2. Creating Infrastructure With Terraform
Activity
1. Open Visual Studio Code and Create a folder and name it appropriately, such as 01-
benefits. Inside the folder, create a file with the .tf extension (e.g., vpc.tf).
2. Write a provider block to define the AWS provider and specify the region.
3. Define a resource block for the VPC with its CIDR block (e.g., 10.0.0.0/16).
4. Create resource blocks for a public and a private subnet, linking them to the VPC and
specifying non-overlapping CIDR blocks.
5. Define a resource block for the Internet Gateway and attach it to the VPC.
6. Create a resource block for the route table and link it to the VPC.
7. Add a route in the route table for internet access (0.0.0.0/0) via the Internet Gateway.
8. Define a route table association resource to link the public subnet to the route table.
9. Review the configuration to ensure all resources are connected correctly and
dependencies are clear.
10. Optionally, apply the configuration using Terraform commands to deploy the
infrastructure to AWS.
11. Run a terraform plan to preview the changes Terraform will apply.
12. If everything looks correct, execute terraform apply to deploy the infrastructure to
AWS.
13. Verify the created resources in the AWS Management Console.
14. The code is shown below:-
Now, we are going to continue this in the next lab.
Exploring Terraform Stages
1. Navigate to the project directory where Terraform configurations are stored.
2. Open VS Code where you have the code and open new terminal.
3. Run terraform init to initialize the directory and download required providers.
8. Verify the resources in the AWS Management Console for the selected region.
9. Run terraform destroy to remove all resources managed by Terraform, confirming
with "yes".
10. Refresh the AWS console to ensure resources were successfully deleted.