无法通过端口443从正在运行的EC2实例与我的ECS服务通信?

huangapple go评论56阅读模式
英文:

Unable to communicate to my ECS Service from a running EC2 instance over port 443?

问题

我有一个在私有子网中运行的 ECS 服务,并且在同一 VPC 中有一个位于公有子网中的 EC2 实例。我想要能够通过端口 443 通过我的 EC2 实例与我的 ECS 服务进行通信。我的 ECS 已配置为使用特定的域名。

当我从我的 EC2 实例运行 nmap 时,我得到以下结果:

nmap -Pn <domain-name> -p443

Starting Nmap 6.40 ( http://nmap.org ) at 2023-05-24 20:35 UTC
Nmap scan report for <domain-name>
Host is up.
Other addresses for <domain-name>
PORT    STATE    SERVICE
443/tcp filtered https

Nmap done: 1 IP address (1 host up) scanned in 2.03 seconds

然而,当我使用正在运行的任务的 IP 地址进行相同操作时,我得到以下结果:

nmap -Pn <Private-IP> -p443

Starting Nmap 6.40 ( http://nmap.org ) at 2023-05-24 20:27 UTC
Host is up (0.00099s latency).
PORT    STATE  SERVICE
443/tcp closed https

Nmap done: 1 IP address (1 host up) scanned in 0.03 seconds

当我运行 ping <domain-name> 时,我得到以下结果:

6 packets transmitted, 0 received, 100% packet loss, time 5106ms

然而,我可以从我的 EC2 实例 ping 通正在运行的任务的私有 IP:

ping <private-IP>

4 packets transmitted, 4 received, 0% packet loss, time 3003ms

我已经配置了我的 ECS 服务的入站安全组以允许来自公有子网的流量,如下所示:

resource "aws_security_group_rule" "inbound_https" {
  security_group_id = aws_security_group.inbound.id
  type              = "ingress"
  from_port         = 443
  to_port           = 443
  protocol          = "tcp"
  cidr_blocks      = ["<public-IP-cidr>"]
}

resource "aws_security_group_rule" "inbound_icmp" {
  security_group_id = aws_security_group.inbound.id
  type              = "ingress"
  from_port         = 0
  to_port           = 0
  protocol          = "icmp"
  cidr_blocks      = ["<public-IP-cidr>"]
}

我可以从界面上看到它们已成功应用。

我感到困惑,因为入口被允许,但我仍然无法通过我的 EC2 实例打开端口 443 连接到我的 ECS 服务。我检查了 NACL,并且它们允许所有端口的所有流量。我错过了什么?

任务定义如下:

{
    "taskDefinitionArn": "<TASK-DEF-ARN>",
    "containerDefinitions": [
        {
            "name": "<NAME>",
            "image": "<IMAGE>",
            "cpu": 1024,
            "memory": 2048,
            "portMappings": [
                {
                    "containerPort": 80,
                    "hostPort": 80,
                    "protocol": "tcp"
                }
            ],
            "essential": true,
            "mountPoints": [],
            "volumesFrom": [],
            "logConfiguration": {
                "logDriver": "awslogs",
                "options": {
                    "awslogs-group": "awslogs-group",
                    "awslogs-region": "us-east-1",
                    "awslogs-stream-prefix": "awslogs-stream-prefix"
                }
            }
        }
    ],
    "family": "<FAMILY>",
    "taskRoleArn": "<TASK-ROLE-ARN>",
    "executionRoleArn": "<EXECUTION-ROLE-ARN>",
    "networkMode": "awsvpc",
    "revision": 63,
    "volumes": [],
    "status": "ACTIVE",
    "requiresAttributes": [
        {
            "name": "com.amazonaws.ecs.capability.logging-driver.awslogs"
        },
        {
            "name": "ecs.capability.execution-role-awslogs"
        },
        {
            "name": "com.amazonaws.ecs.capability.ecr-auth"
        },
        {
            "name": "com.amazonaws.ecs.capability.docker-remote-api.1.19"
        },
        {
            "name": "com.amazonaws.ecs.capability.task-iam-role"
        },
        {
            "name": "ecs.capability.execution-role-ecr-pull"
        },
        {
            "name": "com.amazonaws.ecs.capability.docker-remote-api.1.18"
        },
        {
            "name": "ecs.capability.task-eni"
        }
    ],
    "placementConstraints": [],
    "compatibilities": [
        "EC2",
        "FARGATE"
    ],
    "requiresCompatibilities": [
        "FARGATE"
    ],
    "cpu": "1024",
    "memory": "2048",
    "registeredAt": "2023-05-24T21:25:20.965Z",
    "registeredBy": "<REGISTERED-BY>",
    "tags": [
        {
            "key": "Environment",
            "value": "dev"
        },
        {
            "key": "Region",
            "value": "us-east-1"
        },
        {
            "key": "Service",
            "value": "My Awesome Service"
        },
        {
            "key": "Stage",
            "value": "<STAGE>"
        }
    ]
}

我甚至打开了端口 80:

resource "aws_security_group_rule" "lcp_inbound_http" {
  security_group_id = aws_security_group.lcp_inbound.id
  type              = "ingress"
  from_port         = 80
  to_port           = 80
  protocol          = "tcp"
  cidr_blocks      = ["<public-IP-cidr>"]
}

nmap 仍然显示为 filtered:

nmap -Pn -p80 <DNS>

PORT   STATE    SERVICE
80/tcp filtered http

Nmap done: 1 IP address (1 host up) scanned in 2.04 seconds
英文:

I have an ECS Service running a couple of tasks in a private subnet and an EC2 instance in the same VPC but in a public subnet. I want to be able to communicate with my ECS service via my EC2 instance over port 443. My ECS is configured to use a certain domain.

When I run nmap from my EC2 instance, I get the following :

nmap -Pn &lt;domain-name&gt; -p443

Starting Nmap 6.40 ( http://nmap.org ) at 2023-05-24 20:35 UTC
Nmap scan report for &lt;domain-name&gt;
Host is up.
Other addresses for &lt;domain-name&gt;
PORT    STATE    SERVICE
443/tcp filtered https

Nmap done: 1 IP address (1 host up) scanned in 2.03 seconds

However when I do the same using the IP address of the running task, I'm getting the following:

nmap -Pn &lt;Private-IP&gt; -p443

Starting Nmap 6.40 ( http://nmap.org ) at 2023-05-24 20:27 UTC
Host is up (0.00099s latency).
PORT    STATE  SERVICE
443/tcp closed https

Nmap done: 1 IP address (1 host up) scanned in 0.03 seconds

When I run ping &lt;domain-name&gt; I am getting the following:

6 packets transmitted, 0 received, 100% packet loss, time 5106ms

However I am able to ping the private IP of the running task from my EC2 instance.

ping &lt;private-IP&gt;

4 packets transmitted, 4 received, 0% packet loss, time 3003ms

I have configured the inbound security group of my ECS Service to allow traffic from the public subnet like this.

resource &quot;aws_security_group_rule&quot; &quot;inbound_https&quot; {
  security_group_id = aws_security_group.inbound.id
  type = &quot;ingress&quot;
  from_port = 443
  to_port = 443
  protocol = &quot;tcp&quot;
  cidr_blocks = [&quot;&lt;public-IP-cidr&gt;&quot;]
}

resource &quot;aws_security_group_rule&quot; &quot;inbound_icmp&quot; {
  security_group_id = aws_security_group.inbound.id
  type = &quot;ingress&quot;
  from_port = 0
  to_port = 0
  protocol = &quot;icmp&quot;
  cidr_blocks = [&quot;&lt;public-IP-cidr&gt;&quot;]
}

And I can see from the UI that they were applied successfully.

I am confused because the ingress is allowed, yet I am still not able to open port 443 into my ECS service via my EC2 instance. I checked the NACL and they allow all traffic from all ports. What am I missing?

The Task definition is as follows:

{
    &quot;taskDefinitionArn&quot;: &quot;&lt;TASK-DEF-ARN&gt;&quot;,
    &quot;containerDefinitions&quot;: [
        {
            &quot;name&quot;: &quot;&lt;NAME&gt;&quot;,
            &quot;image&quot;: &quot;&lt;IMAGE&gt;&quot;,
            &quot;cpu&quot;: 1024,
            &quot;memory&quot;: 2048,
            &quot;portMappings&quot;: [
                {
                    &quot;containerPort&quot;: 80,
                    &quot;hostPort&quot;: 80,
                    &quot;protocol&quot;: &quot;tcp&quot;
                }
            ],
            &quot;essential&quot;: true,
            &quot;mountPoints&quot;: [],
            &quot;volumesFrom&quot;: [],
            &quot;logConfiguration&quot;: {
                &quot;logDriver&quot;: &quot;awslogs&quot;,
                &quot;options&quot;: {
                    &quot;awslogs-group&quot;: &quot;awslogs-group&quot;,
                    &quot;awslogs-region&quot;: &quot;us-east-1&quot;,
                    &quot;awslogs-stream-prefix&quot;: &quot;awslogs-stream-prefix&quot;
                }
            }
        }
    ],
    &quot;family&quot;: &quot;&lt;FAMILY&gt;&quot;,
    &quot;taskRoleArn&quot;: &quot;&lt;TASK-ROLE-ARN&gt;&quot;,
    &quot;executionRoleArn&quot;: &quot;&lt;EXECUTION-ROLE-ARN&gt;&quot;,
    &quot;networkMode&quot;: &quot;awsvpc&quot;,
    &quot;revision&quot;: 63,
    &quot;volumes&quot;: [],
    &quot;status&quot;: &quot;ACTIVE&quot;,
    &quot;requiresAttributes&quot;: [
        {
            &quot;name&quot;: &quot;com.amazonaws.ecs.capability.logging-driver.awslogs&quot;
        },
        {
            &quot;name&quot;: &quot;ecs.capability.execution-role-awslogs&quot;
        },
        {
            &quot;name&quot;: &quot;com.amazonaws.ecs.capability.ecr-auth&quot;
        },
        {
            &quot;name&quot;: &quot;com.amazonaws.ecs.capability.docker-remote-api.1.19&quot;
        },
        {
            &quot;name&quot;: &quot;com.amazonaws.ecs.capability.task-iam-role&quot;
        },
        {
            &quot;name&quot;: &quot;ecs.capability.execution-role-ecr-pull&quot;
        },
        {
            &quot;name&quot;: &quot;com.amazonaws.ecs.capability.docker-remote-api.1.18&quot;
        },
        {
            &quot;name&quot;: &quot;ecs.capability.task-eni&quot;
        }
    ],
    &quot;placementConstraints&quot;: [],
    &quot;compatibilities&quot;: [
        &quot;EC2&quot;,
        &quot;FARGATE&quot;
    ],
    &quot;requiresCompatibilities&quot;: [
        &quot;FARGATE&quot;
    ],
    &quot;cpu&quot;: &quot;1024&quot;,
    &quot;memory&quot;: &quot;2048&quot;,
    &quot;registeredAt&quot;: &quot;2023-05-24T21:25:20.965Z&quot;,
    &quot;registeredBy&quot;: &quot;&lt;REGISTERED-BY&gt;&quot;,
    &quot;tags&quot;: [
        {
            &quot;key&quot;: &quot;Environment&quot;,
            &quot;value&quot;: &quot;dev&quot;
        },
        {
            &quot;key&quot;: &quot;Region&quot;,
            &quot;value&quot;: &quot;us-east-1&quot;
        },
        {
            &quot;key&quot;: &quot;Service&quot;,
            &quot;value&quot;: &quot;My Awesome Service&quot;
        },
        {
            &quot;key&quot;: &quot;Stage&quot;,
            &quot;value&quot;: &quot;&lt;STAGE&gt;&quot;
        }
    ]
}

I even opened up port 80

resource &quot;aws_security_group_rule&quot; &quot;lcp_inbound_http&quot; {
  security_group_id = aws_security_group.lcp_inbound.id
  type = &quot;ingress&quot;
  from_port = 80
  to_port = 80
  protocol = &quot;tcp&quot;
  cidr_blocks = [&quot;&lt;public-IP-cidr&gt;&quot;]
}

And the nmap is still filtered.

nmap -Pn -p80 &lt;DNS&gt;

PORT   STATE    SERVICE
80/tcp filtered http

Nmap done: 1 IP address (1 host up) scanned in 2.04 seconds

答案1

得分: 0

我不得不在入站规则中使用 <public-IP-Address>,那就可以了。

英文:

I had to use the &lt;public-IP-Address&gt; in the inbound rule and that worked.

huangapple
  • 本文由 发表于 2023年5月25日 04:46:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定