Terraform script to automate the deployment of an AWS Graviton EC2 instance

Terraform script to automate the deployment of an AWS Graviton EC2 instance

Here’s a Terraform script to automate the deployment of an AWS Graviton EC2 instance. This script will:

Launch a Graviton-based EC2 instance (M7g, C7g, R7g, or T4g)
Use an ARM64 Amazon Linux 2 AMI
Attach a security group for SSH access
Set up user data for initialization


🔹 Step 1: Install Terraform (If Not Installed)

wget https://releases.hashicorp.com/terraform/1.5.5/terraform_1.5.5_linux_amd64.zip
unzip terraform_1.5.5_linux_amd64.zip
sudo mv terraform /usr/local/bin/
terraform -v

🔹 Step 2: Create Terraform Configuration File

Create a new file named graviton-ec2.tf and paste the following code:

provider "aws" {
  region = "us-east-1"  # Change to your preferred region
}

resource "aws_vpc" "graviton_vpc" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "graviton_subnet" {
  vpc_id                  = aws_vpc.graviton_vpc.id
  cidr_block              = "10.0.1.0/24"
  map_public_ip_on_launch = true
  availability_zone       = "us-east-1a"  # Change if needed
}

resource "aws_security_group" "graviton_sg" {
  vpc_id = aws_vpc.graviton_vpc.id

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]  # Allow SSH from anywhere (change for security)
  }

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  ingress {
    from_port   = 443
    to_port     = 443
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_instance" "graviton_ec2" {
  ami           = "ami-0900fe555666598a2"  # Amazon Linux 2 ARM64 AMI (Update as needed)
  instance_type = "t4g.medium"  # Change to m7g.large, c7g.xlarge, etc.
  subnet_id     = aws_subnet.graviton_subnet.id
  security_groups = [aws_security_group.graviton_sg.name]
  key_name      = "my-key-pair"  # Replace with your existing key pair name

  user_data = <<-EOF
              #!/bin/bash
              yum update -y
              yum install -y httpd
              systemctl start httpd
              systemctl enable httpd
              echo "Graviton Server is running on ARM64!" > /var/www/html/index.html
              EOF

  tags = {
    Name = "Graviton-Instance"
  }
}

🔹 Step 3: Initialize and Deploy

Run the following Terraform commands:

terraform init
terraform apply -auto-approve

🔹 Step 4: Verify and Connect to Your Instance

Once Terraform completes, find the public IP of your instance:

terraform output

SSH into the instance:

ssh -i my-key.pem ec2-user@your-instance-ip

Check if the web server is running:

curl http://your-instance-ip

✅ Conclusion

  • 🚀 This Terraform script automates Graviton EC2 instance creation
  • 🌍 You can change instance types (T4g, M7g, C7g, R7g) based on workload
  • 🔧 User data script sets up a basic web server
  • 🔄 Can be modified for Kubernetes (EKS), Docker, or other workloads

Would you like a Terraform module to set up an entire Graviton-based Kubernetes cluster (EKS)? 🚀

About Anant 443 Articles
Senior technical writer

1 Trackbacks & Pingbacks

  1. Step-by-Step Guide: Migrating from x86 to AWS Graviton 🚀 – KTCHost

Comments are closed.