From 2e561bfb299501a8632a032c6aa67601f3c4a67c Mon Sep 17 00:00:00 2001 From: Timothy Middelkoop Date: Thu, 11 Nov 2021 21:58:42 -0600 Subject: [PATCH] AWS VM tools --- Build.md | 4 ++ scripts/aws-create.sh | 75 ++++++++++++++++++++++++++++++++++++++ scripts/aws-delete.sh | 13 +++++++ scripts/aws-vpc-create.sh | 77 +++++++++++++++++++++++++++++++++++++++ scripts/aws-vpc-delete.sh | 43 ++++++++++++++++++++++ 5 files changed, 212 insertions(+) create mode 100755 scripts/aws-create.sh create mode 100755 scripts/aws-delete.sh create mode 100755 scripts/aws-vpc-create.sh create mode 100755 scripts/aws-vpc-delete.sh diff --git a/Build.md b/Build.md index 852be0a..3942f65 100644 --- a/Build.md +++ b/Build.md @@ -39,3 +39,7 @@ export GOOGLE_CLOUD_PROJECT=just-armor-301114 export DEVSHELL_PROJECT_ID=$GOOGLE_CLOUD_PROJECT gcloud config set project $GOOGLE_CLOUD_PROJECT ``` + +## AWS + +Expect that `aws` is installed locally. A ssh-key named 'learner' is required to access the account. diff --git a/scripts/aws-create.sh b/scripts/aws-create.sh new file mode 100755 index 0000000..43e7941 --- /dev/null +++ b/scripts/aws-create.sh @@ -0,0 +1,75 @@ +#!/bin/bash + +# Options +BRANCH="${1:-aws-dev}" # checkout branch $1 + +# Static Config - update aws-*.sh files +NAME=learner +VM=essentials +PROJECT=CLASS-Essentials +GITHUB=github.internet2.edu +REPO="git@${GITHUB}:CLASS/${PROJECT}.git" + +echo "=== aws-create.sh $PROJECT $BRANCH" + +VPC=$(aws ec2 describe-vpcs --filter "Name=tag:Name,Values=${VM}" --query "Vpcs[].VpcId" --output text) +SUBNET=$(aws ec2 describe-subnets --filter "Name=tag:Name,Values=${VM}" --query "Subnets[].SubnetId" --output text) +SG=$(aws ec2 describe-security-groups --filters "Name=group-name,Values=${VM}" --query "SecurityGroups[].GroupId" --output text) + +echo "+++ networking: $VM $VPC $SUBNET $SG" +if [ -z "${VPC}" -o -z "${SUBNET}" -o -z "${SG}" ] ; then + echo "--- '${VM}' networking does not exist. Use 'aws-vpc-create.sh' to create" + exit 1 +fi + +IP=$(aws ec2 describe-instances --filters 'Name=instance-state-name,Values=running' 'Name=tag:Name,Values=essentials' --query "Reservations[*].Instances[*].PublicIpAddress" --output text --no-cli-pager) +if [ -z "${IP}" ] ; then + echo "+++ creating VM" + aws ec2 run-instances \ + --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=$VM}]" \ + --image-id resolve:ssm:/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2 \ + --instance-type m6i.large \ + --subnet-id $SUBNET \ + --security-group-ids $SG \ + --key-name $NAME \ + --no-cli-pager +fi + +while [ -z ${IP:=$(aws ec2 describe-instances --filters 'Name=instance-state-name,Values=running' "Name=tag:Name,Values=${VM}" --query 'Reservations[*].Instances[*].PublicIpAddress' --output text --no-cli-pager)} ] ; do + echo "+++ waiting for IP" + sleep 1 +done + +echo "+++ wait for boot and cloud-init ${VM} ${IP}" +ssh-keygen -R $IP +while ! ssh ec2-user@$IP sudo cloud-init status --wait ; do + sleep 1 +done + +echo "+++ configuring VM" + +ssh ec2-user@$IP -A < .ssh/known_hosts +git config --global color.ui auto +git config --global push.default simple +git config --global pull.ff only +git config --global user.name "$(git config user.name)" +git config --global user.email "$(git config user.name)" +git clone --branch $BRANCH $REPO +EOF + +echo "+++ configure ~/.ssh/$VM.config" +cat > ~/.ssh/$VM.config <