ec2 cmd¶
others¶
get image id¶
get all ubuntu image from here (click to launch)¶
option-1-get-AL2-ami-id-¶
export AWS_DEFAULT_REGION=us-west-2
AMI_ID=$(aws ssm get-parameters \
--names /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2 \
--query 'Parameters[0].Value' --output text)
2022.6.27
us-east-1: ami-065efef2c739d613b
us-east-2: ami-07251f912d2a831a3
option 2¶
# ubuntu
export region=ap-southeast-1
aws ec2 describe-images --region ${region} --owners 099720109477 \
--filters Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64* \
--query 'Images[*].[ImageId,CreationDate,Name]' --output text |sort -k2 -r |column -t
# windows 2012
export region=ap-southeast-1
aws ec2 describe-images --region ${region} --owners 801119661308 \
--filter "Name=name,Values=Windows_Server-2012-R2_RTM-English-64Bit-Base*" \
--query 'Images[*].[ImageId,CreationDate,Name]' --output text |sort -k2 -r |column -t
# windows 2012
export AWS_DEFAULT_REGION=us-east-2
aws ec2 describe-images --owners 801119661308 \
--filter "Name=name,Values=Windows_Server-2019-English-Full-Base*" \
--query 'Images[*].[ImageId,CreationDate,Name]' --output text |sort -k2 -r |column -t
# amzn2
export region=ap-southeast-1
aws ec2 describe-images --region ${region} --owners 137112412989 \
--filters Name=name,Values=amzn2-ami-hvm-*2021*gp2* \
--query 'Images[*].[ImageId,CreationDate,Name]' --output text |sort -k2 -r |column -t
# centos
export AWS_DEFAULT_REGION=us-east-2
aws ec2 describe-images \
--filters Name=name,Values='CentOS Linux 7 x86_64*' \
--query 'Images[*].[ImageId,CreationDate,Name]' --output text \
|sort -k2 -r |column -t
# --owners 679593333241
export region=cn-northwest-1
awscn ec2 describe-images --region ${region} --owners 336777782633 \
--filters Name=name,Values='CentOS-7*' \
--query 'Images[*].[ImageId,CreationDate,Name]' --output text \
|sort -k2 -r |column -t
# centos ami
# https://wiki.centos.org/Cloud/AWS
# aws --region us-east-1 ec2 describe-images --owners aws-marketplace --filters Name=product-code,Values=cvugziknvmxgqna9noibqnnsy
get cloud9 newest image¶
export AWS_DEFAULT_REGION=us-east-2
aws ec2 describe-images --region ${AWS_DEFAULT_REGION} --owners amazon \
--filters "Name=name,Values=Cloud9AmazonLinux2-*" \
--query 'reverse(sort_by(Images, &CreationDate)[].[Name,ImageId])' \
--output text |column -t
create instance¶
# IMAGE_ID=ami-026bd3163cafd87ed #ubuntu
IMAGE_ID=ami-0f511ead81ccde020 #amzn2 ap-southeast-1
IMAGE_ID=ami-028584814b5504f5b #amzn2 cn-northwest-1
IMAGE_ID=ami-01b887d5e264569f5 #amzn2 cn-north-1
region=cn-north-1
KEY_NAME=awskey
aws ec2 run-instances --region ${region} --key-name $KEY_NAME \
--image-id $IMAGE_ID --instance-type c5.large --query Instances[*].InstanceId --output text
SUBNET_ID=
aws ec2 run-instances --region ${region} \
--image-id $IMAGE_ID --instance-type c5.large \
--subnet-id ${SUBNET_ID} --query Instances[*].InstanceId --output text
# centos
IMAGE_ID=ami-07f65177cb990d65b
AWS_REGION=ap-southeast-1
KEY_NAME=aws-key
echo '#!/bin/bash
sudo yum install -y https://s3.'"$AWS_REGION"'.amazonaws.com/amazon-ssm-'"$region"'/latest/linux_amd64/amazon-ssm-agent.rpm' |tee /tmp/tmp_$$.txt
aws ec2 run-instances --region ${AWS_REGION} --key-name $KEY_NAME \
--image-id $IMAGE_ID --instance-type t2.micro \
--user-data file:///tmp/tmp_$$.txt
windows instance¶
-
get image
-
create instance
# windows 2016 base IMAGE_ID=ami-02c88710773712fea AWS_REGION=us-east-2 # INSTANCE_PROFILE_ARN=arn:aws:iam::123456789012:instance-profile/windows-instance # windows 2016 base in china region IMAGE_ID=ami-0cdfdbad775669b71 AWS_REGION=cn-northwest-1 INSTANCE_PROFILE_ARN=arn:aws-cn:iam::123456789012:instance-profile/windows-instance KEY_NAME=awskey STR=$(date +%H%M) aws ec2 run-instances \ --region ${AWS_REGION} --key-name ${KEY_NAME} \ --image-id ${IMAGE_ID} --instance-type m5.large \ --iam-instance-profile Arn=${INSTANCE_PROFILE_ARN} \ --private-dns-name-options "HostnameType=ip-name,EnableResourceNameDnsARecord=true,EnableResourceNameDnsAAAARecord=false" --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=win-'"${STR}"'},{Key=os,Value=windows}]' |tee /tmp/instance-$$.1 INST_ID=$(cat /tmp/instance-$$.1 |jq -r '.Instances[0].InstanceId') # private dns name option is important for join domain # false/false will run ssm document failed # true/false will run ssm document successful
another example:
list instance¶
# using Name to filter
aws ec2 describe-instances |jq -r '.Reservations[].Instances[] | select((.Tags[]|select(.Key=="Name")|.Value) | match(".*") ) | [.InstanceId, (del((.Tags[]|select(.Key!="Name")))|.Tags[]|.Value|tostring)]|@tsv'
# list all instance, name, ips
aws ec2 describe-instances |jq -r '.Reservations[].Instances[] |
[.InstanceId, .State.Name, .PrivateIpAddress, .PublicIpAddress, (del((.Tags[]|select(.Key!="Name")))|.Tags[]|.Value|tostring)]|@tsv'
# add sort in output
aws ec2 describe-instances |jq -r '.Reservations[].Instances[] |=sortby(.LaunchTime) |
[.InstanceId, .State.Name, .PrivateIpAddress, .PublicIpAddress, (del((.Tags[]|select(.Key!="Name")))|.Tags[]|.Value|tostring)]|@tsv'
# by ssh key name
aws ec2 describe-instances --filters "Name=key-name,Values=sshkey-aws"
# by name
aws ec2 describe-instances --filters "Name=tag:Name,Values=eks*"
install ssm¶
sudo yum install -y https://s3.ap-northeast-2.amazonaws.com/amazon-ssm-ap-northeast-2/latest/linux_amd64/amazon-ssm-agent.rpm
systemctl status amazon-ssm-agent
region cmd¶
export ACCOUNT_ID=$(aws sts get-caller-identity --output text --query Account)
export AWS_DEFAULT_REGION=$(curl -s 169.254.169.254/latest/dynamic/instance-identity/document | jq -r '.region')
#export AWS_REGION=ap-northeast-1
export AZS=($(aws ec2 describe-availability-zones --query 'AvailabilityZones[].ZoneName' --output text --region $AWS_REGION))
get instance ids¶
from vpc¶
INSTANCE_IDS=($(aws ec2 describe-instances \
--filters "Name=${TAG},Values=owned" "Name=vpc-id,Values=vpc-xxx"\
|jq -r '.Reservations[].Instances[].InstanceId' ) )
filter tags¶
tag=
aws ec2 describe-instances \
--filters "Name=tag:Name,Values=cloud9-130" \
|jq -r '.Reservations[].Instances[].InstanceId'
aws ec2 describe-volumes \
--filters "Name=tag:kubernetes.io/cluster/ekscluster1,Values=owned" \
--query 'Volumes[].[Tags[?Key==`Name`].Value,State]' --output=text \
|xargs -n 2
aws ec2 describe-volumes \
--filters "Name=tag:kubernetes.io/cluster/${CLUSTER_NAME},Values=owned" \
--query 'Volumes[*].[`aws ec2 delete-volume --volume-id`, VolumeId,`#`,State,Tags[?Key==`Name`].Value | [0]]' --output=text
associate instance profile to ec2¶
aws ec2 describe-iam-instance-profile-associations
aws ec2 disassociate-iam-instance-profile
aws ec2 associate-iam-instance-profile
C9_INST_ID=$(curl 169.254.169.254/latest/meta-data/instance-id)
instance_profile_arn=$(aws ec2 describe-iam-instance-profile-associations \
--filter Name=instance-id,Values=$C9_INST_ID \
--query IamInstanceProfileAssociations[0].IamInstanceProfile.Arn \
--output text)
aws iam get-instance-profile \
--instance-profile-name ${instance_profile_arn##*/}
## add your needed role to it
aws iam add-role-to-instance-profile \
--instance-profile-name ${instance_profile_arn}
--role-name ${ROLE_NAME}
security group¶
# ensure you have security group called 'eks-shared-sg'
aws ec2 describe-security-groups --region $AWS_REGION --filter Name=vpc-id,Values=$VPC_ID --query 'SecurityGroups[*].[GroupName,GroupId]'
# if you have multi eni, do bond sg to instance manually
export SG_ID=$(aws ec2 describe-security-groups --region $AWS_REGION --filter Name=vpc-id,Values=$VPC_ID --query "SecurityGroups[?GroupName == 'eks-shared-sg'].GroupId" --output text)
func-create-sg-¶
- to create security group, you need VPC_ID (git/git-mkdocs/CLI/awscli/vpc-cmd)
refer: ../functions/func-create-sg.sh
create sg allow itself¶
count vcpu¶
aws ec2 describe-instances --region us-east-2 \
--query Reservations[].Instances[].CpuOptions.[CoreCount,ThreadsPerCore] \
--output text \
|awk 'BEGIN {sum=0} {line=$1*$2;sum=sum+line} END {print sum} '
troubleshooting¶
https://linuxconfig.org/how-to-name-label-a-partition-or-volume-on-linux
- refer: linux-cmd
- refer: e2label
KB¶
- How do I move my EC2 instance to another subnet, Availability Zone, or VPC? (LINK)
- 不能 detach primary eni
- 只能 attach 同可用区的 eni (即便是另一个 subnet )
source-destination-check-¶
- disable
Change Source / destination check
create-key-¶
- import-aws-key
create instance by chatgpt¶
KEY_NAME=aws-key
AMI_ID=$(aws ec2 describe-images \
--region us-east-2 \
--filters "Name=name,Values=Windows_Server-2019-English-Full-Base-*" \
"Name=architecture,Values=x86_64" \
"Name=root-device-type,Values=ebs" \
"Name=virtualization-type,Values=hvm" \
--query "reverse(sort_by(Images, &CreationDate))[0].ImageId" \
--output text)
aws ec2 run-instances \
--image-id ${AMI_ID} \
--instance-type t3.medium \
--key-name ${KEY_NAME} \
--subnet-id $(aws ec2 describe-subnets --filters "Name=default-for-az,Values=true" "Name=vpc-id,Values=$(aws ec2 describe-vpcs --filters "Name=isDefault,Values=true" --query "Vpcs[0].VpcId" --output text)" --query "Subnets[0].SubnetId" --output text) \
--iam-instance-profile Name=EC2DomainJoin-Instance-Profile \
--user-data '<powershell>
Import-Module "C:\Program Files\Amazon\Ec2ConfigService\Scripts\InitializeInstance.ps1"
Initialize-EC2Instance -Schedule -DomainName "corp1.aws.panlm.xyz" -DomainUserName "admin" -DomainPassword "password"
</powershell>' \
--associate-public-ip-address \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=MyInstance4}]' \
--region us-east-2
instance-connect-¶
ssh connect¶
-
from public ip
-
from private ip (need to create instance connect endpoint first)