How to use ipv6 in Java Lambda functions?

0

Given that ipv6 is now supported in aws lambda, I have this code-

System.setProperty("java.net.preferIPv6Addresses", "true");
InetAddress ipAddress = Inet6Address.getByName("www.googleapis.com");
logger.info("ip is {}", ipAddress.getHostAddress());

and yet, the hostname is resolving to an ipv4. Note that I am deploying my function as an image with base public.ecr.aws/lambda/java:21. When I run the code locally or on an ec2 instance, I see ipv6 address but on lambda, I see ipv4 address.

FWIW, I have also tried java runtime version 17 (as opposed to a docker image) but I still can't make lambda resolve to an ipv6 address.

gk
已提问 6 个月前288 查看次数
1 回答
0

Hello.

This update seems to allow Lambda connected to VPC to use IPv6, but is your Lambda connected to VPC?
https://aws.amazon.com/about-aws/whats-new/2023/10/aws-lambda-ipv6-outbound-connections-vpc/?nc1=h_ls

profile picture
专家
已回答 6 个月前
  • Of course :) and I can see "Allow IPv6 traffic = true" in the VPC config of the lambda

  • The code below is in Python, but in that case I was able to obtain IPv6. Therefore, you can check the IPv6 address with Lambda as well.

    import json
    import socket
    
    def lambda_handler(event, context):
        try:
            ipv6_address = socket.getaddrinfo("www.googleapis.com", None, socket.AF_INET6)[0][4][0]
    
            print(f"IPv6 address for www.googleapis.com: {ipv6_address}")
        except socket.gaierror as e:
            print(f"Error getting IPv6 address: {e}")
    
    

    Is IPv6 linked to VPC?

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则