Skip to content

Instantly share code, notes, and snippets.

@m99coder
Last active September 3, 2022 11:05
Show Gist options
  • Save m99coder/c296cdb5a244ab9bd3dfcea8f41e33a0 to your computer and use it in GitHub Desktop.
Save m99coder/c296cdb5a244ab9bd3dfcea8f41e33a0 to your computer and use it in GitHub Desktop.
Run EC2 instance with SSH access and install Docker with Docker Compose as CLI plugin

Run EC2 instance and install Docker

AWS EC2

How to install CLI

brew install awscli

How to login

aws configure --profile acg
export AWS_PROFILE=acg

~/.aws/credentials

[acg]
aws_access_key_id = XXXX
aws_secret_access_key = XXXXXXXXXXXX

~/.aws/config

[profile acg]
region = us-east-1

How to create an EC2 keypair

aws ec2 create-key-pair --key-name acg --query "KeyMaterial" --output text > acg.pem
chmod 400 acg.pem

#aws ec2 delete-key-pair --key-name acg
#sudo rm acg.pem

How to run an EC2 instance

# Images:
#   - ami-05fa00d4c63e32376 (64-bit (x86))
#   - ami-05f3141013eebdc12 (64-bit (Arm))
# Instance Types:
#   - t1.micro
#   - t2.micro

EC2_INSTANCE_ID=$(aws ec2 run-instances \
  --image-id ami-05fa00d4c63e32376 \
  --instance-type t2.micro \
  --key-name acg \
  --private-dns-name-options HostnameType=ip-name,EnableResourceNameDnsARecord=true,EnableResourceNameDnsAAAARecord=false \
  | jq -r '.Instances[0].InstanceId')
echo $EC2_INSTANCE_ID

# wait until status is "ok"
watch aws ec2 describe-instance-status \
  --instance-id $EC2_INSTANCE_ID \
  --query "InstanceStatuses[0].InstanceStatus.Status" \
  --output text

# authorize ingress for port 22 (SSH)
EC2_INSTANCE_SG_ID=$(aws ec2 describe-instances \
  --instance-ids $EC2_INSTANCE_ID \
  --query "Reservations[0].Instances[0].SecurityGroups[?GroupName=='default'].GroupId" \
  --output text)
echo $EC2_INSTANCE_SG_ID

aws ec2 authorize-security-group-ingress \
  --group-id $EC2_INSTANCE_SG_ID \
  --protocol tcp \
  --port 22 \
  --cidr 0.0.0.0/0

# connect via SSH
EC2_INSTANCE_PUBLIC_DNS_NAME=$(aws ec2 describe-instances \
  --instance-ids $EC2_INSTANCE_ID \
  --query "Reservations[0].Instances[0].PublicDnsName" \
  --output text)
echo $EC2_INSTANCE_PUBLIC_DNS_NAME

ssh -i "acg.pem" "ec2-user@$EC2_INSTANCE_PUBLIC_DNS_NAME"

# colorize prompt
export PS1="\[\e[0;32m\][\u@\h \W]$\[\e[m\] "

# change locale to English
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
#locale

# set some aliases
alias ll='ls -la --color=auto'

#aws ec2 delete-security-group --group-id $EC2_INSTANCE_SG_ID
#aws ec2 terminate-instances --instance-ids $EC2_INSTANCE_ID

Docker

How to install Docker

# update system
sudo yum update -y

# install docker
sudo yum install docker -y

# add ec2-user to docker group
sudo usermod -a -G docker ec2-user
id ec2-user
newgrp docker

# install docker compose
DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
DOCKER_VERSION=v2.10.2

mkdir -p $DOCKER_CONFIG/cli-plugins
curl -SL https://github.com/docker/compose/releases/download/$DOCKER_VERSION/docker-compose-$(uname -s)-$(uname -m) -o $DOCKER_CONFIG/cli-plugins/docker-compose

chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose

# enable and start service
sudo systemctl enable docker.service
sudo systemctl start docker.service

# check if everything is setup correctly
docker version

#sudo systemctl status docker.service

Shell

How to install ZSH + Oh My Zsh

# install zsh, chsh and git
sudo yum install zsh -y
sudo yum install util-linux-user -y
sudo yum install git -y

# install Oh My Zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

# install useful plugins
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git \
  ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
git clone https://github.com/zsh-users/zsh-autosuggestions \
  ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
  
# add `zsh-autosuggestions` and `zsh-syntax-highlighting` to `plugins` list in `~/.zshrc`
#plugins=(git docker zsh-autosuggestions zsh-syntax-highlighting)

# optionally, change shell to zsh
# log out and log in again to make if effective
sudo chsh -s $(which zsh) $(whoami)

# manually update oh-my-zsh
omz update
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment