Creating Your First Infrastructure With Terraform
Learn to create an infrastructure using Terraform and how Terraform creates and destroys the infrastructure.
We'll cover the following
The first thing you have to do with a new Terraform project is to initialise Terraform for that project. To do this, provide aws_access_key_id
and aws_secret_access_key
, change <your-name>
for the bucket name, and click the RUN button:
đź“ťNote: Before running the project please do make sure that you have set up an AWS account. You can also refer to the lesson, Setting up an AWS Account.
Important Note:
- If you cannot edit the access keys and other fields in the terminal below, you don’t need to provide your credentials. We have got you covered.
- Please use only
us-east-2
region for working in AWS in this course. - Please ensure that the bucket name is globally unique.
provider "aws" { region = "us-east-2" } resource "aws_s3_bucket" "first_bucket" { bucket = "<your-name>-first-bucket" }
You will see some output on the screen as Terraform initialises, then you should see the message
Terraform has been successfully initialized!
. Once you have initialised Terraform, you are ready to create the infrastructure by running:
terraform apply
After you run the apply
, you will see quite a lot of output from Terraform. You will notice that the
apply
has paused and is awaiting a response from you.
Let’s take a second and look at what is happening here. By default, when you run terraform apply
, Terraform will look at the code you have written and compare it to the infrastructure you currently have (in this case in AWS). Once Terraform has done this, it calculates a plan.
Terraform plan
The plan is what Terraform will do to get the real infrastructure from where it is now to how you have specified you want it to be in code. From looking at the plan, we can see Terraform is saying it will create an S3 bucket. You have told Terraform you want an S3 bucket, so Terraform went to AWS to check and realised that there is no S3 bucket in AWS with that name. Thus, it knows that it needs to create the bucket.
đź“ťNote: Plans will be discussed in much more detail later in this course.
The great thing about this plan is that Terraform presents it to us and then pauses, giving us time to decide whether we want to go ahead. You can imagine how useful this is if you accidentally make a change that will destroy your database! To get Terraform to make these changes and create the S3 bucket, type yes and press enter.
Terraform apply
Once the apply
has finished, you should see the message Apply complete! Resources: 1 added, 0 changed, 0 destroyed..
This is Terraform telling you that it successfully created the S3 bucket for you. Now, let’s work through the following steps:
-
Log onto the
aws
console (website). If you go to the S3 section, you will see the bucket that Terraform created. -
Delete the bucket from the AWS console. Now go back to the terminal and run
terraform apply
again.You will notice that Terraform has worked out that the S3 bucket is not there anymore, so it needs to create it again. At no point did you tell Terraform the bucket was gone, Terraform worked it out on its own.
-
Confirm the apply (by typing
yes
), so the S3 bucket exists again. -
Now run
terraform apply
again when the bucket is there. You will see Terraform outputApply complete! Resources: 0 added, 0 changed, 0 destroyed..
Terraform realises that the state of the world is exactly how you want it to be, so Terraform is saying “nothing to do here!”
Terraform destroy
To finish up, let’s destroy the infrastructure we created, don’t worry, Terraform can take care of that for us:
-
Simply run the command
terraform destroy
. -
Terraform will present a plan to you of what it is going to destroy and then pause so you can confirm.
-
Type yes and press enter. When the destroy finishes, you will see a message
Destroy complete! Resources: 1 destroyed.
.This is telling you Terraform has successfully destroyed everything.
-
Log into the AWS console and go to S3, and you will see that the bucket is now gone.
Summary
That concludes our first experience with Terraform. I hope you can start seeing the power and simplicity that Terraform provides. Feel free to play around with this project and try
changing the properties like the name of the S3 bucket and see what happens. That is a great way to learn. Just remember to run terraform destroy
when you are finished to ensure that you are not left with any infrastructure running in AWS.
Do not worry if you have more questions about any of the steps we just went through. We are going to cover everything in much more detail soon!
Get hands-on with 1300+ tech skills courses.