Mastering Terraform: From Essential Commands to Effortless EC2 Instance Provisioning

Praveen Dandu
4 min readJul 13, 2023

--

Introduction:

In the ever-evolving world of infrastructure-as-code, Terraform has emerged as a powerful tool for provisioning and managing cloud resources. In our previous blog, “Top 20 Terraform Commands with Examples: A Beginner’s Guide,” we covered the essential Terraform commands for beginners. If you haven’t read it yet, we highly recommend starting there to establish a solid foundation.

Now, we’re excited to take your Terraform skills to the next level by introducing you to the seamless provisioning of EC2 instances using Terraform’s AWS modules. In this continuation of our Terraform series, we’ll explore how to leverage AWS-provided modules to simplify the process of launching EC2 instances. With these modules, you’ll be able to effortlessly provision and manage your EC2 instances, making infrastructure deployment a breeze. Let’s dive in!

Terraform modules are an essential aspect of infrastructure-as-code (IaC) development. They allow you to organize and reuse Terraform code, making your infrastructure deployments more efficient and maintainable. In this practical guide, we’ll dive into Terraform modules with plenty of code examples to help beginners grasp this concept quickly.

1. Creating a Simple Module:
Let’s start by creating a simple Terraform module that provisions an AWS EC2 instance. Here’s the directory structure for our module:

module/
├── main.tf
├── variables.tf
└── outputs.tf

In `main.tf`, define the resources you want to create:

resource "aws_instance" "example" {
ami = var.ami_id
instance_type = var.instance_type
}

In `variables.tf`, declare the input variables for customization:

variable "ami_id" {
description = "AMI ID for the EC2 instance"
}

variable "instance_type" {
description = "Instance type for the EC2 instance"
default = "t2.micro"
}

In `outputs.tf`, specify the output values you want to expose:

output "instance_id" {
value = aws_instance.example.id
}

2. Using the Module:
Now, let’s see how to use the module in your Terraform configuration. Create a `main.tf` file in your root configuration:

provider "aws" {
region = "us-west-2"
}

module "ec2_instance" {
source = "./module"
ami_id = "ami-12345678"
instance_type = "t2.micro"
}

In this example, we’re using a local module (`source = “./module”`) located in the same directory. However, you can also use a module from the Terraform Registry by specifying the source as `source = “organization/module/name”`. Replace `”ami-12345678"` with the desired AMI ID.

3. Output Values from the Module:
To access the output values from the module, you can reference them using the module’s name and the output name. For example, to print the instance ID from the module, add the following code to your root configuration:

output "module_instance_id" {
value = module.ec2_instance.instance_id
}

4. Module Composition:
Terraform modules can be composed together to build more complex infrastructure. Let’s assume we have another module called `security_group` that creates an AWS security group. We can compose the `ec2_instance` module with the `security_group` module as follows:

module "security_group" {
source = "./security_group"
}

module "ec2_instance" {
source = "./module"
ami_id = "ami-12345678"
instance_type = "t2.micro"
security_group_id = module.security_group.security_group_id
}

By passing the `security_group_id` output value from the `security_group` module to the `ec2_instance` module, we establish a relationship between the two.

Here’s an example of launching an EC2 instance using the AWS-provided modules from the Terraform Registry:

provider "aws" {
region = "us-west-2"
}

module "ec2_instance" {
source = "terraform-aws-modules/ec2-instance/aws"
version = "3.3.0"

name = "my-ec2-instance"
instance_count = 1
ami = "ami-0c94855ba95c71c99"
instance_type = "t2.micro"
key_name = "my-key-pair"
subnet_id = "subnet-0123456789abcdef0"
vpc_security_group_ids = ["sg-0123456789abcdef0"]
}

In this example, we’re using the terraform-aws-modules/ec2-instance/aws module from the Terraform Registry. This module simplifies the provisioning of EC2 instances by providing a set of configurable options. Here's a breakdown of the parameters used:

  • name: Specifies the name tag for the EC2 instance.
  • instance_count: Specifies the number of instances to launch.
  • ami: Specifies the ID of the Amazon Machine Image (AMI) to use for the instance.
  • instance_type: Specifies the instance type for the EC2 instance.
  • key_name: Specifies the name of the key pair to associate with the instance.
  • subnet_id: Specifies the ID of the subnet in which to launch the instance.
  • vpc_security_group_ids: Specifies a list of security group IDs to associate with the instance.

You can modify the values of these parameters to match your requirements. Make sure to replace the placeholder values (e.g., ami-0c94855ba95c71c99, subnet-0123456789abcdef0, sg-0123456789abcdef0) with the appropriate values from your AWS account.

With this example, you can launch an EC2 instance using the AWS-provided Terraform module, simplifying the configuration and provisioning process. Remember to run terraform init to download the necessary modules before applying the Terraform configuration.

Conclusion:
Terraform modules empower you to organize and reuse your infrastructure code effectively. By following the examples provided, you can start creating modular infrastructure deployments. Remember to explore the Terraform Registry for public modules and experiment with different module compositions. With practice, you’ll become proficient in leveraging Terraform modules for scalable and maintainable infrastructure-as-code. Happy module building in Terraform!

--

--

Praveen Dandu

🚀 DevOps Engineer | Automating Infrastructure, Streamlining Deployments | Continuous Integration & Delivery Specialist https://www.linkedin.com/in/pravin24/