From the course: Learning Terraform
Resources - Terraform Tutorial
From the course: Learning Terraform
Resources
- [Instructor] In this lesson, we're going to look a bit deeper at Terraform code. Resources are the building blocks of Terraform code, they define the what of our infrastructure and Terraform figures out the how. Although all resources share the same syntax, every provider has different resource types and slightly different options. We're going to focus just on AWS so that we can go a bit deeper. It should be similar on other platforms like Azure but you'll need to review the documentation if that's the platform you plan to use. Let's look at a simple example, at the top here we've got our provider definition. A provider isn't a resource, it's what gives you access to resources. So the AWS provider, provides a set of possible resources to define. You need to have a provider defined in your code so that Terraform knows where the resources should go. The resource we've defined is an S3 bucket which I chose because it's pretty simple. The resource syntax is pretty self-explanatory but let's walk through it. First, we have the word resource, that's the key word to tell Terraform that we're defining a resource and not a provider for example. Next is the resource type, in this case, it's AWS S3 bucket. Those keywords are defined by the provider and correlate to actual infrastructure elements that can be created on AWS. Finally, we have the name Terraform we'll use for this resource. After the resource name, we can give Terraform some details about what we want to create by passing in parameters between two curly braces. In this example, we're giving a name for our S3 bucket, notice that this doesn't match the resource name. The bucket name is what AWS will call our bucket, but in our Terraform code, we can use another name. And S3 bucket is actually a great example of why this is handy because every S3 bucket needs to have a unique name. This means that we can refer to our bucket inside Terraform code using a friendly and easy to remember name like tf-course. But inside AWS, the bucket might need to include something like a date string to make sure it's unique. And finally, in this example, we have a second parameter that defines this is a private bucket. That's it for our simple example. But there are a lot more options for this resource type like setting up static website hosting or simple things like adding tags to the resource.