Terraform is an open-source infrastructure as a code software tool created by HashiCorp. Users define and provide data centre infrastructure using a declarative configuration language known as HashiCorp Configuration Language (HCL), or optionally JSON. It enables building, changing, and versioning infrastructure safely and efficiently. It can manage existing and popular service providers as well as custom in-house solutions. It also provisions infrastructure across public and private cloud platforms.
Infrastructure as Code:
Infrastructure as code manages and provisions computer data centres through machine-readable definition files, rather than physical hardware configuration or interactive configuration tools. Terraform allows you to build, change, and version your infrastructure using code techniques. It will enable you to set configuration files to define your applications or infrastructure and store your infrastructure's state to modify or update.
The Terraform plan command creates an execution plan. By default, creating a project consists of reading the current state of any existing remote objects to ensure that the Terraform state is up-to-date. It compares the current configuration to the initial state and notes the differences. Terraform has a "planning" step where it generates an execution plan. The execution plan shows what Terraform will do when you apply for a call. This lets you avoid any surprises with Terraform.
Important Terraform Concepts:
Providers: Providers are responsible for creating & managing resources. Terraform uses plug-in based architecture. The ‘init’ command will download & install the provider binary.
E.g., AWS, Azure, Docker, Kubernetes, and more.
Resources: Small building blocks with one or more infrastructure objects such as virtual machine, subnet, and LB. The resource name & type server is an identifier for a given resource; hence it must be unique.
Input Variables: The input variables are used to define values that configure your infrastructure. These values can be used repeatedly without remembering their every occurrence in the event it needs to be updated.
Output Variables: The output variables are used to get information about the infrastructure after deployment. These can be useful for passing on information such as IP addresses for connecting to the server.
Modules: To organize configuration code & make the code reusable, the modules are used as a container for multiple resources that can be used together. Every Terraform configuration has at least one module, called the ‘root’ module.
Terraform Remote: To share responsibility for infrastructure, you can run Terraform in a remote environment with shared access to the state. This feature is called the ‘remote backend’. We can use Consul and S3 as a backend to store the state.
Important Commands of Terraform
Terraform version: This shows the current version of Terraform that is installed on the system. This can be helpful to figure out issues or ways of working with the current version.
Terraform init: Initializes a working directory containing Terraform configuration files. This working directory can also be checked from source control.
Terraform plan: Creates an execution plan. It reads the current state of any pre-existing remote objects to make sure that the Terraform state is up-to-date. The current configurations are compared to the prior state and differences are calculated.
Set of change actions to make the remote objects match the configuration are proposed. This won’t execute the changes
If this proposal matches expectations then this can be executed later.
Terraform fmt: rewrites Terraform configuration files to a canonical format and style. The canonical format may change in minor ways between Terraform versions, so after upgrading Terraform this command should be run on modules to adopt the new version.
Terraform validate: Checks whether a configuration is syntactically valid and internally consistent.
Terraform apply: Executes the actions proposed in a Terraform plan.
Terraform show: Provides human-readable output from a state or plan file. This can be used to inspect a plan to ensure that the planned operations are expected, or to inspect the current state.
Terraform providers: Shows information about the provider requirements of the configuration in the current working directory.
Terraform output: Extracts the value of an output variable from the state file. With no additional arguments, the output will display all the outputs for the root module. If an output NAME is specified, only the value of that output is printed.
Terraform destroy: Destroys all remote objects managed by a particular Terraform configuration.
terraform import: Terraform is able to import existing infrastructure. This command allows us to bring resources that were created by some other means under Terraform management.
You can start using Terraform in internal AWS accounts for the below services.
To Build EC2 instances
To Build RDS instances
For S3 Buckets
To build VPC’s
To build SNS and SQS services
Terraform's versatility and power make it an invaluable tool for various use cases across different industries. Here are some common scenarios where Terraform excels:
Infrastructure provisioning: Terraform simplifies the process of provisioning infrastructure resources in cloud environments. Whether you need to create virtual machines, databases, or storage buckets, Terraform can automate the setup, ensuring consistency and reducing human error.
Auto-scaling: Terraform can automatically scale your infrastructure resources up or down when your application needs to handle varying traffic levels. For instance, you can define Auto Scaling Groups in AWS or similar constructs in other cloud providers to ensure your application can handle increased loads without manual intervention.
Multi-cloud deployments: Organizations use multiple cloud providers to minimize vendor lock-in and increase redundancy in a multi-cloud strategy. Terraform's ability to work with different providers allows you to manage resources on AWS, Google Cloud, Azure, and more using a single configuration language.
Disaster recovery: Terraform helps with disaster recovery planning by allowing you to define backup infrastructure that can be quickly deployed in case of a failure. This ensures minimal downtime and data loss during unexpected outages.
Kubernetes orchestration: If you're running containerized workloads on Kubernetes, Terraform can assist in provisioning Kubernetes clusters, managing namespaces, and deploying applications. This makes it easier to manage your entire containerized ecosystem alongside your infrastructure.
DevOps pipeline integration: Incorporating Terraform into your CI/CD pipelines streamlines the process of deploying infrastructure changes. You can automate the testing and deployment of infrastructure updates, ensuring that your infrastructure is always in sync with your application code.
Database management: Terraform can manage database resources, such as Amazon RDS instances or Google Cloud SQL databases. This includes creating, updating, and scaling databases and managing access and security configurations.
Network infrastructure: Whether you're building a Virtual Private Cloud (VPC) in AWS or a Virtual Network in Azure, Terraform simplifies the setup of complex networking configurations. This includes defining subnets, security groups, and routing rules.
Compliance as code: Organizations with stringent compliance requirements can use Terraform to define infrastructure configurations that adhere to specific compliance standards. This ensures that security and regulatory rules are consistently enforced.
Hybrid cloud: For businesses with on-premises data centers and a presence in the cloud, Terraform can be used to manage hybrid cloud environments. It allows you to treat your on-premises infrastructure as code, just like your cloud resources.
Application staging environments: Terraform can be used to create isolated staging and testing environments that mirror your production infrastructure. This enables thorough testing of changes before they're applied to the live environment.
Infrastructure monitoring: Integrating Terraform with monitoring tools like Prometheus or Grafana helps in automatically provisioning monitoring infrastructure as your applications and services grow.
Benefits of Terraform:
Portability: You can use one tool and one language to describe the infrastructure for Google Cloud, AWS, OpenStack and any other cloud. The hassle of switching providers regularly is mitigated.
Ease of full-stack deployment: You can have Amazon instances running Kubernetes containers with your workloads and manage the whole system from one tool.
Easy collaboration: The Terraform registry (Terraform’s central registry version control) enables teams to collaborate on infrastructure.
Flexibility: Terraform not only handles IaaS (AWS, Azure, etc) but also PaaS (SQL, NodeJS). It can also store local variables such as cloud tokens and passwords in encrypted form on the terraform registry.
Neebal, a technology solutions provider, has delivered top of the line solutions across Agro, Pharma, and BFSI verticals. Neebal aims to provide top tier services for API, RPA, and advanced mobility with a prime focus on Hyperautomation. Founded in 2010, Neebal is a proud recipient of the Deloitte Technology Fast 500 Award (APAC) and the Deloitte Fast 50 Award (India) for four consecutive years (2017-20).