Skip to content

Instantly share code, notes, and snippets.

@tuxpower
Last active August 29, 2018 13:20
Show Gist options
  • Save tuxpower/757a96b12f49ac6bfd42272dff00de29 to your computer and use it in GitHub Desktop.
Save tuxpower/757a96b12f49ac6bfd42272dff00de29 to your computer and use it in GitHub Desktop.

Tired of updating your IP address manually in the security group eveytime you need to access a EC2 instance? Look no further :)

TL;DR

$ mkdir ~/.aws/cli

$ cat >> ~/.aws/cli/alias

update-ssh-ip =
  !f() {
    MYIP=$(curl -s -4 ifconfig.co)
    
    cat <<EOF > /tmp/aws-update-inbound-ssh-traffic.json
    {
        "IpPermissions": [
            {
                "PrefixListIds": [], 
                "FromPort": 22, 
                "IpRanges": [
                    {
                        "CidrIp": "$MYIP/32"
                    }
                ], 
                "ToPort": 22, 
                "IpProtocol": "tcp", 
                "UserIdGroupPairs": [], 
                "Ipv6Ranges": []
            }
        ], 
        "GroupName": "default", 
        "GroupId": "sg-4815ef2c"
    }
    EOF

    aws ec2 authorize-security-group-ingress \
      --group-id sg-4815ef2c \
      --cli-input-json file:///tmp/aws-update-inbound-ssh-traffic.json
  }; f

Finally just run:

$ aws update-ssh-ip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment