
[ad_1]
let’s build!

For this week’s project, we’re going to take an existing Terraform document, and turn it into a reusable module!
.tf The file in question, is a simple EC2 generator. Have AWS as a provider, ec2_instance as a resource and voila! But we want to convert it to a module, so that we can use it in any future projects by simply referencing the module.
What do we need:
- a Git/GitHub account
- aws admin user account
- Terminal/IDE
- Workstation with Terraform installed.
To begin with, we were tasked with forking our ec2.tf File in our own GitHub account…



Then, clone it into our environment (may be local to you, I’m using AWS Cloud9 environment.)

Setup complete!
This is where it gets nifty!
I went ahead and created a new folder in this repo that I just cloned: ec2_create. This will be the name of our module. In this folder we will create a new main.tf and variables.tf. To build the module, I copied the resource block from the original ec2.tf, and pasted it into our module’s main.tf.
… and our variables:
Finally, we convert the “resource” block in the original ec2.tf file into a module:
Very important here is the “source” element of the module. This tells Terraform where to find your module. It can be in your local directory (as shown in my example, with ./ ), or can be pulled from the public or private registry.
This was a very simple example, but hopefully it shows how useful modularization can be! next…
As with all things, we should always check on our work, and make sure everything goes according to plan! a terraform plan
he is! Cheese aside, we’ll run our terraform init
Firstly, so that TF knows about our module, and will build infrastructure accordingly.

Right along the way, if you’re feeling pricy, you can skip the “planning” step, and go straight to terraform apply
,

Looks like it worked! But just for laughs, let’s take a look in our AWS console:

And there you have it! A simple module for creating EC2 instances that can now be easily reused in any future project!
This project was a light one, but I found it very helpful to show me how to not only build my own modules, but also how to convert some existing TF configurations into modules.
Thanks for sticking around! See you next time!
[ad_2]
Source link
#Modularization #Terraform #Lets #create #Steven #Laszlofi #August