Introduction ¶
Terraform is a fantastic tool for automating resource creation in clouds like AWS, Azure, GCP, Hetzner, and many others. It’s really cool. With tfenv, you can easily and conveniently update Terraform locally and choose the desired version. You can create a file in the project’s root to specify the required version, allowing tfenv to automatically use the correct Terraform version for your project.
There are several practical recommendations I’ve developed from reading the official documentation, which is excellent, as well as the modules and code written by AWS.
Recommendations ¶
Study the official recommendations. Below are my own notes.
1. Do not duplicate resource type in the name ¶
Incorrect
|
|
Correct
|
|
Explanation
- In the code, you’ll still reference the resource as
${aws_vpc.production.id}
—it’s already clear that this is a VPC, so there’s no need to waste characters on what’s already written. - The same goes for the resource name; you’ll always see the VPC in the list on the VPC page in the AWS Console, and there won’t be anything else. Again, why duplicate?
2. Use this
as a name, but only in modules ¶
Suppose you have a module that creates a virtual machine and minimal attachments. Within the module, the virtual machine is unique.
|
|
Why come up with names for resources if it’s the only one within the module? There’s no need. It’s also important to understand that this is the object name within the Terraform code. It’s convenient when any resource type is simply this
, but you should only do this within a single module that creates a logical unit. A virtual machine without its Security Group, which opens the ports, doesn’t make sense; thus, logically, it’s one piece.
3. Create standard files for modules ¶
File | Content |
---|---|
vars.tf | All variables |
outputs.tf | Output parameters |
versions.tf | Section with provider requirements |
main.tf | File with the root content |
4. Do not declare providers in modules ¶
Specify version constraints, but leave initialization to the discretion of the user of your module.
5. Tags variable ¶
Add a variable that will apply tags to all resources in the module.
|
|
In the local.tags
variable, we first collect the final dictionary and then use it where needed within the module.
6. Resource naming with name_prefix
¶
Always, without exception, use a name prefix for creating resources. By changing just this variable, the code should create exactly the same infrastructure in the same account without any conflicts—this is a good module that can be reused.
|
|
And there’s no point in adding anything to the name if there’s only one resource. Suppose this code is part of the same module for creating a virtual machine; it will be used like this.
|
|
This way, it will be clear from the prefix to which virtual machine the EC2 Key Pair belongs in the list. If there were two, then the name in the code would be "${var.name_prefix}-some-suffix"
.
7. No hyphens in resource names ¶
Forget about using hyphens in resource names. It breaks the code analyzer in the IDE, and it causes issues in various situations.
Incorrect
|
|
Correct
|
|
I would like to emphasize that for the name of the resource in the cloud, I recommend using hyphens as much as possible—it looks nicer—but in Terraform, never.