我可以在aws安全组中添加dns名称吗[已关闭]

问题描述 投票:0回答:6

我必须将我的动态IP(每次都会改变)连接到AWS EC2机器。
为此,我将我的公共IP映射到域名(xyz.com),现在我尝试将其添加到安全组。
但AWS安全组不允许添加DNS名称。 这是正确的流程吗?如果不正确,请建议我。

amazon-web-services amazon-ec2 aws-security-group
6个回答
18
投票

安全组和 ACL 无法解析 DNS 主机名。

您可以使用 AWS CLI 编写 IP 动态地址更新脚本:

aws ec2 授权安全组入口 --group-id --协议 tcp --端口 22 --cidr /24

http://docs.aws.amazon.com/cli/latest/userguide/cli-ec2-sg.html


9
投票

AWS 安全规则仅允许您可以使用 AWS CLI 更新的 IP 范围(称为 CIDRs)。但是,您不能简单地更新现有规则的 CIDR,您需要:

  1. 删除旧规则:
    aws ec2 revoke-security-group-ingress ...
  2. 创建新规则:
    aws ec2 authorize-security-group-ingress ...

示例

我发现该脚本的某种形式对于封装必要的步骤很有用:

#!/bin/bash

# == Script Config ===================

# The rule description is used to determine the rule that should be updated.
RULE_DESCRIPTION=My-Rule-Description
SECURITY_GROUP_NAME=My-Security-Group-Name

# ====================================

OLD_CIDR_IP=`aws ec2 describe-security-groups --query "SecurityGroups[?GroupName=='"$SECURITY_GROUP_NAME"'].IpPermissions[*].IpRanges[?Description=='"$RULE_DESCRIPTION"'].CidrIp" --output text`
NEW_IP=`curl -s http://checkip.amazonaws.com`
NEW_CIDR_IP=$NEW_IP'/32'

# If IP has changed and the old IP could be obtained, remove the old rule
if [[ $OLD_CIDR_IP != "" ]] && [[ $OLD_CIDR_IP != $NEW_CIDR_IP ]]; then
    aws ec2 revoke-security-group-ingress --group-name $SECURITY_GROUP_NAME --protocol tcp --port 8080 --cidr $OLD_CIDR_IP
fi

# If the IP has changed and the new IP could be obtained, create a new rule
if [[ $NEW_IP != "" ]] && [[ $OLD_CIDR_IP != $NEW_CIDR_IP ]]; then
   aws ec2 authorize-security-group-ingress --group-name $SECURITY_GROUP_NAME --ip-permissions '[{"IpProtocol": "tcp", "FromPort": 8080, "ToPort": 8080, "IpRanges": [{"CidrIp": "'$NEW_CIDR_IP'", "Description": "'$RULE_DESCRIPTION'"}]}]'
fi

解释

此方法使用以下 3 个 AWS CLI 命令,取自上面的示例,并删除了 bash 脚本。

1)根据规则的描述获取特定安全组中规则的CIDR IP。该命令使用 query 参数中的

JMESPath
来仅返回我们想要的数据:

aws ec2 describe-security-groups --query "SecurityGroups[?GroupName=='MY_SECURITY_GROUP_NAME'].IpPermissions[*].IpRanges[?Description=='MY_RULE_DESCRIPTION'].CidrIp" --output text

2) 删除旧 CIDR 的规则(即使规则不存在也会成功):

aws ec2 revoke-security-group-ingress --group-name MY_SECURITY_GROUP_NAME --protocol tcp --port 80 --cidr 0.0.0.0/32

3)为新的 CIDR 添加规则(当规则已存在时失败):

aws ec2 authorize-security-group-ingress --group-name MY_SECURITY_GROUP_NAME --ip-permissions '[{"IpProtocol": "tcp", "FromPort": 8080, "ToPort": 8080, "IpRanges": [{"CidrIp": "1.1.1.1/32", "Description": "MY_RULE_DESCRIPTION"}]}]'

5
投票

我使用这个小 bash 脚本从我当前的地址在防火墙上戳了一个洞:

#!/bin/sh
AWS_IP=$(curl http://checkip.amazonaws.com)
aws ec2 authorize-security-group-imgress --group-name my-security-group \
         --protocol tcp --port 22 \
         --cidr $AWS_IP/32

但是,这会导致安全组充满来自随机 IP 地址的瑞士奶酪漏洞,因此您随后需要询问如何不让安全组拥有不再属于您的临时地址。解决该问题的一种方法是设置一个具有(相对)稳定的 IP 地址端点的 VPN,然后仅允许该单个地址通过安全组。


2
投票

我为动态 ip 创建一个安全组,每次运行脚本时都会删除存储在文件中的 ip。

这是我针对 Windows 的解决方案。

SETLOCAL
@echo off
SET mypath=%~dp0
set PATH=%PATH%;"C:\Program Files\Amazon\AWSCLI\";"C:\Program Files (x86)\PuTTY\";"C:\MyApps\gnuwin32\bin"
set GROUPID=  PUT YOUR DYNAMIC SECURITY GROUP ID HERE
rem aws ec2 create-security-group --group-name dynamic_ips --vpc-id vpc-81a519e5 --description "Dynamic Ip Address"
set /p MYIP=<%mypath%\MYIP_NODELETE.txt
aws ec2 revoke-security-group-ingress --group-id %GROUPID% --protocol tcp --port 0-65535 --cidr %MYIP%/24
wget -qO %mypath%\MYIP_NODELETE.txt http://ipinfo.io/ip
set /p MYIP=<%mypath%\MYIP_NODELETE.txt
aws ec2 authorize-security-group-ingress --group-id %GROUPID% --protocol tcp --port 0-65535 --cidr %MYIP%/24
rem cat %mypath%\MYIP_NODELETE.txt
pause

2
投票

为什么不在公共 IP 上创建 Bastian 主机,然后将其用作跳转盒?


0
投票

你无法按照你想要的方式连接动态ip;每次您的 IP 更改时,如果您想允许它通过您的安全组,您需要将设置更改为新的 IP。

您可以编写一个小脚本,将其制作为桌面上的图标,但是使用 AWS API 重新允许您当前的 IP,以便在更改时更轻松。

© www.soinside.com 2019 - 2024. All rights reserved.