Going with the flow

According to the principle of the ‘survival of the fittest’, selfish individuals should be better off compared to peers that cooperate with each other. Indeed, even though a population of organisms…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Module in Terraform

In production, we need to manage multiple environments, different products with similar infrastructure. Writing code to manage each of these similar configurations increases a lot of redundancy in the code. No reusability adds an overhead of managing code and doing changes across the infrastructure. Collaboration on the different part of the code is hard when everyone is working on the same code. At times members of different teams have similar needs, which makes them write the same code. So, we need a mechanism to share code among teams, reuse existing code and make it robust. We need the capability to test different versions while keeping production infrastructure stable.

Terraform provides modules which allow us to abstract away re-usable parts, which we can configure once, and use everywhere. Modules allow us to group resources together, define input variables which are used to change needed resource configuration parameters, define output variables that other resources or modules can use. Modules are basically like files with exposed input and output variables which facilitate reuse. We don’t need to do anything special to create a module, it is just like our terraform configuration which runs independently. Modules can’t inherit or reference parent resources, we need to pass them to the module explicitly as input variables. Modules are self-contained packages which can be shared across teams for different projects.

Using modules in terraform is similar to using resources except we use module clause for modules instead of resource clause.

We are going to use public subnet id from the output of our networking module inside main configuration file using below code.

Our final EC2 resource is going to look something like mentioned below.

PS: If you liked the article, please support it with claps. Cheers

Add a comment

Related posts:

HiBTC

A cryptocurrency is a digital asset designed to work as a medium of exchange that uses strong cryptography to secure financial transactions, control the creation of additional units, and verify the…

Colorful Paper Chains Make Sustainable Holiday Decorations

Christmas always brings back childhood memories for me. One memory that I love goes back to grade school. We learned to make paper chains using colored construction paper. We used those chains to…

How to Succeed in the Digital Nomad Lifestyle

The journey of becoming a digital nomad can range from deceptively simple to overwhelmingly impossible. Cliché stories abound of people who claim to have walked straight out of their cubicle and into…