Terraform Modules For Beginners

A simple example of how to make a resource into a re-useable module

Must-Haves:

  • AWS account with the proper permissions configured

For this project, I'll be using Cloud9.

Step 1: Fork and clone (Git)

First, we'll fork this repo into our GitHub https://github.com/LevelUpInTech/terraformec2

Now clone it into your environment. You should see your repos' folder in your environment now. Make sure you create a new branch to work in.

Step 2: Module folder and file

Within the "terraformec2" folder, right-click and create a new folder for your module. Mine is called "ec2mod". Then we'll create a main.tf file within our new folder.< /p>

Once we have this we can cut and paste the resource block from the original configuration to our modules' main.tf.

In your parent module, reference your newly created module. I also had to change the region of my AWS provider. If you change your region, make sure you go to the EC2 dashboard in AWS and check the instance AMI for your region. Save the changes to your files.

Step 3: Test your work

First, we'll run a "terraform init" to download our plugins and initialize the environment.

I'm also running "terraform fmt" a nd "terraform validate" to ensure my configuration is valid and syntactically correct. Then feel free to run a "terraform plan" to check out what you'll be provisioning. Once you're ready, run a "terraform apply" to launch your infrastructure! When it's finished, you should see your newly created ec2.

Step 4: Push your changes back to GitHub

If you're using cloud9, go to your source control tab on the left-hand side and click the plus sign at the top next to "Changes". This will stage all of your changes.

Now let's add a message for a description and hit CTL+Enter to commit your changes. If you have yet to configure your git username and email you'll need to do so.

Now once you hit CTL+Enter your changes will be commited. On the left -hand side of cloud9 at the bottom you'll see two small arrows.

Click the small arrow with the 1 next to it to push your changes.

I had to make a ".gitignore" file so my push didn't exceed the memory limit.

I also had to make a new GitHub access token to be able to actually finish pushing these changes and I ran the command:

and everything was good to go! Now you should see your changes in your GitHub!

That wraps up this short article on module creation, I hope this helps you along your Terraform journey!

Thanks for reading along, best wishes.


Terraform Modules For Beginners was originally published in Towards AWS on Medium, where people are continuing the conversation by highlighting and responding to this story.

Namaste Devops is a one stop solution view, read and learn Devops Articles selected from worlds Top Devops content publishers inclusing AWS, Azure and others. All the credit/appreciations/issues apart from the Clean UI and faster loading time goes to original author.

Comments

Did you find the article or blog useful? Please share this among your dev friends or network.

An android app or website on your mind?

We build blazing fast Rest APIs and web-apps and love to discuss and develop on great product ideas over a Google meet call. Let's connect for a free consultation or project development.

Contact Us

Trending DevOps Articles

Working with System.Random and threads safely in .NET Core and .NET Framework

Popular DevOps Categories

Docker aws cdk application load balancer AWS CDK Application security AWS CDK application Application Load Balancers with DevOps Guru Auto scale group Automation Autoscale EC2 Autoscale VPC Autoscaling AWS Azure DevOps Big Data BigQuery CAMS DevOps Containers Data Observability Frequently Asked Devops Questions in Interviews GCP Large Table Export GCP Serverless Dataproc DB Export GTmetrix Page Speed 100% Google Page Speed 100% Healthy CI/CD Pipelines How to use AWS Developer Tools IDL web services Infrastructure as code Istio App Deploy Istio Gateways Istio Installation Istio Official Docs Istio Service Istio Traffic Management Java Database Export with GCP Jenkin K8 Kubernetes Large DB Export GCP Linux MSSQL March announcement MySQL Networking Popular DevOps Tools PostgreSQL Puppet Python Database Export with GCP Python GCP Large Table Export Python GCP Serverless Dataproc DB Export Python Postgres DB Export to BigQuery Sprint Top 100 Devops Questions TypeScript Client Generator anti-patterns of DevOps application performance monitoring (APM) aws amplify deploy blazor webassembly aws cdk application load balancer security group aws cdk construct example aws cdk l2 constructs aws cdk web application firewall aws codeguru reviewer cli command aws devops guru performance management aws service catalog best practices aws service catalog ci/cd aws service catalog examples azure Devops use cases azure devops whitepaper codeguru aws cli deploy asp.net core blazor webassembly devops guru for rds devops guru rds performance devops project explanation devops project ideas devops real time examples devops real time scenarios devops whitepaper aws docker-compose.yml health aware ci/cd pipeline example host and deploy asp.net core blazor webassembly on AWS scalable and secure CI/CD pipelines security vulnerabilities ci cd pipeline security vulnerabilities ci cd pipeline aws smithy code generation smithy server generator
Show more