게시 2026-11-24Published 2026-11-24Networking읽는 시간 9분9 min read

Transit Gateway 보안 그룹 참조는 Pod 보안 그룹에서도 동작합니다Transit Gateway security group referencing works with security groups for Pods

AWS Transit Gateway(TGW)의 보안 그룹 참조 기능을 켜면 TGW로 연결된 다른 VPC의 보안 그룹을 인바운드 규칙의 출발지로 쓸 수 있습니다. 그런데 Amazon EKS의 Pod 보안 그룹(Security Groups for Pods)처럼 Pod 전용 branch ENI에 붙은 보안 그룹도 이렇게 참조되는지는 문서에 나와 있지 않습니다. 서울 리전에서 직접 테스트해 보니 strict 모드에서는 Pod 보안 그룹 하나만 출발지로 적은 규칙으로 TGW 너머의 접속이 허용됐습니다. standard 모드에 AWS_VPC_K8S_CNI_EXTERNALSNAT=false를 쓰면 트래픽이 노드 IP로 바뀌어 노드 보안 그룹으로 판정됐습니다.With security group referencing turned on in AWS Transit Gateway (TGW), an inbound rule can name a security group from another VPC attached to the same TGW as its source. The documentation does not say whether this also works for a security group attached to a Pod's own branch ENI, which is how Amazon EKS security groups for Pods work. I tested it in the Seoul Region. In strict mode, a rule whose only source was the Pod security group allowed the connection across the TGW. In standard mode with AWS_VPC_K8S_CNI_EXTERNALSNAT=false, the traffic left with the node IP and was judged by the node security group instead.

#Transit Gateway#Security group referencing#Amazon EKS#Security groups for Pods#Amazon VPC CNI#VPC Flow Logs

Where security group referencing works, and the gap in the docs

A security group rule can name another security group ID as its source instead of a CIDR. Only traffic from network interfaces (ENIs) that carry that security group is allowed, so the rule keeps working when IP ranges change. How far the reference reaches depends on how the VPCs are connected.

ConnectionSecurity group referencing
Same VPCNo restrictions
VPC peeringSame-Region peering only, in inbound and outbound rules
Transit GatewayInbound rules only, after it is turned on for both the TGW and the VPC attachments. Only between VPCs attached to the same TGW. Not across TGW peering, not for PrivateLink endpoints, and not through a Gateway Load Balancer or AWS Network Firewall in an inspection VPC

Table 1. Reach of security group referencing by connection type (VPC peering docs, TGW VPC attachment docs)

Amazon EKS security groups for Pods create one branch ENI per Pod on top of the node's trunk ENI, and attach the security groups named in a SecurityGroupPolicy to that branch ENI. Pods on the same node can have different security groups, so you can open a database to specific workloads only.

When the EKS cluster and the database sit in different VPCs connected by a TGW, being able to name the Pod security group as the source in the database security group would carry that per-Pod allow across the VPC boundary. Without it, you have to open the Pod or node range as a CIDR, and every other Pod in that range is allowed too. The TGW docs describe instance-to-instance traffic and say nothing about branch ENIs, so I tested it.

01

One Pod security group was enough to allow traffic across the TGW

Pods in an EKS cluster in VPC A connected to port 8080 on an EC2 instance in VPC B. The target security group sg-db in VPC B had one rule: "8080 from sg-pod", where sg-pod is the Pod security group in VPC A.

TestSourceWhat changedcurlFlow Logs (source IP, action)
1Pod with sg-pod (branch ENI)NothingHTTP 200Pod IP, ACCEPT
2Pod without a SecurityGroupPolicy (node security group)Source PodtimeoutPod IP, REJECT
3Same Pod as test 1Referencing off on the VPC A attachmenttimeoutPod IP, REJECT
3 restoredSame Pod as test 1Turned back onHTTP 200Pod IP, ACCEPT
4aPod with sg-podStandard mode + EXTERNALSNAT=falsetimeoutNode IP, REJECT
4bPods with and without the Pod security group4a plus a node security group ruleHTTP 200 for bothNode IP, ACCEPT

Table 2. Results. Seoul Region (ap-northeast-2), November 24, 2026, Kubernetes 1.35, Amazon VPC CNI EKS add-on

Test 1 is the result that matters; tests 2 and 3 are controls. Test 2 shows that the same VPC and the same path are blocked without sg-pod. Test 3 shows that the allow depends on the TGW referencing feature. Flow Logs recorded the Pod IP rather than the node IP, so the traffic left the branch ENI without SNAT.

02

Test setup: two VPCs and one TGW

I created 45 resources with Terraform in the Seoul Region of an empty account and deleted all of them after the tests. No instance had a public IP, and the EKS API endpoint was private only. I reached the cluster through an SSM session on the node.

VPC A 10.50.0.0/16 EKS 1.35, strict 모드, EXTERNALSNAT=true EKS 1.35, strict mode, EXTERNALSNAT=true 노드 m5.large (trunk ENI) Node m5.large (trunk ENI) with-sg Pod Pod 전용 branch ENI Dedicated branch ENI 보안 그룹: sg-pod + 클러스터 SG SGs: sg-pod + cluster SG 테스트 1, 3 Tests 1 and 3 no-sg Pod 노드 ENI의 보조 IP Secondary IP on node ENI 보안 그룹: 노드 SG만 SG: node SG only 테스트 2 Test 2 TGW attach A 테스트 3에서 Test 3 turns 참조 off referencing off Transit Gateway 보안 그룹 참조 on SG referencing on VPC B 10.60.0.0/16 TGW attach B EC2 t4g.nano :8080 EC2 t4g.nano :8080 sg-db 인바운드 규칙 sg-db inbound rule TCP 8080, 출발지 = sg-pod TCP 8080, source = sg-pod (VPC A의 보안 그룹 ID) (security group ID in VPC A) CIDR 규칙 없음 no CIDR rule Flow Logs: subnet 테스트 1: 허용 Test 1: allowed 테스트 2, 3: 차단 Tests 2 and 3: blocked 응답은 보안 그룹이 연결 상태를 기억해 자동 허용 Return traffic is allowed by connection tracking

Figure 1. Test setup. Solid lines are paths expected to be allowed; dashed lines are paths expected to be blocked.

ComponentConfiguration
VPC AEKS 1.35 cluster, one m5.large managed node (AL2023). A Nitro instance that supports trunk ENIs. One NAT gateway for image pulls
VPC CNIENABLE_POD_ENI=true, AWS_VPC_K8S_CNI_EXTERNALSNAT=true, POD_SECURITY_GROUP_ENFORCING_MODE not set (default strict)
sg-podAttached to the with-sg Pod through a SecurityGroupPolicy. Outbound 8080 to the VPC B CIDR, written as a CIDR because TGW referencing does not work in outbound rules. The cluster security group is attached as well for DNS
VPC BOne t4g.nano running python3 -m http.server 8080. VPC Flow Logs on the target subnet
sg-dbInbound 8080 from sg-pod only. No CIDR rule
TGWAttachments for VPC A and B. Referencing enabled on the TGW and both attachments. Each VPC route table sends the other VPC's CIDR to the TGW

Table 3. Components and configuration

03

Settings for referencing and for security groups for Pods

On the TGW side, turn on referencing for the TGW and both attachments, then add the security group from the other VPC as the source in the target security group. The test used security_group_referencing_support = "enable" in Terraform. The CLI equivalent:

bash
aws ec2 modify-transit-gateway --transit-gateway-id $TGW_ID \
  --options SecurityGroupReferencingSupport=enable

aws ec2 modify-transit-gateway-vpc-attachment --transit-gateway-attachment-id $ATT_A \
  --options SecurityGroupReferencingSupport=enable
aws ec2 modify-transit-gateway-vpc-attachment --transit-gateway-attachment-id $ATT_B \
  --options SecurityGroupReferencingSupport=enable

# Allow sg-pod (VPC A) as the source in sg-db (VPC B)
aws ec2 authorize-security-group-ingress --group-id $SG_DB \
  --ip-permissions "IpProtocol=tcp,FromPort=8080,ToPort=8080,UserIdGroupPairs=[{GroupId=$SG_POD}]"

On the EKS side, set environment variables on the VPC CNI add-on and assign the Pod security group with a SecurityGroupPolicy. The cluster IAM role needs the AmazonEKSVPCResourceController policy.

json (vpc-cni configuration_values)
{
  "env": {
    "ENABLE_POD_ENI": "true",
    "AWS_VPC_K8S_CNI_EXTERNALSNAT": "true"
  }
}
yaml
apiVersion: vpcresources.k8s.aws/v1beta1
kind: SecurityGroupPolicy
metadata:
  name: with-sg
  namespace: sgref
spec:
  podSelector:
    matchLabels:
      app: with-sg
  securityGroups:
    groupIds:
      - sg-pod-id        # sg-pod
      - sg-cluster-id    # cluster security group (DNS)

To confirm that a Pod got a branch ENI, check its vpc.amazonaws.com/pod-eni annotation. The with-sg Pod had this annotation:

text
[{"eniId":"eni-0123456789abcdef0","privateIp":"10.50.1.67","vlanId":1,
  "subnetCidr":"10.50.1.0/24","associationID":"trunk-assoc-xxxxxxxx"}]

04

curl results and Flow Logs for the three tests

Each Pod ran curl -m 5 http://10.60.1.15:8080/. An allowed connection returns HTTP 200 right away; a packet dropped by a security group times out after five seconds (exit code 28). For test 3, I set referencing on the VPC A attachment to disable, waited for the attachment to return to available, then waited another 60 seconds before connecting. Afterwards I set it back to enable and waited the same way.

text
pods: with-sg=10.50.1.67 (branch ENI)  no-sg=10.50.1.22  node=10.50.1.9
05:27:09 test 1 (sg-pod, branch ENI)        OK (HTTP 200)
05:27:10 test 2 (no SGPP, node SG)          FAIL (timeout)
05:27:54 attachment A referencing disable
         test 3                             FAIL (timeout)
05:29:24 attachment A referencing enable
         test 1 again                       OK (HTTP 200)

Filtering the Flow Logs for the target ENI in VPC B to port 8080 shows source IPs and actions that match the curl results.

text (VPC Flow Logs)
srcaddr      dstaddr     dstport  action  direction
10.50.1.22   10.60.1.15  8080     REJECT  ingress   # test 2
10.50.1.67   10.60.1.15  8080     ACCEPT  ingress   # test 1
10.50.1.67   10.60.1.15  8080     REJECT  ingress   # test 3
10.50.1.67   10.60.1.15  8080     ACCEPT  ingress   # test 1 after re-enable

Flow Logs also check the test's premise. If srcaddr had shown the node IP (10.50.1.9), the traffic would have been SNATed and judged by the node security group rather than the Pod security group. In all three tests it showed the Pod IP.

05

Standard mode with EXTERNALSNAT=false falls back to the node security group

POD_SECURITY_GROUP_ENFORCING_MODE controls how Pod security groups behave. According to the VPC CNI README, in the default strict mode the Pod security group applies to all of the Pod's traffic. In standard mode, traffic to addresses outside the VPC follows the EXTERNALSNAT setting: with it enabled the traffic is not SNATed, and with it disabled the traffic is SNATed through the node's eth0 and only eth0's security group applies. The EKS user guide says the same: with standard and AWS_VPC_K8S_CNI_EXTERNALSNAT=false, traffic to endpoints outside the VPC uses the node's security groups.

VPC B is outside VPC A's CIDR, so standard mode with AWS_VPC_K8S_CNI_EXTERNALSNAT=false means the Pod security group reference no longer matches. I checked this combination in the same environment. Existing Pods must be recreated after a mode change for the new behavior to apply.

strict 모드 (테스트 1) Strict mode (test 1) with-sg Pod branch ENI, 출발지 Pod IP branch ENI, source = Pod IP TGW (판정 기준: sg-pod) TGW (judged by sg-pod) sg-db: 8080 ← sg-pod 허용. Pod 단위 판정 Allowed. Judged per Pod standard 모드 + EXTERNALSNAT=false (테스트 4) Standard mode + EXTERNALSNAT=false (test 4) 노드 eth0로 SNAT SNAT to node eth0 출발지 노드 IP source = node IP TGW (판정 기준: 노드 SG) TGW (judged by node SG) 4a. sg-pod 규칙만 있으면 차단 4a. Blocked with the sg-pod rule only 4b. 노드 SG 규칙을 넣으면 노드의 Pod 모두 허용 4b. With a node SG rule, every Pod on the node passes

Figure 2. The source the target security group sees depends on the mode.

text
aws-node env: POD_SECURITY_GROUP_ENFORCING_MODE=standard  AWS_VPC_K8S_CNI_EXTERNALSNAT=false
06:30:20 4a  sg-db allows sg-pod only     with-sg FAIL (timeout)
06:30:43 4b  sg-db also allows node SG    with-sg OK (HTTP 200)  no-sg OK (HTTP 200)

srcaddr      dstaddr     dstport  action
10.50.1.9    10.60.1.15  8080     REJECT   # 4a
10.50.1.9    10.60.1.15  8080     ACCEPT   # 4b with-sg
10.50.1.9    10.60.1.15  8080     ACCEPT   # 4b no-sg

In 4b the node security group reference worked as expected. The node's primary ENI is a regular ENI, which is within what the TGW docs describe. But the decision is now per node: other Pods on the same node and hostNetwork DaemonSets are allowed too. To limit access to a specific workload in this setup, run that workload on its own node group and reference a security group that only those nodes carry.

06

When to apply it and what to check first

To allow a target in another TGW-connected VPC (a database, for example) per Pod, all of these must hold.

  • Both the TGW and both attachments have referencing turned on. It is off by default at the TGW level, including TGWs created before the feature existed (launch post). If one side is off, traffic that relies only on the reference is dropped, as in test 3. When replacing CIDR rules with references, keep the CIDR rules in place until you have confirmed the new rule works.
  • Inbound rules only. Write the Pod security group's outbound rules with the other VPC's CIDR or a prefix list.
  • The Pod IP reaches the target unchanged. The combination I confirmed is strict mode with EXTERNALSNAT=true. Check first that srcaddr in the Flow Logs for the target ENI is the Pod IP.
  • No inspection appliance on the path. The TGW docs say referencing does not work across a Gateway Load Balancer or Network Firewall in an inspection VPC. An AWS blog post says you cannot use referencing when more specific routing in a VPC sends traffic between subnets through a Gateway Load Balancer or Network Firewall for inspection (prefix list post). The Cloud WAN launch post says it doesn't work when Network Firewall or a third-party appliance inspects the traffic (Cloud WAN post). Allow such paths with a CIDR or prefix list.
  • The same TGW connects both VPCs. Paths across TGW peering and PrivateLink endpoints are not supported.

You still need TGW and VPC routes and network ACL entries in both directions. The reference only covers the security group decision.

07

Setups I did not test

The test used one account, one Region, one node, and one target. I did not check:

  • A Network Firewall or Gateway Load Balancer on the path. I did not reproduce what the docs and blog posts describe.
  • VPC A and VPC B in different accounts
  • Referencing a branch ENI security group over same-Region VPC peering
  • Nodes and targets spread across several Availability Zones
  • Strict mode with EXTERNALSNAT=false. The EKS user guide says strict mode turns off source NAT for Pods with security groups, so I expect the same result as test 1, but I did not run it.
  • Standard mode with EXTERNALSNAT=true
  • IPv6 clusters, Fargate, and nodes launched by Karpenter
  • How long a referencing change takes to apply. Test 3 waited 60 seconds after the attachment became available, so I did not measure it.

Test code

The Terraform configuration and scripts that produced these results are in the blog repository under demos/tgw-sg-referencing-pods. They create billable resources (an EKS control plane, a NAT gateway, TGW attachments, and two instances), so run terraform destroy when you are done.

References

보안 그룹 참조가 되는 범위와 문서에 없는 부분

보안 그룹 규칙의 출발지에는 CIDR 대신 다른 보안 그룹 ID를 적을 수 있습니다. 그 보안 그룹이 붙은 네트워크 인터페이스(ENI)에서 오는 트래픽만 허용하므로, IP 대역이 바뀌어도 규칙을 고칠 필요가 없습니다. 이 참조가 되는 범위는 연결 방식마다 다릅니다.

연결 방식보안 그룹 참조
같은 VPC제한 없음
VPC 피어링같은 리전의 피어링에서만, 인바운드와 아웃바운드 모두
Transit GatewayTGW와 VPC attachment 양쪽에서 켰을 때 인바운드만. 같은 TGW에 붙은 VPC끼리만 되고 TGW 피어링, PrivateLink 엔드포인트, 검사용 VPC의 Gateway Load Balancer나 AWS Network Firewall을 거치는 경로에서는 되지 않음

표 1. 연결 방식별 보안 그룹 참조 범위 (VPC 피어링 문서, TGW VPC attachment 문서)

Amazon EKS의 Pod 보안 그룹은 노드의 trunk ENI 위에 Pod마다 branch ENI를 하나씩 만들고, SecurityGroupPolicy로 지정한 보안 그룹을 그 branch ENI에 붙입니다. 같은 노드의 Pod라도 서로 다른 보안 그룹을 가질 수 있어서, 데이터베이스 접근을 특정 워크로드에만 열 수 있습니다.

EKS 클러스터와 데이터베이스가 서로 다른 VPC에 있고 TGW로 연결된 경우, 데이터베이스 보안 그룹에 Pod 보안 그룹을 출발지로 적을 수 있으면 Pod 단위 허용을 VPC 경계 너머에서도 그대로 쓸 수 있습니다. 그렇지 않으면 Pod 대역이나 노드 대역을 CIDR로 열어야 하고, 같은 대역의 다른 Pod도 함께 허용됩니다. TGW 문서는 인스턴스 간 트래픽을 기준으로 설명할 뿐 branch ENI에 대한 언급이 없어서, 이 부분을 직접 확인했습니다.

01

Pod 보안 그룹 하나로 TGW 너머 접속이 허용됐습니다

VPC A의 EKS 클러스터에서 VPC B의 EC2 8080 포트로 접속했습니다. VPC B의 대상 보안 그룹 sg-db에는 "8080, 출발지 = sg-pod" 규칙 하나만 두었습니다. sg-pod는 VPC A에 있는 Pod 보안 그룹입니다.

테스트출발지바꾼 것curlFlow Logs (출발지 IP, 결과)
1sg-pod가 붙은 Pod (branch ENI)없음HTTP 200Pod IP, ACCEPT
2SecurityGroupPolicy가 없는 Pod (노드 보안 그룹)출발지 PodtimeoutPod IP, REJECT
3테스트 1과 같은 PodVPC A attachment의 보안 그룹 참조 offtimeoutPod IP, REJECT
3 복구테스트 1과 같은 Pod다시 onHTTP 200Pod IP, ACCEPT
4asg-pod가 붙은 Podstandard 모드 + EXTERNALSNAT=falsetimeout노드 IP, REJECT
4bPod 보안 그룹이 있는 Pod와 없는 Pod4a에 노드 보안 그룹 규칙 추가둘 다 HTTP 200노드 IP, ACCEPT

표 2. 주요 결과. 2026년 11월 24일 서울 리전(ap-northeast-2), Kubernetes 1.35, Amazon VPC CNI EKS 애드온

테스트 1이 핵심 결과이고 2와 3은 대조군입니다. 2는 같은 VPC, 같은 경로라도 sg-pod가 없으면 막힌다는 것을, 3은 허용이 TGW의 보안 그룹 참조 기능 덕분이라는 것을 보여 줍니다. Flow Logs의 출발지가 노드 IP가 아니라 Pod IP였으므로, 트래픽은 SNAT 없이 branch ENI에서 그대로 나갔습니다.

02

VPC 두 개와 TGW 하나로 만든 테스트 구성

빈 계정 하나의 서울 리전에 Terraform으로 리소스 45개를 만들고, 테스트가 끝난 뒤 모두 지웠습니다. 인스턴스에는 공인 IP를 주지 않았고 EKS API 엔드포인트는 프라이빗 전용으로 두었습니다. 클러스터에는 노드의 SSM 세션을 거쳐 접속했습니다.

VPC A 10.50.0.0/16 EKS 1.35, strict 모드, EXTERNALSNAT=true EKS 1.35, strict mode, EXTERNALSNAT=true 노드 m5.large (trunk ENI) Node m5.large (trunk ENI) with-sg Pod Pod 전용 branch ENI Dedicated branch ENI 보안 그룹: sg-pod + 클러스터 SG SGs: sg-pod + cluster SG 테스트 1, 3 Tests 1 and 3 no-sg Pod 노드 ENI의 보조 IP Secondary IP on node ENI 보안 그룹: 노드 SG만 SG: node SG only 테스트 2 Test 2 TGW attach A 테스트 3에서 Test 3 turns 참조 off referencing off Transit Gateway 보안 그룹 참조 on SG referencing on VPC B 10.60.0.0/16 TGW attach B EC2 t4g.nano :8080 EC2 t4g.nano :8080 sg-db 인바운드 규칙 sg-db inbound rule TCP 8080, 출발지 = sg-pod TCP 8080, source = sg-pod (VPC A의 보안 그룹 ID) (security group ID in VPC A) CIDR 규칙 없음 no CIDR rule Flow Logs: subnet 테스트 1: 허용 Test 1: allowed 테스트 2, 3: 차단 Tests 2 and 3: blocked 응답은 보안 그룹이 연결 상태를 기억해 자동 허용 Return traffic is allowed by connection tracking

그림 1. 테스트 구성. 실선은 허용될 것으로 예상한 경로, 점선은 차단될 것으로 예상한 경로입니다.

구성 요소설정
VPC AEKS 1.35 클러스터, 관리형 노드 그룹 m5.large 1대(AL2023). trunk ENI를 지원하는 Nitro 인스턴스. 이미지를 받기 위한 NAT 게이트웨이 1개
VPC CNIENABLE_POD_ENI=true, AWS_VPC_K8S_CNI_EXTERNALSNAT=true, POD_SECURITY_GROUP_ENFORCING_MODE는 지정하지 않음(기본값 strict)
sg-podSecurityGroupPolicy로 with-sg Pod에 붙임. 아웃바운드는 VPC B CIDR로 8080. TGW에서는 아웃바운드 규칙에 보안 그룹을 참조할 수 없어서 CIDR로 적음. DNS를 위해 클러스터 보안 그룹도 함께 붙임
VPC Bt4g.nano 1대에서 python3 -m http.server 8080. 대상 서브넷에 VPC Flow Logs
sg-db인바운드 8080, 출발지는 sg-pod 하나. CIDR 규칙 없음
TGWVPC A와 B attachment. TGW와 두 attachment 모두 보안 그룹 참조 enable. 양쪽 라우팅 테이블에 상대 VPC CIDR → TGW

표 3. 구성 요소와 설정

03

보안 그룹 참조와 Pod 보안 그룹을 켜는 설정

TGW 쪽은 TGW와 양쪽 attachment에서 보안 그룹 참조를 켜고, 대상 보안 그룹에 다른 VPC의 보안 그룹을 출발지로 넣습니다. 테스트에서는 Terraform의 security_group_referencing_support = "enable"로 만들었고, CLI로는 다음과 같습니다.

bash
aws ec2 modify-transit-gateway --transit-gateway-id $TGW_ID \
  --options SecurityGroupReferencingSupport=enable

aws ec2 modify-transit-gateway-vpc-attachment --transit-gateway-attachment-id $ATT_A \
  --options SecurityGroupReferencingSupport=enable
aws ec2 modify-transit-gateway-vpc-attachment --transit-gateway-attachment-id $ATT_B \
  --options SecurityGroupReferencingSupport=enable

# VPC B의 sg-db에 VPC A의 sg-pod를 출발지로 넣는다
aws ec2 authorize-security-group-ingress --group-id $SG_DB \
  --ip-permissions "IpProtocol=tcp,FromPort=8080,ToPort=8080,UserIdGroupPairs=[{GroupId=$SG_POD}]"

EKS 쪽은 VPC CNI 애드온에 환경 변수를 넣고, Pod 보안 그룹을 SecurityGroupPolicy로 지정합니다. 클러스터 IAM 역할에는 AmazonEKSVPCResourceController 정책이 필요합니다.

json (vpc-cni configuration_values)
{
  "env": {
    "ENABLE_POD_ENI": "true",
    "AWS_VPC_K8S_CNI_EXTERNALSNAT": "true"
  }
}
yaml
apiVersion: vpcresources.k8s.aws/v1beta1
kind: SecurityGroupPolicy
metadata:
  name: with-sg
  namespace: sgref
spec:
  podSelector:
    matchLabels:
      app: with-sg
  securityGroups:
    groupIds:
      - sg-pod-id        # sg-pod
      - sg-cluster-id    # 클러스터 보안 그룹 (DNS)

Pod가 실제로 branch ENI를 받았는지는 Pod의 vpc.amazonaws.com/pod-eni 주석으로 확인합니다. 테스트에서 with-sg Pod에는 다음 주석이 달렸습니다.

text
[{"eniId":"eni-0123456789abcdef0","privateIp":"10.50.1.67","vlanId":1,
  "subnetCidr":"10.50.1.0/24","associationID":"trunk-assoc-xxxxxxxx"}]

04

테스트 세 개의 curl 결과와 Flow Logs

각 Pod에서 curl -m 5 http://10.60.1.15:8080/을 실행했습니다. 허용되면 바로 HTTP 200이 오고, 보안 그룹이 패킷을 버리면 5초 뒤 timeout(종료 코드 28)이 납니다. 테스트 3에서는 VPC A attachment의 보안 그룹 참조를 disable로 바꾸고 attachment가 available로 돌아온 뒤 60초를 기다렸다가 접속했습니다. 끝난 뒤 다시 enable로 바꾸고 같은 방법으로 기다렸습니다.

text
pods: with-sg=10.50.1.67 (branch ENI)  no-sg=10.50.1.22  node=10.50.1.9
05:27:09 test 1 (sg-pod, branch ENI)        OK (HTTP 200)
05:27:10 test 2 (no SGPP, node SG)          FAIL (timeout)
05:27:54 attachment A referencing disable
         test 3                             FAIL (timeout)
05:29:24 attachment A referencing enable
         test 1 again                       OK (HTTP 200)

VPC B 대상 ENI에 대한 Flow Logs에서 8080 포트만 골라 보면 출발지 IP와 판정이 curl 결과와 일치합니다.

text (VPC Flow Logs)
srcaddr      dstaddr     dstport  action  direction
10.50.1.22   10.60.1.15  8080     REJECT  ingress   # test 2
10.50.1.67   10.60.1.15  8080     ACCEPT  ingress   # test 1
10.50.1.67   10.60.1.15  8080     REJECT  ingress   # test 3
10.50.1.67   10.60.1.15  8080     ACCEPT  ingress   # test 1 after re-enable

Flow Logs는 테스트 전제를 확인하는 데도 씁니다. srcaddr가 노드 IP(10.50.1.9)로 찍혔다면 SNAT가 일어난 것이고, 그때는 Pod 보안 그룹이 아니라 노드 보안 그룹으로 판정됩니다. 이번에는 세 테스트 모두 Pod IP로 찍혔습니다.

05

standard 모드와 EXTERNALSNAT=false에서는 노드 보안 그룹으로 판정됩니다

Pod 보안 그룹의 동작 방식은 POD_SECURITY_GROUP_ENFORCING_MODE로 정합니다. VPC CNI README에 따르면 기본값인 strict 모드에서는 Pod의 모든 트래픽에 Pod 보안 그룹이 적용됩니다. standard 모드에서 VPC 밖으로 나가는 트래픽은 EXTERNALSNAT 설정을 따릅니다. EXTERNALSNAT가 켜져 있으면 SNAT 없이 나가고, 꺼져 있으면 노드의 eth0으로 SNAT되어 eth0의 보안 그룹만 적용됩니다. EKS 사용 설명서도 standard와 AWS_VPC_K8S_CNI_EXTERNALSNAT=false에서는 VPC 밖으로 가는 트래픽에 노드의 보안 그룹이 쓰인다고 설명합니다.

VPC B는 VPC A의 CIDR 밖이므로, standard 모드에 AWS_VPC_K8S_CNI_EXTERNALSNAT=false를 쓰면 Pod 보안 그룹 참조가 맞지 않게 됩니다. 이 조합도 같은 환경에서 확인했습니다. 모드를 바꾼 뒤에는 기존 Pod를 다시 만들어야 새 동작이 적용됩니다.

strict 모드 (테스트 1) Strict mode (test 1) with-sg Pod branch ENI, 출발지 Pod IP branch ENI, source = Pod IP TGW (판정 기준: sg-pod) TGW (judged by sg-pod) sg-db: 8080 ← sg-pod 허용. Pod 단위 판정 Allowed. Judged per Pod standard 모드 + EXTERNALSNAT=false (테스트 4) Standard mode + EXTERNALSNAT=false (test 4) 노드 eth0로 SNAT SNAT to node eth0 출발지 노드 IP source = node IP TGW (판정 기준: 노드 SG) TGW (judged by node SG) 4a. sg-pod 규칙만 있으면 차단 4a. Blocked with the sg-pod rule only 4b. 노드 SG 규칙을 넣으면 노드의 Pod 모두 허용 4b. With a node SG rule, every Pod on the node passes

그림 2. 모드에 따라 대상 보안 그룹이 보는 출발지가 달라집니다.

text
aws-node env: POD_SECURITY_GROUP_ENFORCING_MODE=standard  AWS_VPC_K8S_CNI_EXTERNALSNAT=false
06:30:20 4a  sg-db allows sg-pod only     with-sg FAIL (timeout)
06:30:43 4b  sg-db also allows node SG    with-sg OK (HTTP 200)  no-sg OK (HTTP 200)

srcaddr      dstaddr     dstport  action
10.50.1.9    10.60.1.15  8080     REJECT   # 4a
10.50.1.9    10.60.1.15  8080     ACCEPT   # 4b with-sg
10.50.1.9    10.60.1.15  8080     ACCEPT   # 4b no-sg

4b에서 노드 보안 그룹 참조는 정상으로 동작했습니다. 노드의 기본 ENI는 일반 ENI라서 TGW 문서가 설명하는 범위 안에 있습니다. 다만 판정이 노드 단위가 되어, 같은 노드에 있는 다른 Pod와 hostNetwork를 쓰는 DaemonSet도 함께 허용됩니다. 이 구성에서 대상 접근을 특정 워크로드로 좁히려면 그 워크로드만 올라가는 노드 그룹을 따로 두고 그 노드에만 붙는 보안 그룹을 참조해야 합니다.

06

적용할 조건과 적용 전에 확인할 항목

TGW로 연결된 다른 VPC의 대상(데이터베이스 등)을 Pod 단위로 허용하려면 다음 조건을 모두 만족해야 합니다.

  • TGW와 양쪽 attachment에서 보안 그룹 참조가 켜져 있어야 합니다. TGW 수준의 기본값은 꺼짐이고, 이 기능이 나오기 전에 만든 TGW도 꺼져 있습니다(발표 블로그). 한쪽만 꺼져 있어도 테스트 3처럼 보안 그룹 참조에만 의존하는 트래픽은 막힙니다. 기존 CIDR 규칙을 보안 그룹 참조로 바꿀 때는 CIDR 규칙을 남겨 둔 채로 먼저 확인하는 편이 안전합니다.
  • 인바운드 규칙에만 씁니다. Pod 보안 그룹의 아웃바운드는 상대 VPC CIDR이나 prefix list로 적어야 합니다.
  • Pod IP 그대로 대상에 도착해야 합니다. 이번에 확인한 조합은 strict 모드 + EXTERNALSNAT=true입니다. 대상 ENI에 대한 Flow Logs에서 srcaddr가 Pod IP인지 먼저 확인합니다.
  • 경로에 검사 장비가 없어야 합니다. TGW 문서는 검사용 VPC의 Gateway Load Balancer나 Network Firewall을 거치면 보안 그룹 참조가 동작하지 않는다고 설명합니다. AWS 블로그는 VPC 안에서 더 구체적인 라우팅으로 서브넷 사이 트래픽을 Gateway Load Balancer나 Network Firewall로 검사하면 보안 그룹 참조를 쓸 수 없다고 설명합니다(prefix list 블로그). Cloud WAN 발표 블로그도 Network Firewall이나 서드파티 장비가 트래픽을 검사하면 동작하지 않는다고 설명합니다(Cloud WAN 블로그). 그런 경로는 CIDR이나 prefix list로 허용합니다.
  • 같은 TGW에 두 VPC가 붙어 있어야 합니다. TGW 피어링을 거치는 경로와 PrivateLink 엔드포인트는 지원하지 않습니다.

TGW와 VPC 라우팅, 네트워크 ACL은 양방향으로 따로 열어야 합니다. 보안 그룹 참조는 보안 그룹의 허용 여부만 정합니다.

07

테스트하지 않은 구성

이번 테스트는 한 계정, 한 리전, 노드 1대, 대상 1대로 진행했습니다. 다음 구성은 확인하지 않았습니다.

  • 경로에 Network Firewall이나 Gateway Load Balancer가 있는 경우. 문서와 블로그 내용을 재현하지 않았습니다.
  • VPC A와 B가 서로 다른 계정에 있는 경우
  • 같은 리전의 VPC 피어링에서 branch ENI 보안 그룹을 참조하는 경우
  • 노드와 대상이 여러 가용 영역에 나뉘어 있는 경우
  • strict 모드 + EXTERNALSNAT=false. EKS 사용 설명서에 따르면 strict 모드에서는 보안 그룹이 붙은 Pod의 SNAT가 꺼지므로 테스트 1과 같은 결과를 예상하지만, 실행하지 않았습니다.
  • standard 모드 + EXTERNALSNAT=true
  • IPv6 클러스터, Fargate, Karpenter가 띄운 노드
  • 보안 그룹 참조 설정을 바꾼 뒤 실제로 반영되기까지 걸리는 시간. 테스트 3은 attachment가 available이 된 뒤 60초를 기다렸기 때문에 반영 시간을 측정하지 않았습니다.

테스트 코드

이 결과를 만든 Terraform 코드와 스크립트는 블로그 저장소의 demos/tgw-sg-referencing-pods에 있습니다. EKS 컨트롤 플레인, NAT 게이트웨이, TGW attachment, 인스턴스 2대처럼 비용이 드는 리소스를 만들므로 테스트가 끝나면 terraform destroy로 지우세요.

참고 자료