Terraform - Multi Resource File
Efficient Terraform Setup: Using Multiple Resource Files
Using multiple files in Terraform helps keep things organized and easy to understand. Each file can focus on a specific part of your setup, making it simpler to manage and update. It also allows you to reuse parts in different projects, work better with others and keep different parts of your setup separate and clear.
Example for Multi Resource File:
Create provider.tf file,
The provider
block in Terraform is used to configure the specified provider, in this case it's AWS.
The region
parameter is set to a variable, var.REGION
, which allows you to dynamically specify the AWS region where your resources will be created.
This setup is essential for defining the provider's configuration and ensuring that Terraform knows which cloud provider and region to interact with.
provider "aws" {
region = var.REGION
}
The backend.tf
file in Terraform is used to configure the backend where Terraform's state file is stored. In this example, the backend is configured to use Amazon S3.
The bucket
parameter specifies the S3 bucket name, the key
parameter defines the path within the bucket where the state file will be stored and the region
parameter specifies the AWS region where the bucket is located.
This setup ensures that the Terraform state is stored remotely, providing better collaboration and state management.
terraform {
backend "s3" {
bucket = "terraform-bucket-ragavi"
key = "terraform/backend"
region = "us-east-2"
}
}
The var.tf
file in Terraform defines several variables that are used throughout the Terraform configuration. These variables provide default values for the AWS region, availability zones, AMI IDs, user credentials and IP address.
This setup allows for flexible and reusable configurations by parameterizing values that can be easily changed without modifying the main configuration files.
variable REGION {
default = "us-east-2"
}
variable ZONE1 {
default = "us-east-2a"
}
variable ZONE2 {
default = "us-east-2b"
}
variable ZONE3 {
default = "us-east-2c"
}
variable "AMIS" {
type = map(any)
default = {
us-east-2 = "ami-0649bea3443ede307"
us-east-1 = "ami-0947d2ba12ee1ff75"
}
}
variable USER {
default = "ec2-user"
}
variable PUB_KEY {
default = "terraform-key.pub"
}
variable PRIV_KEY {
default = "terraform-key"
}
variable MYIP {
default = "Your ip"
}
The instance.tf
file in Terraform defines several AWS resources and their configurations. It includes an AWS key pair, an EC2 instance, an EBS volume and the attachment of the EBS volume to the EC2 instance.
Additionally, it outputs the public IP address of the EC2 instance.
This setup is useful for creating and managing an EC2 instance with a specific key pair and attaching a storage volume to it.
resource "aws_key_pair" "terraform-key" {
key_name = "terraform-key"
public_key = file("terraform-key.pub")
}
resource "aws_instance" "exercise6-inst" {
ami = var.AMIS[var.REGION]
instance_type = "t2.micro"
availability_zone = var.ZONE1
key_name = aws_key_pair.terraform-key.key_name
vpc_security_group_ids = ["sg-0e34c2d3e71d616fa"]
tags = {
Name = "exercise6-Instance"
Project = "exercise6"
}
}
resource "aws_ebs_volume" "volume_exercise6" {
availability_zone = var.ZONE1
size = 3
tags = {
Name = "volume_exercise6"
}
}
resource "aws_volume_attachment" "attach_terraform" {
device_name = "/dev/xvdh"
volume_id = aws_ebs_volume.volume_exercise6.id
instance_id = aws_instance.exercise3-inst.id
}
output "PublicIP" {
value = aws_instance.exercise3-inst.public_ip
}
The secgrp.tf
file in Terraform defines an AWS Security Group resource. This security group allows SSH access from a specific IP address and permits all outbound traffic. The vpc_id
parameter links the security group to a specific VPC and the name
and description
provide metadata for the security group.
The egress
block allows all outbound traffic, while the ingress
block restricts inbound SSH traffic to the IP address specified by the var.MYIP
variable.
Tags are used to label the security group for easier identification.
resource "aws_security_group" "terraform_sg" {
vpc_id = aws_vpc.vpc_rag.id
name = "terraform_sg"
description = "Security group for instance"
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [var.MYIP]
}
tags = {
Name = "allow-ssh"
}
}
The vpc.tf
file in Terraform defines the configuration for an AWS VPC and its associated subnets.
The VPC is created with a CIDR block 10.0.0.0/16
and has DNS support and hostnames enabled.
Three public subnets are created within this VPC, each in a different availability zone and with their own CIDR blocks.
The subnets are configured to automatically assign public IP addresses to instances launched within them.
Tags are used to label the VPC and subnets for easier identification.
resource "aws_vpc" "vpc_rag" {
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"
enable_dns_support = "true"
enable_dns_hostnames = "true"
tags = {
Name = "vpc_rag"
}
}
resource "aws_subnet" "rag-pub-1" {
vpc_id = aws_vpc.vpc_rag.id
cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = "true"
availability_zone = var.ZONE1
tags = {
Name = "rag-pub-1"
}
}
resource "aws_subnet" "rag-pub-2" {
vpc_id = aws_vpc.vpc_rag.id
cidr_block = "10.0.2.0/24"
map_public_ip_on_launch = "true"
availability_zone = var.ZONE2
tags = {
Name = "rag-pub-2"
}
}
resource "aws_subnet" "rag-pub-3" {
vpc_id = aws_vpc.vpc_rag.id
cidr_block = "10.0.3.0/24"
map_public_ip_on_launch = "true"
availability_zone = var.ZONE3
tags = {
Name = "rag-pub-3"
}
}
This is an example of a multi-resource file.
Refer: https://github.com/Ragavi04P/Terraform-Practice/tree/main/exercise6
Output: