This file contains the Terraform configuration for the EC2 instance.
hcl
# main.tf
# Create a new AWS provider
provider "aws" {
region = var.region
}
# Create a new VPC
resource "aws_vpc" "example" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "example-vpc"
}
}
# Create a new subnet
resource "aws_subnet" "example" {
vpc_id = aws_vpc.example.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-west-2a"
tags = {
Name = "example-subnet"
}
}
# Create a new security group
resource "aws_security_group" "example" {
vpc_id = aws_vpc.example.id
name = "example-sg"
description = "Allow inbound traffic on port 22"
# Allow inbound traffic on port 22
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
# Create a new EC2 instance
resource "aws_instance" "example" {
ami = var.ami
instance_type = var.instance_type
vpc_security_group_ids = [aws_security_group.example.id]
subnet_id = aws_subnet.example.id
tags = {
Name = "example-ec2"
}
}
variables.tf
This file contains the input variables for the module.
hcl
# variables.tf
variable "region" {
type = string
description = "The AWS region to create the resources in"
}
variable "ami" {
type = string
description = "The ID of the Amazon Machine Image (AMI) to use"
}
variable "instance_type" {
type = string
description = "The type of instance to start"
}
outputs.tf
This file contains the output values of the module.
hcl
# outputs.tf
output "ec2_public_ip" {
value = aws_instance.example.public_ip
description = "The public IP address of the EC2 instance"
}
output "ec2_private_ip" {
value = aws_instance.example.private_ip
description = "The private IP address of the EC2 instance"
}
Example Usage
To use this module, create a new Terraform configuration file (e.g., main.tf) and reference the module:
hcl
# main.tf
module "ec2" {
source = file("./module")
region = "us-west-2"
ami = "ami-abc123"
instance_type = "t2.micro"
}
Then, run Terraform to create the resources:
bash
terraform init
terraform apply -auto-approve
This will create a new EC2 instance in the specified region with the specified AMI and instance type. You can then view the public and private IP addresses of the instance using the output values:
bash
terraform output
Code Explanation
The module uses the aws provider to create a new VPC, subnet, security group, and EC2 instance. The aws_instance resource is used to create the EC2 instance, and the aws_security_group resource is used to create the security group.
The variables.tf file defines the input variables for the module, which are used to customize the creation of the resources. The outputs.tf file defines the output values of the module, which can be used to retrieve the public and private IP addresses of the EC2 instance.
The main.tf file contains the Terraform configuration for the EC2 instance. It uses the aws_instance resource to create the instance and the aws_security_group resource to create the security group. The aws_vpc resource is used to create the VPC, and the aws_subnet resource is used to create the subnet.
Error Handling
To handle errors, you can use the error function to catch and handle errors that occur during the creation of the resources. For example:
In this example, the remote-exec provisioner is used to run a command on the EC2 instance, and the local-exec provisioner is used to run a command locally. If the remote-exec provisioner fails, the local-exec provisioner will run and output an error message.
“Create terraform for creating aws ec2 module using aws terraform modules.” Conversation with llama-3.1-8b-instruct, from the LMArena Human Preference 100K dataset. The Chat Archive, 2026. https://thechatarchive.com/c/01m851hgm6r13pg1babs0gp9f6