Last active
March 4, 2023 17:45
-
-
Save roeniss/5aaa55b4c4ac18fabfd7a2b45ef287a7 to your computer and use it in GitHub Desktop.
Instant launching ec2 using awscli
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo yum update -y &&\ | |
sudo yum upgrade -y &&\ | |
sudo yum install -y git tree htop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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