Setting up Your First Project
This lesson will teach you how to set up your first project in Terraform and will go over each part of the project in detail.
We'll cover the following
In this chapter, we are going to create your first Terraform project. We will not cover everything in great detail as we will circle back and fill in the gaps in later chapters. We want the focus of this chapter to be about getting a feel for running Terraform and actually creating some infrastructure with it.
Setting up your first project#
-
We’ll create a file named
main.tf
-
Inside the file
main.tf
, we’ll paste the following text (replace<yourname>
with your name or something unique):
That’s all we need for our first Terraform project. The Terraform code we have just written will
create an S3 bucket in AWS with the name <yourname>-first-bucket
in the region eu-west-1
. You may have been able to guess this was going to happen, even without knowing Terraform. That is one of the strong parts of Terraform; the code is very readable and it is normally quite clear what is going to happen.
What does your first project do?#
Let us take a second to explain each part of the code in a bit more detail.
First block#
In the first 3 lines, we are defining the provider that we want to use. Terraform itself is just an engine that knows how to run a provider that conforms to an interface. The Terraform engine is smart and knows how to create dependency trees and plans, and it uses the provider to interface with the outside world. Since we are going to be using Terraform with AWS in this course, we need to configure the AWS provider.
To configure the provider, we use the keyword provider
then follow it with the name of the provider
in quotes (in this case "aws"
). We start the provider block by opening a curly brace {
. We can now
specify any parameters we want to configure the provider.
To pass a parameter, you simply put the name of the parameter followed by an equals sign and then the value you want to pass the parameter in quotation markers. In our example, we are setting the region this provider will use to be eu-west-1
. This is the region where the AWS Terraform provider will create all of the infrastructures we define. We then
end the provider block with a closing curly brace }
.
Second block#
The next block we have defined is a resource. A resource in Terraform represents a thing in the
real world (in this case, an S3 bucket). To define a resource, you start a resource block by using the
keyword resource
. You then follow it with the resource you want to create in quotes. We want to
create an S3 bucket, so we are using the S3 resource "aws_s3_bucket"
.
If you are following along in
IntelliJ and typing in the code, you might have noticed that IntelliJ gave you a full list of possible
resources once you started typing. You can see the full list on the AWS provider page if you are
interested. After we have specified the
type of resource we want to create, we put another space and then the identifier you want
to give that resource in quotes, in our example "first_bucket"
.
We then open the block in the
same way that we did for the provider block with an opening curly brace {
. Next, we can give any
parameters the resource takes values. In this case, we are only setting the name of the bucket. We then end the resource block with a closing }
.