Skip to content

Commit a310826

Browse files
committed
fix: Add NAT gateway IPs to Keycloak ALB security group
When ECS tasks in private subnets call Keycloak's public DNS name, traffic goes through the NAT gateway and the source IP becomes the NAT gateway's public IP instead of the ECS task's security group. This was causing OAuth2 callback failures. Added dynamic security group rule that allows HTTPS traffic from all NAT gateway public IPs to the Keycloak load balancer.
1 parent dd3b728 commit a310826

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

terraform/aws-ecs/keycloak-security-groups.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ resource "aws_security_group_rule" "keycloak_lb_ingress_https" {
9797
}
9898

9999
# Load Balancer Ingress from MCP Gateway Auth Server (HTTPS)
100+
# Note: This rule is for direct VPC traffic. For traffic via NAT gateway,
101+
# see keycloak_lb_ingress_nat_gateway rule below.
100102
resource "aws_security_group_rule" "keycloak_lb_ingress_auth_server" {
101103
description = "Ingress from MCP Gateway Auth Server to Keycloak load balancer (HTTPS)"
102104
type = "ingress"
@@ -107,6 +109,19 @@ resource "aws_security_group_rule" "keycloak_lb_ingress_auth_server" {
107109
source_security_group_id = module.mcp_gateway.ecs_security_group_ids.auth
108110
}
109111

112+
# Load Balancer Ingress from NAT Gateways (for ECS tasks making HTTPS requests to Keycloak public URL)
113+
# When ECS tasks in private subnets call Keycloak's public DNS name, traffic goes through NAT gateway.
114+
# The source IP becomes the NAT gateway's public IP, not the ECS task's security group.
115+
resource "aws_security_group_rule" "keycloak_lb_ingress_nat_gateway" {
116+
description = "Ingress from NAT gateways to Keycloak load balancer (HTTPS)"
117+
type = "ingress"
118+
from_port = 443
119+
to_port = 443
120+
protocol = "tcp"
121+
cidr_blocks = [for ip in module.vpc.nat_public_ips : "${ip}/32"]
122+
security_group_id = aws_security_group.keycloak_lb.id
123+
}
124+
110125
# Load Balancer Ingress from MCP Gateway Registry (HTTPS)
111126
resource "aws_security_group_rule" "keycloak_lb_ingress_registry" {
112127
description = "Ingress from MCP Gateway Registry to Keycloak load balancer (HTTPS)"

0 commit comments

Comments
 (0)