Provisioning an AWS VPC with Terraform.

Ronnie Ford
5 min readDec 7, 2020

Purpose of this lab: To create a VPC with a subnet in AWS using Terraform.

What is Terraform? Terraform is an infrastructure as code (IAS) tool that will efficiently build, change, and version your cloud infrastructure.

Benefits of Terraform:

1. Supports multiple cloud platforms (AWS, Azure, GCP, etc.)

2. Free to use

3. Easy integration with configuration management tools

4. Simple configuration language and fast learning curve

5. Can use on multiple platforms (Windows, Linux, MacOS, etc.)

What you will need to complete this lab:

1. An AWS account.

2. Terraform installed on your machine. Here’s the link if you don’t have it installed Terraform.

3. A text editor installed on your machine (Atom, Sublime, Vim, etc.).

Part1: Creating the IAM User and give them administrator access.

1. Login into your AWS account.

2. Search for IAM.

3. Click on it to bring you to the IAM homepage.

4. In the navigation window select “users”.

5. Since we’re creating a new user select “add user”.

6. Under the set user details section you want to type in a username. I used Vpv_Terraform as my username, but you can use whatever you would like.

7. Under the select AWS access type section you’ll want to select “programmatic access”. The reason being is because we’re not provisioning our VPC in the AWS console.

8. Once you have a username and selected programmatic access click on “next:premissions”.

9. On the set permissions page select “attach existing policies directly”.

10. Select the “administrator access” policy.

11. Select “next: tags”.

12. We will not add any tags select “next:review”.

13. You will be brought to the review page.

14. Make sure is everything is correct. If everything is correct select “create user”.

15. You will be brought to the below page. The user has now been created, but you will need to download the CSV file to your computer. The file will contain your access key and your secret key which you will need later on in the lab.

Part 2: Coding and deploying your infrastructure.

  1. Before you start, you’ll want to create a folder on your desktop to store your files. Just to reiterate I’m using Windows as my operating system. I named my folder “VPC Terraform.

2. Open up your text editor. I’ll be using Atom as my text editor.

3. If you’re using Atom select file -> add project folder.

4. Once you’ve named your project starting coding your infrastructure.

5. Your code should be similar to the below screenshot. (Please note: That you should NEVER hard code your access key and secret access key into your code. You’ll want to a create variable and store them within the variable, but for the sake of this lab I hard coded them in.)

6. Save the changes to your file.

7. Go to the command line.

8. You will want to change directories so that you’re in the folder that you created.

9. Once you’re in the correct directory you will want to type in “terraform init” this will initialize the working directory containing the Terraform config files.

10. If everything initializes correctly you will get the below screen.

11. Now that Terraform has been successfully initialized you will want to make sure that there’s nothing wrong with your infrastructure. You will want to enter the command “terraform plan”. It will show you what infrastructure will be launched.

12. If everything is correct, you can deploy your infrastructure. To do that you’ll want to use the command “terraform apply”.

13. If your infrastructure launched correctly you’ll see the below message.

14. Go to the AWS console to verify that your infrastructure deployed successfully.

15. You can see that our VPC and our subnet has been successfully deployed.

16. If you want to delete your infrastructure go back to the command line, and type in the command “terraform destroy”.

17. All of your resources will be destroyed.

18. You’ll be asked if you want to destroy all of your resources. Type in “yes”.

19. Your resources have been destroyed.

--

--