AWS EC2
Deploy and manage Amazon EC2 compute instances for production, staging, and development workloads.
When to Use This Skill
- Launching new compute instances for application hosting
- Building golden AMIs for consistent deployments
- Setting up auto-scaling groups behind load balancers
- Migrating workloads to Spot instances for cost savings
- Troubleshooting instance connectivity, performance, or launch failures
- Creating launch templates for repeatable infrastructure
Prerequisites
- AWS CLI v2 installed and configured (
aws configure)
- Least-privilege IAM actions for selected compute, scaling and load-balancer resources; restrict iam:PassRole to the intended instance role
- An existing VPC with subnets (see aws-vpc (
aws-vpc))
- SSH key pair created (
umask 077 before creating a key file in a private directory; prefer approved session access where appropriate)
Instance Type Selection Guide
| Category |
Types |
Use Case |
| General Purpose |
t3, t3a, m6i, m7g |
Web servers, small databases, dev/test |
| Compute Optimized |
c6i, c7g |
Batch processing, media encoding, ML inference |
| Memory Optimized |
r6i, r7g, x2idn |
In-memory caches, large databases |
| Storage Optimized |
i3, i4i, d3 |
Data warehousing, distributed file systems |
| Accelerated |
p4d, g5, inf2 |
ML training, GPU rendering, inference |
| Burstable |
t3.micro-t3.2xlarge |
Low-steady-state with occasional bursts |
Launch an Instance
# Launch a production web server
aws ec2 run-instances \
--image-id ami-0abcdef1234567890 \
--instance-type t3.medium \
--key-name my-key \
--security-group-ids sg-12345678 \
--subnet-id subnet-12345678 \
--iam-instance-profile Name=EC2AppProfile \
--metadata-options "HttpTokens=required,HttpEndpoint=enabled" \
--block-device-mappings '[{
"DeviceName": "/dev/xvda",
"Ebs": {
"VolumeSize": 30,
"VolumeType": "gp3",
"Iops": 3000,
"Throughput": 125,
"Encrypted": true
}
}]' \
--tag-specifications 'ResourceType=instance,Tags=[
{Key=Name,Value=web-server-01},
{Key=Environment,Value=production},
{Key=Team,Value=platform}
]' \
--user-data file://userdata.sh
# Launch with IMDSv2 required (security best practice)
aws ec2 run-instances \
--image-id ami-0abcdef1234567890 \
--instance-type t3.micro \
--metadata-options "HttpTokens=required,HttpPutResponseHopLimit=1,HttpEndpoint=enabled" \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=secure-instance}]'
User Data Scripts
#!/bin/bash
# userdata.sh - Bootstrap a web server on Amazon Linux 2023
set -euo pipefail # avoid tracing secrets in user-data logs
# System updates
dnf update -y
# Install and start web server
dnf install -y nginx
systemctl enable nginx
systemctl start nginx
# Install CloudWatch agent
dnf install -y amazon-cloudwatch-agent
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl \
-a fetch-config -m ec2 \
-s -c ssm:AmazonCloudWatch-linux
# Install CodeDeploy agent
dnf install -y ruby wget
cd /home/ec2-user
wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/latest/install
# Verify a reviewed pinned installer and integrity before execution; do not run latest blindly.
# Signal CloudFormation (if launched via CFN)
# /opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource ASG --region ${AWS::Region}
Launch Templates
# Create a launch template with full configuration
aws ec2 create-launch-template \
--launch-template-name web-server-template \
--version-description "v1 - AL2023 with nginx" \
--launch-template-data '{
"ImageId": "ami-0abcdef1234567890",
"InstanceType": "t3.medium",
"KeyName": "my-key",
"SecurityGroupIds": ["sg-12345678"],
"IamInstanceProfile": {"Name": "EC2AppProfile"},
"MetadataOptions": {
"HttpTokens": "required",
"HttpEndpoint": "enabled"
},
"BlockDeviceMappings": [{
"DeviceName": "/dev/xvda",
"Ebs": {
"VolumeSize": 30,
"VolumeType": "gp3",
"Encrypted": true
}
}],
"TagSpecifications": [{
"ResourceType": "instance",
"Tags": [
{"Key": "Environment", "Value": "production"},
{"Key": "ManagedBy", "Value": "launch-template"}
]
}],
"Monitoring": {"Enabled": true},
"UserData": "'"$(base64 -w0 userdata.sh)"'"
}'
# Create a new version of the launch template
aws ec2 create-launch-template-version \
--launch-template-name web-server-template \
--source-version 1 \
--version-description "v2 - updated AMI" \
--launch-template-data '{"ImageId": "ami-0newami1234567890"}'
# Set the default version
aws ec2 modify-launch-template \
--launch-template-name web-server-template \
--default-version 2
Auto Scaling Group
# Create ASG with mixed instances (on-demand + spot)
aws autoscaling create-auto-scaling-group \
--auto-scaling-group-name web-asg \
--m
1---2name: aws-ec23description: Manage EC2 instances, AMIs, and auto-scaling groups. Configure security groups, key pairs, and instance types. Use when deploying compute resources on AWS. 4category: AI & Agents5source: antigravity6tags: [ai, agent, template, image, security, aws, rag, cro]7url: https://github.com/sickn33/antigravity-awesome-skills/tree/main/skills/aws-ec28metadata:9 source: https://skillmd.com/skills/ranbot-ai/aws-ec210---111213# AWS EC21415Deploy and manage Amazon EC2 compute instances for production, staging, and development workloads.1617## When to Use This Skill1819- Launching new compute instances for application hosting20- Building golden AMIs for consistent deployments21- Setting up auto-scaling groups behind load balancers22- Migrating workloads to Spot instances for cost savings23- Troubleshooting instance connectivity, performance, or launch failures24- Creating launch templates for repeatable infrastructure2526## Prerequisites2728- AWS CLI v2 installed and configured (`aws configure`)29- Least-privilege IAM actions for selected compute, scaling and load-balancer resources; restrict iam:PassRole to the intended instance role30- An existing VPC with subnets (see aws-vpc (`aws-vpc`))31- SSH key pair created (`umask 077` before creating a key file in a private directory; prefer approved session access where appropriate)3233## Instance Type Selection Guide3435| Category | Types | Use Case |36|---|---|---|37| General Purpose | t3, t3a, m6i, m7g | Web servers, small databases, dev/test |38| Compute Optimized | c6i, c7g | Batch processing, media encoding, ML inference |39| Memory Optimized | r6i, r7g, x2idn | In-memory caches, large databases |40| Storage Optimized | i3, i4i, d3 | Data warehousing, distributed file systems |41| Accelerated | p4d, g5, inf2 | ML training, GPU rendering, inference |42| Burstable | t3.micro-t3.2xlarge | Low-steady-state with occasional bursts |4344## Launch an Instance4546```bash47# Launch a production web server48aws ec2 run-instances \49 --image-id ami-0abcdef1234567890 \50 --instance-type t3.medium \51 --key-name my-key \52 --security-group-ids sg-12345678 \53 --subnet-id subnet-12345678 \54 --iam-instance-profile Name=EC2AppProfile \55 --metadata-options "HttpTokens=required,HttpEndpoint=enabled" \56 --block-device-mappings '[{57 "DeviceName": "/dev/xvda",58 "Ebs": {59 "VolumeSize": 30,60 "VolumeType": "gp3",61 "Iops": 3000,62 "Throughput": 125,63 "Encrypted": true64 }65 }]' \66 --tag-specifications 'ResourceType=instance,Tags=[67 {Key=Name,Value=web-server-01},68 {Key=Environment,Value=production},69 {Key=Team,Value=platform}70 ]' \71 --user-data file://userdata.sh7273# Launch with IMDSv2 required (security best practice)74aws ec2 run-instances \75 --image-id ami-0abcdef1234567890 \76 --instance-type t3.micro \77 --metadata-options "HttpTokens=required,HttpPutResponseHopLimit=1,HttpEndpoint=enabled" \78 --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=secure-instance}]'79```8081## User Data Scripts8283```bash84#!/bin/bash85# userdata.sh - Bootstrap a web server on Amazon Linux 202386set -euo pipefail # avoid tracing secrets in user-data logs8788# System updates89dnf update -y9091# Install and start web server92dnf install -y nginx93systemctl enable nginx94systemctl start nginx9596# Install CloudWatch agent97dnf install -y amazon-cloudwatch-agent98/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl \99 -a fetch-config -m ec2 \100 -s -c ssm:AmazonCloudWatch-linux101102# Install CodeDeploy agent103dnf install -y ruby wget104cd /home/ec2-user105wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/latest/install106# Verify a reviewed pinned installer and integrity before execution; do not run latest blindly.107108# Signal CloudFormation (if launched via CFN)109# /opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource ASG --region ${AWS::Region}110```111112## Launch Templates113114```bash115# Create a launch template with full configuration116aws ec2 create-launch-template \117 --launch-template-name web-server-template \118 --version-description "v1 - AL2023 with nginx" \119 --launch-template-data '{120 "ImageId": "ami-0abcdef1234567890",121 "InstanceType": "t3.medium",122 "KeyName": "my-key",123 "SecurityGroupIds": ["sg-12345678"],124 "IamInstanceProfile": {"Name": "EC2AppProfile"},125 "MetadataOptions": {126 "HttpTokens": "required",127 "HttpEndpoint": "enabled"128 },129 "BlockDeviceMappings": [{130 "DeviceName": "/dev/xvda",131 "Ebs": {132 "VolumeSize": 30,133 "VolumeType": "gp3",134 "Encrypted": true135 }136 }],137 "TagSpecifications": [{138 "ResourceType": "instance",139 "Tags": [140 {"Key": "Environment", "Value": "production"},141 {"Key": "ManagedBy", "Value": "launch-template"}142 ]143 }],144 "Monitoring": {"Enabled": true},145 "UserData": "'"$(base64 -w0 userdata.sh)"'"146 }'147148# Create a new version of the launch template149aws ec2 create-launch-template-version \150 --launch-template-name web-server-template \151 --source-version 1 \152 --version-description "v2 - updated AMI" \153 --launch-template-data '{"ImageId": "ami-0newami1234567890"}'154155# Set the default version156aws ec2 modify-launch-template \157 --launch-template-name web-server-template \158 --default-version 2159```160161## Auto Scaling Group162163```bash164# Create ASG with mixed instances (on-demand + spot)165aws autoscaling create-auto-scaling-group \166 --auto-scaling-group-name web-asg \167 --m