Skip to main content

Amazon EC2

Overview

The AWS EC2 Module provides a reusable and scalable way to provision and manage Amazon Elastic Compute Cloud (EC2) instances using Terraform.
It abstracts the complexity of configuring EC2 resources and enables developers to define compute infrastructure with minimal effort while following AWS best practices.

This module supports both resource creation and data sources for managing EC2 instances, networking, and security configurations in a consistent and modular way.


Features

  • 🚀 Reusable & Modular – Predefined structure for resources and data modules.
  • 🔐 Security Best Practices – Supports key pairs, security groups, and IAM roles.
  • Instance Flexibility – Choose from a variety of instance types and AMIs.
  • 📡 Networking Support – Configure VPC, subnets, and Elastic IPs.
  • 📜 Monitoring & Logging – Enable CloudWatch monitoring and detailed instance logs.
  • 🧩 Extensible – Easy to integrate with other Terraform modules and AWS services.

Core Concepts

  1. Resources Module

    • Creates and manages EC2 instances and related configurations.
    • Includes support for:
      • Instance type & AMI selection
      • Key pair association
      • Security groups & network interfaces
      • Elastic IP attachment
      • User data & bootstrap scripts
      • IAM roles and instance profiles
  2. Data Module

    • Fetches details of existing EC2 resources.
    • Useful for referencing:
      • Existing AMIs
      • Existing instances
      • Security groups or key pairs
    • Helps integrate EC2 instances with other infrastructure components.
  3. Networking & Security

    • Supports VPC and subnet configuration.
    • Attach security groups and NACLs for secure access.
    • Optional public IP allocation for internet-facing instances.
  4. Separation of Concerns

    • resources/ handles EC2 instance creation and configuration.
    • data/ is focused on reading and referencing existing EC2 resources.
    • This separation ensures clarity, reusability, and modular design.

Sample Usage

# DEPLOY ANANTA CLOUD'S EC2 MODULE

module "ec2_instance" {
source = "github.com/anantacloud/terraform-aws-compute-module/modules/resources"

# ==========================
# REQUIRED VARIABLES
# ==========================
instance_type = "t2.micro"
ami_id = "ami-0c55b159cbfafe1f0"
subnet_id = "subnet-123456"
key_name = "my-key"

# ==========================
# OPTIONAL VARIABLES
# ==========================
associate_public_ip = true
security_groups = ["sg-123456"]

user_data = <<-EOF
#!/bin/bash
echo "Hello, World" > /var/www/html/index.html
EOF

tags = {
Environment = "dev"
Project = "my-app"
}
}

Required

NameDescriptionTypeExample
instance_typeEC2 instance typestring"t2.micro"
ami_idAMI ID to launch the instancestring"ami-0c55b159cbfafe1f0"
subnet_idSubnet ID where the instance will be launchedstring"subnet-123456"
key_nameName of the SSH key pairstring"my-key"

Optional

NameDescriptionTypeDefault
associate_public_ipAssign a public IP to the instanceboolfalse
security_groupsList of security group IDs to attachlist[]
user_dataUser data script for instance initializationstringnull
tagsAdd custom tags to the instancemap{}