Terraform Modules

Terraform modules provide a way of reusing infrastructure code. We can either use an external module, or write our own module. External modules can help us setting up infrastructure without much effort.

Writing our own modules gives us the full flexibility of what to control.

Let's take a look at an example,

It consist of four modules that creates an ECS cluster.

  • ECS cluster has an ECS resource to run the containers.

  • CloudWatch is used to keep the logs generated by the Docker containers.

  • IAM is used with the EC2 instances of the ECS.

  • ECS is created within a security group, therefore we have to create a security group resource as well.

  • Finally, it is required to launch EC2 instances to be consumed by the EC2 instances. This will allow to launch EC2 instances automatically.

In simple, a variable declares the input of the modules, and output declares the output, which in turn can be used in the root project or other modules. Resources can be stored within a module to allow easy provisioning of a logic component that consists of multiple resources.

Last updated