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
- AWS Management Console (UI)
- Easiest way for beginners.
- Step-by-step wizard to configure AMI, instance type,
key pair, storage, network, security group, etc.
- 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
- 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
- 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
- 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
- Pulumi
- IaC using general-purpose programming languages
(Python, Go, JavaScript, etc.).
- Similar to Terraform but more code-centric.
Automated & Advanced Methods
- AWS EC2 Auto Scaling Groups
- Automatically launch EC2 instances based on demand or
schedules.
- Useful for horizontal scaling and fault tolerance.
- Elastic Beanstalk
- PaaS abstraction; handles EC2 instance creation
automatically as part of environment setup.
- AWS OpsWorks
- Uses Chef/Puppet to manage EC2 instances.
- Useful if you're in a configuration management-heavy
environment.
- EC2 Image Builder
- Automates creation of golden AMIs and can include EC2
launch steps.
- 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:
- 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 Configures the variables
and resources for the instances:
Select the Amazon Machine Image
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.
From:
To:
# Sonarqube installation script
path
https://github.com/Devopspat35/Package-management/blob/master/twtech-sonarqube.sh
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
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
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
Assign the Security-groupID
# 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
No comments:
Post a Comment