Last active
August 1, 2019 09:13
-
-
Save sayadi/d37ce48b283e48df89530b5165255d66 to your computer and use it in GitHub Desktop.
How to SSH to an AWS EC2 Instance Running Linux from a CircleCI Build
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
# 1- Install AWS CLI | |
curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip" | |
unzip awscli-bundle.zip | |
./awscli-bundle/install -b ~/bin/aws | |
# 2- Get the public IP of the current CircleCI runner | |
PUBLIC_IP=$(curl ipinfo.io/ip) | |
# 3- Get AWS Region | |
# TODO Don't forget to replcae with your own Region | |
AWS_REGION=us-east-2 | |
# 4- Get SG ID | |
# TODO Don't forget to replace with your own SG ID | |
SG_ID=sg-XXXXXXXX | |
# 5- Add an ingress rule to the security group | |
~/bin/aws ec2 authorize-security-group-ingress --region $AWS_REGION --group-id $SG_ID \ | |
--protocol tcp --port 22 --cidr $PUBLIC_IP/24 | |
# 6- Give the ingress rule some time to propogate | |
sleep 5 | |
# 7- SSH to the server to deploy | |
# TODO Change to your username | |
EC2_USERNAME=ubuntu | |
# TODO Change to your server's URL or public IP# Remember that this matches the hostname you entered# in the Project Settings on Circle | |
EC2_PUBLIC_DNS=application-server.example.com | |
ssh -o StrictHostKeyChecking=no $EC2_USERNAME@$EC2_PUBLIC_DNS \ | |
# other commands \ | |
# TODO Perform steps to deploy | |
# . \ | |
# . \ | |
# . | |
# 8- Remove the ingress rule | |
~/bin/aws ec2 revoke-security-group-ingress --region $AWS_REGION --group-id $SG_ID \ | |
--protocol tcp --port 22 --cidr $PUBLIC_IP/24 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment