Azure Functions - Get the outbound IP adresse used in each request

Viewed 73

I've implemented an azure function with a timer tigger to scrape a website.

Since azure function used a different outbound IP adresse on each request. I want to log the IP adresse used to call the https://example.com (see code example below).

import logging
import azure.functions as func
import requests


def main(mytimer: func.TimerRequest) -> None:
    if mytimer.past_due:
        logging.info('The timer is past due!')

    response = requests.get('https://example.com')
    logging.info(response.text)
1 Answers

Get the outbound IP adresse used in each request

After testing in our local environment and as suggested by @Thomas if you could call the json link http://ipinfo.io/json you will get the Outbound IP

Here is the code to call the link in Azure functions:

#r "Newtonsoft.Json"

  

using System.Net;

using Microsoft.AspNetCore.Mvc;

using Microsoft.Extensions.Primitives;

using Newtonsoft.Json;

using System.Net.Http.Headers;

  

public  static  async Task<IActionResult> Run(HttpRequest req, ILogger log)

{

log.LogInformation("C# HTTP trigger function processed a request.");

string url = "http://ipinfo.io/json";

HttpClient client = new HttpClient();

using HttpResponseMessage response = await client.GetAsync(url);

using HttpContent content = response.Content;

string pageContent = await content.ReadAsStringAsync();

return  new OkObjectResult(pageContent);

}

Output:

enter image description here

Else,

You could even pull out the outbound IP manually through Azure Portal in Properties pane of functions.

enter image description here

Related