跳转至

Get Source IP in API Gateway

client ip

  • create function nodejs 18x

    export const handler = async (event,context) => {
        console.log('Received event:', JSON.stringify(event, null, 2));
        context.succeed(event);
    };
    

  • create rest api

  • create POST method and integration request to lambda
  • in method execution, add mapping templates, content-type is application/json, content as following:

    {
      "sourceIp" : "$context.identity.sourceIp",
      "input" : "$input.path('$')"
    }
    

  • deploy it and test

  • curl
    curl -X POST -H 'Content-type: application/json' \
    -d '{ "key1": "value1", "key2": "value2", "key3": "value3" }' \
    https://xxx.execute-api.us-east-2.amazonaws.com/prod/
    

alb ip

refer