Key Concepts
Loading...
The official Terraform documentation comprehensively covers all aspects of configuration. It is essential to thoroughly understand these concepts for the discussions in this section.
Resource
A resource, such as aws_vpc or aws_db_instance, belongs to a provider, accepts arguments, outputs attributes, and has a lifecycle. Resources can be created, retrieved, updated, and deleted. Key points to consider:
- Provider Integration: Resources are tied to specific providers, defining the API interactions required to manage infrastructure components.
- Arguments and Attributes: Each resource type has a set of required and optional arguments that customize its behavior. Attributes are properties exposed by the resource once created.
- Lifecycle Management: Resources support lifecycle hooks like
create,read,update, anddelete, enabling fine-grained control over their management.
Resource Module
A resource module is a collection of connected resources that collectively perform a common action. For example, the AWS VPC Terraform module creates a VPC, subnets, NAT gateway, etc. It can depend on provider configuration defined within it or in higher-level structures, like an infrastructure module. Key points include:
- Modularity: Modules encapsulate functionality, making it reusable and maintainable.
- Input Variables: Modules accept input variables to customize behavior, promoting reuse across different environments.
- Outputs: Modules expose outputs that other modules or resources can use, facilitating integration.
Infrastructure Module
An infrastructure module is a collection of resource modules that, while logically not always connected, serve the same purpose in the current situation/project/setup. It defines the configuration for providers, passed downstream to resource modules and resources. Typically, it operates within one entity per logical separator (e.g., AWS Region, Google Project). Key points include:
- Provider Configuration: Centralizes provider settings, ensuring consistency across all included resource modules.
- Orchestration: Manages the interaction and dependencies between resource modules to build a coherent infrastructure setup.
- Scalability: Infrastructure modules can be scaled up or down by adding or removing resource modules as needed.
For instance, the terraform-aws-atlantis module utilizes resource modules like terraform-aws-vpc and terraform-aws-security-group to manage the infrastructure for running Atlantis on AWS Fargate.
Composition
A composition is a collection of infrastructure modules spanning across logically separated areas (e.g., AWS Regions, several AWS accounts). It describes the complete infrastructure required for the entire organization or project. Compositions consist of infrastructure modules, which, in turn, consist of resource modules implementing individual resources. Key points include:
- Cross-Region/Account Management: Compositions handle infrastructure spread across multiple regions or accounts, ensuring global consistency.
- Layered Structure: Builds on infrastructure modules, maintaining a clear separation of concerns.
- Centralized Management: Simplifies managing complex infrastructures by aggregating all configurations into a single composition.

Data Source
A data source performs a read-only operation and depends on provider configuration. It is used in both resource modules and infrastructure modules. For example, the terraform_remote_state data source acts as glue between higher-level modules and compositions. Key points include:
- Read-Only: Data sources retrieve information without making changes, useful for referencing existing infrastructure.
- Provider Dependency: Requires provider configuration to access and gather data.
- Integration: Facilitates sharing data between different parts of the configuration, enhancing modularity.
The external data source enables an external program to act as a data source, exposing arbitrary data for use elsewhere in the Terraform configuration. The http data source makes an HTTP GET request to a given URL, exporting information about the response.
Remote State
Infrastructure modules and compositions should store their Terraform state remotely, enabling controlled access and management (e.g., specifying ACL, versioning, logging). Key points include:
- State Management: Centralizes state files to prevent conflicts and ensure consistency.
- Access Control: Defines who can read or modify the state, enhancing security.
- Backup and Recovery: Remote state storage typically includes versioning and backup capabilities, aiding in disaster recovery.
Provider, Provisioner, etc.
Providers, provisioners, and similar terms are well-documented in the official Terraform documentation. These concepts, while crucial, have little impact on writing effective Terraform modules. Key points include:
- Providers: Define the API interactions for managing infrastructure. Each provider corresponds to a specific service or platform.
- Provisioners: Execute scripts or commands on resources after they are created or before they are destroyed, adding flexibility in configuration management.
- Simplification: Using well-defined providers and provisioners simplifies the module development process by abstracting complex interactions.
Why So Difficult?
While individual resources are like atoms in infrastructure, resource modules are molecules composed of these atoms. A module is the smallest versioned and shareable unit, implementing basic logic for a specific function. For instance, the terraform-aws-security-group module creates aws_security_group and aws_security_group_rule resources based on input. This resource module can be used with other modules to create the infrastructure module.
Data sharing between modules occurs through outputs and data sources, while compositions often use remote state data sources. Various methods exist for sharing data between configurations.