VPC内.NET Lambda连接AWS Keyspaces报远程证书无效问题排查
我按照AWS Keyspaces .NET Core驱动官方文档的指引,使用Terraform搭建了自认为符合要求的基础设施,但尝试建立连接时触发如下错误:
{ "errorType": "AggregateException", "errorMessage": "One or more errors occurred. (All hosts tried for query failed (tried 172.31.41.121:9142: AuthenticationException 'The remote certificate is invalid according to the validation procedure.'; 172.31.18.20:9142: AuthenticationException 'The remote certificate is invalid according to the validation procedure.'))", "stackTrace": [ "at lambda_method(Closure , Stream , Stream , LambdaContextInternal )" ], "cause": { "errorType": "NoHostAvailableException", "errorMessage": "All hosts tried for query failed (tried 172.31.41.121:9142: AuthenticationException 'The remote certificate is invalid according to the validation procedure.'; 172.31.18.20:9142: AuthenticationException 'The remote certificate is invalid according to the validation procedure.')", "stackTrace": [ "at Cassandra.Connections.Control.ControlConnection.Connect(Boolean isInitializing)", "at Cassandra.Connections.Control.ControlConnection.InitAsync()", "at Cassandra.Tasks.TaskHelper.WaitToCompleteAsync(Task task, Int32 timeout)", "at Cassandra.Cluster.Init()", "at Cassandra.Cluster.ConnectAsync(String keyspace)" ] }, "causes": [ { "errorType": "NoHostAvailableException", "errorMessage": "All hosts tried for query failed (tried 172.31.41.121:9142: AuthenticationException 'The remote certificate is invalid according to the validation procedure.'; 172.31.18.20:9142: AuthenticationException 'The remote certificate is invalid according to the validation procedure.')", "stackTrace": [ "at Cassandra.Connections.Control.ControlConnection.Connect(Boolean isInitializing)", "at Cassandra.Connections.Control.ControlConnection.InitAsync()", "at Cassandra.Tasks.TaskHelper.WaitToCompleteAsync(Task task, Int32 timeout)", "at Cassandra.Cluster.Init()", "at Cassandra.Cluster.ConnectAsync(String keyspace)" ] } ] }
我已创建aws_vpc_endpoint_service资源,对当前连接失败的情况感到困惑,相关Terraform配置如下:
# Security group for resources that want to access Keyspaces from the VPC resource "aws_security_group" "keyspaces_endpoint_vpc_access" { name = "keyspaces-endpoint-access" vpc_id = aws_default_vpc.default.id } resource "aws_security_group" "keyspaces_endpoint" { name = "keyspaces-endpoint" vpc_id = aws_default_vpc.default.id ingress { from_port = 9142 to_port = 9142 protocol = "tcp" security_groups = [ aws_security_group.keyspaces_endpoint_vpc_access.id ] } } data "aws_vpc_endpoint_service" "keyspaces" { service = "cassandra" } resource "aws_vpc_endpoint" "keyspaces_endpoint" { vpc_id = aws_default_vpc.default.id vpc_endpoint_type = "Interface" service_name = data.aws_vpc_endpoint_service.keyspaces.service_name security_group_ids = [ aws_security_group.keyspaces_endpoint.id ] private_dns_enabled = true subnet_ids = [ data.aws_subnet.selected.id, aws_default_subnet.subnet_a.id, aws_default_subnet.subnet_b.id ] policy = <<EOF { "Statement": [ { "Sid": "keyspaces-full-access", "Principal": "*", "Action": "cassandra:*", "Effect": "Allow", "Resource": "*" } ] } EOF } resource "aws_security_group" "my_func" { vpc_id = aws_default_vpc.default.id egress { from_port = 0 to_port = 65535 protocol = "tcp" cidr_blocks = [ "0.0.0.0/0" ] } lifecycle { create_before_destroy = true } } resource "aws_lambda_function" "my_func" { runtime = "dotnetcore3.1" timeout = 900 memory_size = 512 # etc. vpc_config { subnet_ids = [ data.aws_subnet.selected.id ] security_group_ids = [ aws_security_group.my_func.id, aws_security_group.keyspaces_endpoint_vpc_access.id ] } }
请问当前的配置存在什么问题?
核心问题1:VPC私有DNS配置不完整,导致域名校验失败
你虽然为VPC端点开启了private_dns_enabled = true,但该功能生效的前提是关联VPC同时开启两个属性:
enableDnsSupport:允许VPC内资源使用私有DNS解析服务域名enableDnsHostnames:为VPC内实例分配可解析的DNS主机名
默认VPC通常仅开启enableDnsSupport,未开启enableDnsHostnames,这会导致DNS无法将Keyspaces的官方服务域名解析为VPC端点的私有IP,驱动最终直接访问了端点的私有IP(即报错日志中的172.31.x.x地址)。
而Keyspaces的SSL证书仅绑定官方服务域名(如cassandra.<区域>.amazonaws.com),不会绑定端点的私有IP,直接用IP连接自然会触发「远程证书无效」的校验错误。
修复方式
在Terraform中补充默认VPC的属性配置:
resource "aws_vpc" "default" { # 复用现有默认VPC时直接修改对应参数即可 enable_dns_support = true enable_dns_hostnames = true }
配置生效后,确保连接字符串使用Keyspaces的官方服务域名,不要硬编码端点私有IP。
核心问题2:.NET驱动未正确配置SSL证书校验
接入Keyspaces的.NET Cassandra驱动时,必须显式指定校验用的根证书(Amazon Root CA 1),如果驱动未加载对应根证书,即使域名解析正确,也会触发证书无效报错。
另外你使用的dotnetcore3.1运行时已停止维护,旧版本驱动的默认证书校验逻辑也存在兼容问题。
修复方式
在驱动初始化时显式指定SSL校验使用的证书集:
var cluster = Cluster.Builder() .AddContactPoint("cassandra.<你的AWS区域>.amazonaws.com") .WithPort(9142) .WithAuthProvider(new PlainTextAuthProvider("你的IAM访问密钥", "你的IAM会话密钥")) .WithSSL(new SSLOptions() .SetCertificateCollection(new X509Certificate2Collection( new X509Certificate2("<Amazon Root CA 1的本地证书路径>"))) .Build();
次要配置问题
你定义的aws_security_group.keyspaces_endpoint_vpc_access没有配置任何规则,虽然当前Lambda同时关联了带全放行出网规则的my_func安全组,不会直接阻断流量,但后续如果有其他资源仅关联该安全组时,会无法正常发起出站请求。建议补全该安全组的默认出站规则(AWS默认安全组默认放行全部出站流量,可参考该逻辑配置)。
内容的提问来源于stack exchange,提问作者sdgfsdh

