Friday, April 25, 2025

Different Ways twtech Creates EC2 Instances in AWS: Usecases.

 

There are several ways to create EC2 instances in AWS, ranging from:  manual creation through the console(sometimes bootstrapping packages)  to fully automated methods.

Here is twtech breakdown of the most common approaches:

 Manual Methods

  1. AWS Management Console (UI)
    • Easiest way for beginners.
    • Step-by-step wizard to configure AMI, instance type, key pair, storage, network, security group, etc.
  2. AWS CLI (Command Line Interface)
    • Useful for scripting or quick instance launches.
    • Example:

#   bash

 

aws ec2 run-instances --image-id ami-1234567xxxxx  --count 1 --instance-type t2.micro --key-name twtechKeyPair --security-groups twtech-SecurityGroup

 

Or:

 

#  bash

 

aws ec2 run-instances \

  --image-id ami-1234567xxxxx \

  --count 1 \

  --instance-type t2.micro \

  --key-name twtechKeyPair \

  --security-groups twtech-SecurityGroup

  1. AWS SDKs (Boto3 for Python, AWS SDK for JavaScript, etc.)
    • Great for integrating instance creation into your applications or backend services.
    • Python (Boto3) example:

#  python

 

import boto3

ec2 = boto3.resource('ec2')

ec2.create_instances(ImageId='ami-12345678xxx', MinCount=1, MaxCount=1, InstanceType='t2.micro')

Infrastructure as Code (IaC) Tools

  1. AWS CloudFormation
    • Declarative IaC tool.
    • Define instances and other resources in YAML or JSON.
    • Example snippet:

#  yaml

 

Resources:

  MyEC2Instance:

    Type: AWS::EC2::Instance

    Properties:

      InstanceType: t2.micro

      ImageId: ami-12345678xxx

      KeyName: twtechKeyPair

  1. Terraform (by HashiCorp)
    • Popular cross-cloud IaC tool.

twtech-refactored terraform codes.

# resources.tf

resource "aws_instance" "twtech" {

  ami             = var.ami

  instance_type   = var.instance

  key_name        = var.key

  user_data       = file("${path.module}/userdata.sh")

  count           = 1

  subnet_id       = var.subnet

  security_groups = ["sg-00215f09ece4xxxx"]

  tags = {

    Name = "twtech-sonar-maven-server"

    env  = var.namespace

  }

  root_block_device {

    volume_size = 20 # Size of the root volume in GB

  }

}

# variables.tf

variable "instance" {

  type    = string

  default = "t2.medium"

}

variable "key" {

  type    = string

  default = "twtech-KeyPair"

}

variable "namespace" {

  type    = string

  default = "dev"

}

variable "subnet" {

  type    = string

  default = "subnet-0c2261460dbxxxxxx"

}

variable "ami" {

  type    = string

  default = "ami-0720ac433axxxxx"

}

# providers.tf

terraform {

  required_version = "~> 1.0"

  required_providers {

    aws = {

      source  = "hashicorp/aws"

      version = "~> 5.0"

    }

  }

}

# twtech-sonar-maven-userdata

Link to the userdata-script:

https://github.com/Devopspat35/Package-management/blob/master/twtech-sonarqube.sh

  1. Pulumi
    • IaC using general-purpose programming languages (Python, Go, JavaScript, etc.).
    • Similar to Terraform but more code-centric.

 Automated & Advanced Methods

  1. AWS EC2 Auto Scaling Groups
    • Automatically launch EC2 instances based on demand or schedules.
    • Useful for horizontal scaling and fault tolerance.
  2. Elastic Beanstalk
    • PaaS abstraction; handles EC2 instance creation automatically as part of environment setup.
  3. AWS OpsWorks
    • Uses Chef/Puppet to manage EC2 instances.
    • Useful if you're in a configuration management-heavy environment.
  4. EC2 Image Builder
  • Automates creation of golden AMIs and can include EC2 launch steps.
  1. AWS CDK (Cloud Development Kit)
  • Infrastructure defined using familiar programming languages (TypeScript, Python, etc.).
  • Example in Python:

#  python


from aws_cdk import aws_ec2 as ec2

 

instance = ec2.Instance(self, "twtech-Instance",

  instance_type=ec2.InstanceType("t2.micro"),

  machine_image=ec2.MachineImage.latest_amazon_linux()

)

 Project: 

Method ONE

Installation of Sonarqube from:

  1. AWS Management Console (UI)
    • Easiest way for beginners.
    • Step-by-step wizard to configure AMI, instance type, key pair, storage, network, security group, etc.
    • twtech sometime Bootstraps the needed dependencies, packages and applications while provisioning the instance.

twtech-web-server creation: GUI

Dependencies:

 t2.medium and above,

openjdk-11 (java-11)

Search for ec2 from among aws services:


How twtech Launchs instance from console: UI

How twtech Configures the variables and resources for the instances:

Select the Amazon Machine Image


For the purpose of this project, twtech is using:  t2.medium

Key pair for login can be selected from: dropdown menu or created.

Network settings: for security reason, twtech open traffic to only the needed ports.

Configure storage: The root-volume starts from 8Gigabytes. Volume can not be decreased once created, but twtech can increase up to 30G which is the maximum free tier eligible size.

 From:

To:

Advanced details:  To bootstrap package or Userdata:

# Sonarqube installation script path

https://github.com/Devopspat35/Package-management/blob/master/twtech-sonarqube.sh


twtech may have the script written, copied and pasted or uploaded


launch the instance:

It takes a couple of minutes for packages to be fully bootstrapped on the instance.

Refresh page:

Connect to the instance to verify that the needed configurations, and resources were successfully provisioned (bootstrapped)

Using ssh client ( VSCode, mobaxterm, putty, intelliJ idea …etc ), ssh into the sever:

We are are using VSCode from gitbash terminal

Navigate to location of the key.pem( twtechKeyPai) create

cd ~/Downloads


Connect to the instance: twtech-sonarqube-server

with ssh-client:

ssh -i "twtech-KeyPair.pem" ec2-user@ec2-3-148-xxx-187.us-east-2.compute.amazonaws.com

Successfully, twtech has bootstrapped sonarqube in a redhat instance with all the needed dependencies, assigne it the nee permissions, initialize the server, started the services and switched to sonar-usrer

# switch to sonar user

 sudo su - sonar

# verify that sonar have ownership of sonarqube home directory

ls -al /opt/sonarqube

#  How twtech starts sonarQube server

 sh /opt/sonarqube/bin/linux-x86-64/sonar.sh start

# How twtech verifies the sonarqube server status.

  sh /opt/sonarqube/bin/linux-x86-64/sonar.sh status

#  How twtech Accesses twtech-sonarqube-server: Access sonarqube on the browser

curl ifconfig.me                # To get host server pubIPaddress.

# twtech-serverPubIP:9000

18.191.xxx.2:9000

# Default USERNAME: admin

# Default password: admin


Once login as admin, other twech users are created and passwords assigned .


How twtech-admin-user creates other sonar-users.




Sign out as admin user

sign as twtech-patpat:

twtech-patpat can not create other sonar user, because of readonly access.

twtech Successfully created sonar user  (twtech-patpat) and used credentials to login

Project: 

Method Two

Installation of instance (twtech-webserver)  from:  command line interface

2,  AWS CLI (Command Line Interface)

We can use the Ubuntu (subsystem for window), Powershell, gitbash or Command prompt.

Terminal must be configured with aws-access-keys


twtech makes sure the appropriate  Amazon Machine Image (ami) of choice is referenced:

Assign the Security-groupID


Useful for scripting or quick instance launches. CLI

#   bash

 

aws ec2 run-instances --image-id ami-1234567xxxxx  --count 1 --instance-type t2.micro --key-name twtech-KeyPair –security­-groups-ids sg-0cfb2xxxxxxx --subnet-id subnet-6e7xxx --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=twtech-webserver}]'

 

or:

 

aws ec2 run-instances --image-id ami-1234567xxxxx  --count 1 --instance-type t2.micro --key-name twtech-KeyPair –security­-groups-ids sg-0cfb2xxxxxxx --subnet-id subnet-6e7f82xxx --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=twtech-db-server}]'

Explanation of Parameters:

  • --image-id: AMI ID to launch (choose one from your region).
  • --count: Number of instances to launch.
  • --instance-type: EC2 instance type (e.g., t2.micro for free tier).
  • --key-name: Name of the existing EC2 key pair for SSH access.
  • --security-group-ids: Security group IDs to attach.
  • --subnet-id: Subnet within your VPC.
  • --tag-specifications: Optional tags to label the instance,


twtech Verifies on console to make sure the instance is provisioned using command line: GUI


twtech-insights:

Make sure your AWS CLI is configured (aws configure) with appropriate credentials and region referenced.

Project: 

Method Three   

Infrastructure as Code (IaC) Tools

Terraform (by HashiCorp)

twtech uses tarraform (.tf) files to reference the: resources, variables, provider and userdata (if possible to bootstrap)

How twtech refactors terraform codes.

# resources.tf

resource "aws_instance" "twtech" {

  ami             = var.ami

  instance_type   = var.instance

  key_name        = var.key

  user_data       = file("${path.module}/userdata.sh")

  count           = 1

  subnet_id       = var.subnet

  security_groups = ["sg-00215f09ece4xxxx"]

  tags = {

    Name = "twtech-sonar-maven-server"

    env  = var.namespace

  }

  root_block_device {

    volume_size = 20 # Size of the root volume in GB

  }

}

# variables.tf

variable "instance" {

  type    = string

  default = "t2.medium"

}

variable "key" {

  type    = string

  default = "twtech-KeyPair"

}

variable "namespace" {

  type    = string

  default = "dev"

}

variable "subnet" {

  type    = string

  default = "subnet-0c2261460dbxxxxxx"

}

variable "ami" {

  type    = string

  default = "ami-0720ac433axxxxx"

}

# providers.tf

terraform {

  required_version = "~> 1.0"

  required_providers {

    aws = {

      source  = "hashicorp/aws"

      version = "~> 5.0"

    }

  }

}

# twtech-sonar-maven-userdata

Link to the userdata-script:

https://github.com/Devopspat35/Package-management/blob/master/twtech-sonarqube.sh


How twtech connects in sonar-maven server


ssh -i "devsecopspat.pem" ec2-user@ec2-128-221-xxx-20.us-east-2.compute.amazonaws.com
How twtech verifies that all the dependecies , and applications are bootstrapped. 

 verify installed packages
 java -version

 mvn --version

 npm --version

 switch to sonar user
 sudo su - sonar
 How twtech verifies that the home directory of sonar is owned by sonar user. twtech waits two minutes for packages and directories to be fully created.
ls -al /opt/sonarqube

How twtech starts and verify the status of the sonar-maven-server.
 sh /opt/sonarqube/bin/linux-x86-64/sonar.sh start 
 sh /opt/sonarqube/bin/linux-x86-64/sonar.sh status

How twtech Accesses the sonar-maven-server application logs: CLI
How twtech Gets the host pubIPaddress with command line:
curl ifconfig.me

curl -v 3.142.xxx.237:9000
How twtech accesses sonarqube serve on the browser.

#  PubIP:9000
142.xxx.237:9000
Login to sonarqube application:
The default USERNAME: admin
The default password: admin

twtech-admin can successfully start creating: projects, user, passwords and threshold (standard or quality gate) , and  much more.

How twtech-admin-user creates other sonar-users.




Sign out as admin user

sign as twtech-patpat:

twtech-patpat can not create other sonar user, because of readonly access.

twtech Successfully created sonar user  (twtech-patpat) and used credentials to login

No comments:

Post a Comment

Kubernetes Clusters | Upstream Vs Downstream.

  The terms "upstream" and "downstream" in the context of Kubernetes clusters often refer to the direction of code fl...