Advertisement
Javi

AWS: CLI for starting/stopping instance

Mar 12th, 2019
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. #/bin/sh
  2.  
  3. export AWS_PROFILE=<profile>
  4. MY_IP=$(curl -s ifconfig.co)
  5. INSTANCE_ID=<instance>
  6. SG=<sg>
  7.  
  8. if [[ $1 = "start" ]]; then
  9. echo Starting $INSTANCE_ID.
  10. aws ec2 start-instances --instance-ids $INSTANCE_ID > /dev/null
  11. aws ec2 wait instance-running --instance-ids $INSTANCE_ID
  12. echo Instance running.
  13. aws ec2 authorize-security-group-ingress --group-id $SG --port 8080 --cidr $MY_IP/32 --protocol tcp > /dev/null
  14. if [ $? -eq 0 ]; then
  15. echo New security group rule added to allow traffic from $MY_IP.
  16. else
  17. echo Security group already allows traffic from $MY_IP.
  18. fi
  19. sleep 15
  20. WW=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID --query "Reservations[0].Instances[0].PublicIpAddress" --output text)
  21. echo Instance IP: $WW
  22. start http://$WW:8080
  23. elif [[ $1 = "stop" ]]; then
  24. aws ec2 stop-instances --instance-ids $INSTANCE_ID > /dev/null
  25. echo Stopping instance.
  26. else
  27. STATUS=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID --query "Reservations[0].Instances[0].State.Name" --output text)
  28. echo Instance $INSTANCE_ID is $STATUS.
  29. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement