Skip to content
This repository was archived by the owner on Dec 12, 2025. It is now read-only.

Commit

Permalink
AWS VM tools
Browse files Browse the repository at this point in the history
  • Loading branch information
tmiddelkoop committed Nov 12, 2021
1 parent 2c1a0cd commit 2e561bf
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Build.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
75 changes: 75 additions & 0 deletions scripts/aws-create.sh
Original file line number Diff line number Diff line change
@@ -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 <<EOF
sudo yum install -y git python3-pip python3-venv bash-completion
ssh-keyscan ${GITHUB} > .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 <<EOF
Host essentials
HostName $IP
CheckHostIP=no
ForwardAgent=yes
User=ec2-user
EOF

echo "+++ starting Jypter"
ssh ec2-user@$IP -t -L 8080:localhost:8080 -L 8081:localhost:8081 "cd $PROJECT ; ./scripts/jupyter-lab.sh"

echo "+++ ssh command"
echo "ssh ec2-user@$IP"
13 changes: 13 additions & 0 deletions scripts/aws-delete.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

## Settings copied from aws-create.sh
VM=essentials

echo "=== aws-delete.sh $VM"

INSTANCE=$(aws ec2 describe-instances --filters 'Name=instance-state-name,Values=running' 'Name=tag:Name,Values=essentials' --query "Reservations[*].Instances[*].InstanceId" --output text --no-cli-pager)

if [ -n "${INSTANCE}" ] ; then
echo "+++ terminating $VM $INSTANCE"
aws ec2 terminate-instances --instance-id $INSTANCE --no-cli-pager
fi
77 changes: 77 additions & 0 deletions scripts/aws-vpc-create.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/bash

## Settings copied from aws-create.sh
VM=essentials

## Create a simple VPC.
echo "=== aws-vpc-create.sh"

echo "+++ creating '$VM' networking allowing internal traffic and external ssh and ICMP access"
aws ec2 create-vpc \
--tag-specifications "ResourceType=vpc,Tags=[{Key=Name,Value=$VM}]" \
--cidr-block 10.0.0.0/16 \
--no-cli-pager
VPC=$(aws ec2 describe-vpcs --filter "Name=tag:Name,Values=${VM}" --query "Vpcs[].VpcId" --output text)
echo "+++ created VPC $VPC"

aws ec2 create-subnet \
--tag-specifications "ResourceType=subnet,Tags=[{Key=Name,Value=$VM}]" \
--vpc-id $VPC \
--cidr-block 10.0.0.0/16 \
--no-cli-pager
SUBNET=$(aws ec2 describe-subnets --filter "Name=tag:Name,Values=${VM}" --query "Subnets[].SubnetId" --output text)
echo "+++ created subnet $SUBNET"

aws ec2 create-security-group \
--group-name $VM \
--description $VM \
--vpc-id $VPC \
--no-cli-pager
SG=$(aws ec2 describe-security-groups --filters "Name=group-name,Values=${VM}" --query "SecurityGroups[].GroupId" --output text)
echo "+++ created security group $SG"

# SSH, ICMP
aws ec2 authorize-security-group-ingress \
--group-id $SG \
--protocol=tcp --port=22 --cidr 0.0.0.0/0 \
--no-cli-pager
aws ec2 authorize-security-group-ingress \
--group-id $SG \
--protocol=icmp --port=-1 --cidr 0.0.0.0/0 \
--no-cli-pager

aws ec2 create-internet-gateway \
--tag-specifications "ResourceType=internet-gateway,Tags=[{Key=Name,Value=$VM}]" \
--no-cli-pager
IG=$(aws ec2 describe-internet-gateways --filters "Name=tag:Name,Values=${VM}" --query "InternetGateways[].InternetGatewayId" --output text)
echo "+++ created internet gateway $IG"

aws ec2 attach-internet-gateway \
--internet-gateway-id $IG \
--vpc-id $VPC \
--no-cli-pager

aws ec2 create-route-table \
--tag-specifications "ResourceType=route-table,Tags=[{Key=Name,Value=$VM}]" \
--vpc-id $VPC \
--no-cli-pager
RT=$(aws ec2 describe-route-tables --filters "Name=tag:Name,Values=${VM}" --query "RouteTables[].RouteTableId" --output text)
echo "+++ created route table $RT"

aws ec2 create-route \
--route-table-id $RT \
--gateway-id $IG \
--destination-cidr-block 0.0.0.0/0 \
--no-cli-pager

aws ec2 associate-route-table \
--route-table-id $RT \
--subnet-id $SUBNET \
--no-cli-pager

aws ec2 modify-subnet-attribute \
--subnet-id $SUBNET \
--map-public-ip-on-launch \
--no-cli-pager

echo "+++ networking: $VM,$VPC,$SUBNET,$SG,$IG,$RT."
43 changes: 43 additions & 0 deletions scripts/aws-vpc-delete.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

# References:
# * https://docs.aws.amazon.com/vpc/latest/userguide/vpc-subnets-commands-example.html

# Settings in aws-create.sh
VM=essentials

echo "=== aws-vpc-delete.sh $VM"

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)
IG=$(aws ec2 describe-internet-gateways --filters "Name=tag:Name,Values=${VM}" --query "InternetGateways[].InternetGatewayId" --output text)
RT=$(aws ec2 describe-route-tables --filters "Name=tag:Name,Values=${VM}" --query "RouteTables[].RouteTableId" --output text)
echo "+++ networking: $VM,$VPC,$SUBNET,$SG,$IG,$RT."

if [ -n "${SG}" ] ; then
echo "+++ terminating $VM $SG"
aws ec2 delete-security-group --group-id $SG --no-cli-pager
fi

if [ -n "${SUBNET}" ] ; then
echo "+++ terminating $VM $SUBNET"
aws ec2 delete-subnet --subnet-id $SUBNET --no-cli-pager
fi

if [ -n "${RT}" ] ; then
echo "+++ terminating $VM $RT"
aws ec2 delete-route-table --route-table-id $RT --no-cli-pager
fi

if [ -n "${IG}" ] ; then
echo "+++ terminating $VM $IG"
aws ec2 detach-internet-gateway --internet-gateway-id $IG --vpc-id $VPC --no-cli-pager
aws ec2 delete-internet-gateway --internet-gateway-id $IG --no-cli-pager
fi

if [ -n "${VPC}" ] ; then
echo "+++ terminating $VM $VPC"
aws ec2 delete-vpc --vpc-id $VPC --no-cli-pager
fi

0 comments on commit 2e561bf

Please sign in to comment.