Skip to content

Instantly share code, notes, and snippets.

@roeniss
Last active March 4, 2023 17:45
Show Gist options
  • Save roeniss/5aaa55b4c4ac18fabfd7a2b45ef287a7 to your computer and use it in GitHub Desktop.
Save roeniss/5aaa55b4c4ac18fabfd7a2b45ef287a7 to your computer and use it in GitHub Desktop.
Instant launching ec2 using awscli
# Reference gist: https://gist.github.com/gordonmurray/4e6541f689205db3eb9bddc17a95fb1c
export AMI_ID="ami-033a6a056910d1137" # 2022.03 latest amazon linux 2 AMI
export AWS_PROFILE="roeniss"
export AWS_REGION="ap-northeast-2"
export INSTANCE_TYPE="t2.medium"
export KEY_PAIR_NAME="onetime_keypair_$(uuidgen)"
export SECURITY_GROUP_NAME="onetime_security_group_$(uuidgen)"
# Create ec2 with keypair then echo public DNS
aws ec2 create-key-pair --profile "$AWS_PROFILE" --region "$AWS_REGION" --key-name "$KEY_PAIR_NAME" --query 'KeyMaterial' --output text > "$KEY_PAIR_NAME.pem" &&\
chmod 400 "$KEY_PAIR_NAME.pem" &&\
export SECURITY_GROUP_ID=$(aws ec2 create-security-group --profile "$AWS_PROFILE" --region "$AWS_REGION" --group-name "$SECURITY_GROUP_NAME" --description "onetime security group" --query "GroupId" --output text) &&\
aws ec2 authorize-security-group-ingress --profile "$AWS_PROFILE" --region "$AWS_REGION" --group-name "$SECURITY_GROUP_NAME" --to-port 22 --from-port 22 --ip-protocol tcp --cidr-ip "0.0.0.0/0" &&\
export INSTANCE_ID=$(aws ec2 run-instances --profile "$AWS_PROFILE" --region "$AWS_REGION" --image-id "$AMI_ID" --associate-public-ip-address --count 1 --instance-type "$INSTANCE_TYPE" --security-group-ids ${SECURITY_GROUP_ID} --key-name "$KEY_PAIR_NAME" --query 'Instances[0].InstanceId' --output text) &&\
export PUBLIC_DNS_NAME=$(aws ec2 describe-instances --profile "$AWS_PROFILE" --region "$AWS_REGION" --filter "Name=instance-id,Values=$INSTANCE_ID" --query 'Reservations[0].Instances[0].PublicDnsName' --output text) &&\
echo "INSTANCE_ID: $INSTANCE_ID\nSECURITY_GROUP_ID: $SECURITY_GROUP_ID\nKEY_PAIR_NAME: $KEY_PAIR_NAME.pem" &&\
echo "INSTANCE_ID: $INSTANCE_ID\nSECURITY_GROUP_ID: $SECURITY_GROUP_ID\nKEY_PAIR_NAME: $KEY_PAIR_NAME.pem" | pbcopy
# Access the instance
sleep 60 && ssh -i "$KEY_PAIR_NAME.pem" "ec2-user@$PUBLIC_DNS_NAME"
# Delete ec2 and keypair
aws ec2 terminate-instances --profile "$AWS_PROFILE" --region "$AWS_REGION" --instance-ids "$INSTANCE_ID" &&\
aws ec2 delete-key-pair --profile "$AWS_PROFILE" --region "$AWS_REGION" --key-name "$KEY_PAIR_NAME" &&\
trash "$KEY_PAIR_NAME.pem" || rm "$KEY_PAIR_NAME.pem" &&\
sleep 60 &&\
aws ec2 delete-security-group --profile "$AWS_PROFILE" --region "$AWS_REGION" --group-name "$SECURITY_GROUP_NAME"
sudo yum update -y &&\
sudo yum upgrade -y &&\
sudo yum install -y git tree htop
@roeniss
Copy link
Author

roeniss commented Apr 2, 2022

trouble shooting

unix_listener too long for Unix domain socket

https://gist.github.com/andyvanee/bcf95b1044b80e72b4a42933549a079b

LC_ALL: cannot change locale (en_US.UTF-8)

https://www.lesstif.com/lpt/linux-warning-setlocale-lc_all-cannot-change-locale-106856458.html

yum search locale en
sudo yum install glibc-langpack-en.x86_64

@roeniss
Copy link
Author

roeniss commented Dec 11, 2022

ami-0c70a556e1325c8d0 : ubuntu 1804

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment