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
asked 5 months ago257 views
1 Answer
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
EXPERT
answered 5 months ago
  • 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?

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions