Faking DDNS with Azure Services

Published 06-03-2021 12:00 AM 2,062 Views
Microsoft

Hello folks,

Since the beginning of the pandemic, we’ve all been mostly stuck to our home offices.  And since I’ve been concentrating of the hybrid services that Azure can provide I setup a simulated on-prem environment at home with left-over desktop, laptops, and Raspberry PI.

 

It’s been really cool and useful.  But now that vaccination is progressing and that the government is starting to talk about re-opening the economy and the border, I’m thinking I might get back on the road sooner rather than later.  I’ll want to access my “on-prem lab” environment from anywhere but my ISP cycles my IP address regularly.

 

I could always use a service like noip.com or dyndns but I already have a zone managed in Azure DNS and I thought it would be fun to figure it out.  Azure DNS doesn't currently support zone transfers so I could not setup a zone transfer from my own DNS server. DNS zones can be imported into Azure DNS by using the Azure CLI. DNS records are managed via the Azure DNS management portalREST APISDKPowerShell cmdlets, or the CLI tool.

 

dns1.png

Here is my V1 plan.

I already have a DNS server running on a Linux server in my on-prem environment.  I will schedule a job to get the external IP of my ISP’s router and send it to my zone in Azure.  Sounds easy….  Problem...  I don’t want to cache credentials for my job to connect to my zone and update the record.

 

Plan v1.1

Just like v1, I will schedule a job on my Linux server to get the external IP of my ISP’s router.  BUT!! I code it to send it to an Azure Function via a webhook and let the function update my record in the DNS zone in Azure.

 

That’s better...

 

I can secure the webhook with a function key and assign a managed identity to the function, so it only has rights to update the zone records but not change any of the other configuration and security.

 

That should work!

 

Let’s go.

Linux Server setup

I installed PowerShell Core on the Linux box since I’m more familiar with PowerShell than I am with bash.  But I could have used either.

Created a simple script   Some parts of the script like the Azure Function key and the Function webhook URL will be updated after we create the function.

 

 

 

 

Function Get-ExternalIP {
    $jsonContent = Invoke-WebRequest -UseBasicParsing -Uri "http://ipinfo.io/json" | ConvertFrom-Json

    $pattern = "^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$"

    If ($jsonContent.ip -match $pattern)
    {
        return $jsonContent.ip
    }
    Else
    {
        Write-Output "False"
        Return $false
    }
}

$logfilepath= "/home/wiredcanuck/pwsh.log"
Start-Transcript -Path $logfilepath

$datestamp = Get-Date
write-host $datestamp

# get the current DNS record (Using Dig since Resolve-DnsName in the DnsClient module as not yet been ported to PowerShell Core)
$EI = dig home.wiredcanuck.com +short
Write-host $EI.IPAddress

$IP = Get-ExternalIP

if ($IP -ne $EI)
{
    $WebHook = https://homednsupdate.azurewebsites.net/api/UpdateDNSRecord
# the code below id the Function Key
    $code = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    $name = "home"

    write-host "External IP is $IP"

    $URL = $WebHook+"?code="+$code+"&name="+$name+"&ip="+$IP

    write-host "calling WebHook"

    $response = Invoke-WebRequest -UseBasicParsing $URL -Method POST

    Write-host $response

}
Else
{
    write-host "Current IP ($IP) is the same as the IP in the DNS record ($EI).  no update needed"
}

 

 

 

 

Azure Function Setup

In the Azure portal I navigated to the Resource Group where my DNS Zone is lo0cation and created an Azure Function

 

dns4.png

 

I ensured to create the function with the PowerShell Core stack and using a consumption (Serverless) plan type.

 

dns5.png

Once the function was created, I navigated to the function App and clicked on the Functions menu item on the left of the screen.

 

dns6.png

And clicked the “+ Add” selection on the next screen and fill in the details like Using the Portal for development (1), “HTTP Trigger” from the template list (2), a function name (3) and “Function” as the authorization level.

 

dns7.png

Going back to the DNSUpdate1 function screen I made sure to navigate to the “Identity” section and turn on the system assigned identity. And save the changes and confirm on the next screen. That way the function has an identity we can use to restrict access.

 

dns8.png

When the changes are saved, you can click the Azure role assignments button to assign the proper rights.  In my case I only assigned “DNS Zone contributor” only in the specific resource group in my subscription.

 

dns9.png

Now that my function is created and has the proper rights, we can create the PowerShell code in the “Code + Test” section and insert our code.

 

dns10.png

 

The code will capture the HTTP request, and the body of the request and process the information.

 

 

 

 

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."

# Interact with query parameters or the body of the request.

$RG = "WiredCanuck"
$ZoneName = "Wiredcanuck.com"

$name = $Request.Query.Name
if (-not $name) {
    $name = $Request.Body.Name
}

$NewIP = $Request.Query.IP
if (-not $NewIP) {
    $IP = $Request.Body.IP
}

$RecordSet = Get-AzDnsRecordSet -ResourceGroupName $RG -ZoneName $ZoneName -Name $name -RecordType A

if (-not $RecordSet) {
    $RecordSet = New-AzDnsRecordSet -Name $name -RecordType A -ZoneName $ZoneName -ResourceGroupName $RG -Ttl 3600 -DnsRecords (New-AzDnsRecordConfig -IPv4Address $NewIP)
}
$OldIP = $recordset.Records.Ipv4Address

if ($NewIP -ne $OldIP) {
    $body = "DNS address $name.$ZoneName does not match. $NewIP will be created"
    $Records = @()
    $Records += New-AzDnsRecordConfig -IPv4Address $NewIP
    $RecordSet = New-AzDnsRecordSet -Name $name -RecordType A -ZoneName $ZoneName -ResourceGroupName $RG -Ttl 3600 -DnsRecords $Records -Confirm:$False -Overwrite
}
Else {
    $body = "Dynamic address ($NewIP) and DNS address ($OldIP) match.  No update needed"
}

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
    StatusCode = [HttpStatusCode]::OK
    Body = $body
})

 

 

 

 

Once the code is saved, I can click the “Get function URL” to get the Webhook address for the on-prem portion of this solution.  The URL will include the Default function key as highlighted below

 

https://dnsupdate1.azurewebsites.net/api/HttpTrigger1?code=FPX43K329H7PEeJPl68ZC8LZfYCwkDyKJElShadzPWSxtc3LQASlbA==

 

dns11.png

 

And now, every 12 hours (as per my CRON job) the IP address of my home network will be checked and updated if need be.  You can monitor the results in the monitor section and see all the invocation of the function.

 

dns12.png

 

Conclusion

This just proves that even if some functionalities don’t exist yet, you can leverage multiple Azure services and configuration to address on-prem issues efficiently and securely.

 

Are you running into challenges that could be addressed with cloud services and a bit of imaginations?  Let me know.

 

Cheers!

 

Pierre

1 Comment
Senior Member

I use something quite similar but imho simpler : the machine/rpi/router calling every XX minutes (or even better on the dhcp hooks of the router) an azure function with these inputs : the zone name and the recordset to update (and the function key of course to protect it). The function itself gets the the client IP originating the request so I don't have any script running locally, just a curl call to the function in my crontab

%3CLINGO-SUB%20id%3D%22lingo-sub-2411008%22%20slang%3D%22en-US%22%3EFaking%20DDNS%20with%20Azure%20Services%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2411008%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EHello%20folks%2C%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ESince%20the%20beginning%20of%20the%20pandemic%2C%20we%E2%80%99ve%20all%20been%20mostly%20stuck%20to%20our%20home%20offices.%26nbsp%3B%20And%20since%20I%E2%80%99ve%20been%20concentrating%20of%20the%20hybrid%20services%20that%20Azure%20can%20provide%20I%20setup%20a%20simulated%20on-prem%20environment%20at%20home%20with%20left-over%20desktop%2C%20laptops%2C%20and%20Raspberry%20PI.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIt%E2%80%99s%20been%20really%20cool%20and%20useful.%26nbsp%3B%20But%20now%20that%20vaccination%20is%20progressing%20and%20that%20the%20government%20is%20starting%20to%20talk%20about%20re-opening%20the%20economy%20and%20the%20border%2C%20I%E2%80%99m%20thinking%20I%20might%20get%20back%20on%20the%20road%20sooner%20rather%20than%20later.%26nbsp%3B%20I%E2%80%99ll%20want%20to%20access%20my%20%E2%80%9Con-prem%20lab%E2%80%9D%20environment%20from%20anywhere%20but%20my%20ISP%20cycles%20my%20IP%20address%20regularly.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EI%20could%20always%20use%20a%20service%20like%20noip.com%20or%20dyndns%20but%20I%20already%20have%20a%20zone%20managed%20in%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fdns%2Fdns-overview%2F%3FWT.mc_id%3Dmodinfra-30397-pierrer%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20DNS%3C%2FA%3E%20and%20I%20thought%20it%20would%20be%20fun%20to%20figure%20it%20out.%26nbsp%3B%20%3C%2FSPAN%3EAzure%20DNS%20doesn't%20currently%20support%20zone%20transfers%20so%20I%20could%20not%20setup%20a%20zone%20transfer%20from%20my%20own%20DNS%20server.%20DNS%20zones%20can%20be%26nbsp%3B%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fdns%2Fdns-import-export%3FWT.mc_id%3Dmodinfra-30397-pierrer%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Eimported%20into%20Azure%20DNS%20by%20using%20the%20Azure%20CLI%3C%2FA%3E%3C%2FSPAN%3E.%20DNS%20records%20are%20managed%20via%20the%26nbsp%3B%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fdns%2Fdns-operations-recordsets-portal%3FWT.mc_id%3Dmodinfra-30397-pierrer%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20DNS%20management%20portal%3C%2FA%3E%3C%2FSPAN%3E%2C%26nbsp%3B%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fpowershell%2Fmodule%2Faz.dns%2F%3FWT.mc_id%3Dmodinfra-30397-pierrer%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EREST%20API%3C%2FA%3E%3C%2FSPAN%3E%2C%26nbsp%3B%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fdns%2Fdns-sdk%3FWT.mc_id%3Dmodinfra-30397-pierrer%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ESDK%3C%2FA%3E%3C%2FSPAN%3E%2C%26nbsp%3B%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fdns%2Fdns-operations-recordsets%3FWT.mc_id%3Dmodinfra-30397-pierrer%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EPowerShell%20cmdlets%3C%2FA%3E%3C%2FSPAN%3E%2C%20or%20the%26nbsp%3B%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fdns%2Fdns-operations-recordsets-cli%3FWT.mc_id%3Dmodinfra-30397-pierrer%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ECLI%20tool%3C%2FA%3E%3C%2FSPAN%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22dns1.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F285817i604387DEB217CF97%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22dns1.png%22%20alt%3D%22dns1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%E2%80%83%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--442118089%22%20id%3D%22toc-hId--442118089%22%20id%3D%22toc-hId--442118089%22%20id%3D%22toc-hId--442118089%22%20id%3D%22toc-hId--442118089%22%20id%3D%22toc-hId--442118089%22%20id%3D%22toc-hId--442118089%22%20id%3D%22toc-hId--442121930%22%3EHere%20is%20my%20V1%20plan.%3C%2FH2%3E%0A%3CP%3E%3CSPAN%3EI%20already%20have%20a%20DNS%20server%20running%20on%20a%20Linux%20server%20in%20my%20on-prem%20environment.%26nbsp%3B%20I%20will%20schedule%20a%20job%20to%20get%20the%20external%20IP%20of%20my%20ISP%E2%80%99s%20router%20and%20send%20it%20to%20my%20zone%20in%20Azure.%26nbsp%3B%20Sounds%20easy%E2%80%A6.%26nbsp%3B%20Problem...%26nbsp%3B%20I%20don%E2%80%99t%20want%20to%20cache%20credentials%20for%20my%20job%20to%20connect%20to%20my%20zone%20and%20update%20the%20record.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-2045394744%22%20id%3D%22toc-hId-2045394744%22%20id%3D%22toc-hId-2045394744%22%20id%3D%22toc-hId-2045394744%22%20id%3D%22toc-hId-2045394744%22%20id%3D%22toc-hId-2045394744%22%20id%3D%22toc-hId-2045394744%22%20id%3D%22toc-hId-2045390903%22%3E%3CSPAN%3EPlan%20v1.1%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%3CSPAN%3EJust%20like%20v1%2C%20I%20will%20schedule%20a%20job%20on%20my%20Linux%20server%20to%20get%20the%20external%20IP%20of%20my%20ISP%E2%80%99s%20router.%26nbsp%3B%20BUT!!%20I%20code%20it%20to%20send%20it%20to%20an%20Azure%20Function%20via%20a%20webhook%20and%20let%20the%20function%20update%20my%20record%20in%20the%20DNS%20zone%20in%20Azure.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThat%E2%80%99s%20better...%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EI%20can%20secure%20the%20webhook%20with%20a%20function%20key%20and%20assign%20a%20managed%20identity%20to%20the%20function%2C%20so%20it%20only%20has%20rights%20to%20update%20the%20zone%20records%20but%20not%20change%20any%20of%20the%20other%20configuration%20and%20security.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThat%20should%20work!%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ELet%E2%80%99s%20go.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-237940281%22%20id%3D%22toc-hId-237940281%22%20id%3D%22toc-hId-237940281%22%20id%3D%22toc-hId-237940281%22%20id%3D%22toc-hId-237940281%22%20id%3D%22toc-hId-237940281%22%20id%3D%22toc-hId-237940281%22%20id%3D%22toc-hId-237936440%22%3E%3CSPAN%3ELinux%20Server%20setup%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%3CSPAN%3EI%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fpowershell%2Fscripting%2Finstall%2Finstalling-powershell-core-on-linux%3Fview%3Dpowershell-7.1%26amp%3BWT.mc_id%3Dmodinfra-30397-pierrer%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Einstalled%20PowerShell%20Core%20on%20the%20Linux%20box%3C%2FA%3E%20since%20I%E2%80%99m%20more%20familiar%20with%20PowerShell%20than%20I%20am%20with%20bash.%26nbsp%3B%20But%20I%20could%20have%20used%20either.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ECreated%20a%20simple%20script%20%26nbsp%3B%26nbsp%3BSome%20parts%20of%20the%20script%20like%20the%20Azure%20Function%20key%20and%20the%20Function%20webhook%20URL%20will%20be%20updated%20after%20we%20create%20the%20function.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EFunction%20Get-ExternalIP%20%7B%0A%20%20%20%20%24jsonContent%20%3D%20Invoke-WebRequest%20-UseBasicParsing%20-Uri%20%22http%3A%2F%2Fipinfo.io%2Fjson%22%20%7C%20ConvertFrom-Json%0A%0A%20%20%20%20%24pattern%20%3D%20%22%5E(%5B1-9%5D%7C%5B1-9%5D%5B0-9%5D%7C1%5B0-9%5D%5B0-9%5D%7C2%5B0-4%5D%5B0-9%5D%7C25%5B0-5%5D)(%5C.(%5B0-9%5D%7C%5B1-9%5D%5B0-9%5D%7C1%5B0-9%5D%5B0-9%5D%7C2%5B0-4%5D%5B0-9%5D%7C25%5B0-5%5D))%7B3%7D%24%22%0A%0A%20%20%20%20If%20(%24jsonContent.ip%20-match%20%24pattern)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20return%20%24jsonContent.ip%0A%20%20%20%20%7D%0A%20%20%20%20Else%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Write-Output%20%22False%22%0A%20%20%20%20%20%20%20%20Return%20%24false%0A%20%20%20%20%7D%0A%7D%0A%0A%24logfilepath%3D%20%22%2Fhome%2Fwiredcanuck%2Fpwsh.log%22%0AStart-Transcript%20-Path%20%24logfilepath%0A%0A%24datestamp%20%3D%20Get-Date%0Awrite-host%20%24datestamp%0A%0A%23%20get%20the%20current%20DNS%20record%20(Using%20Dig%20since%20Resolve-DnsName%20in%20the%20DnsClient%20module%20as%20not%20yet%20been%20ported%20to%20PowerShell%20Core)%0A%24EI%20%3D%20dig%20home.wiredcanuck.com%20%2Bshort%0AWrite-host%20%24EI.IPAddress%0A%0A%24IP%20%3D%20Get-ExternalIP%0A%0Aif%20(%24IP%20-ne%20%24EI)%0A%7B%0A%20%20%20%20%24WebHook%20%3D%20https%3A%2F%2Fhomednsupdate.azurewebsites.net%2Fapi%2FUpdateDNSRecord%0A%23%20the%20code%20below%20id%20the%20Function%20Key%0A%20%20%20%20%24code%20%3D%20%22XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX%0A%20%20%20%20%24name%20%3D%20%22home%22%0A%0A%20%20%20%20write-host%20%22External%20IP%20is%20%24IP%22%0A%0A%20%20%20%20%24URL%20%3D%20%24WebHook%2B%22%3Fcode%3D%22%2B%24code%2B%22%26amp%3Bname%3D%22%2B%24name%2B%22%26amp%3Bip%3D%22%2B%24IP%0A%0A%20%20%20%20write-host%20%22calling%20WebHook%22%0A%0A%20%20%20%20%24response%20%3D%20Invoke-WebRequest%20-UseBasicParsing%20%24URL%20-Method%20POST%0A%0A%20%20%20%20Write-host%20%24response%0A%0A%7D%0AElse%0A%7B%0A%20%20%20%20write-host%20%22Current%20IP%20(%24IP)%20is%20the%20same%20as%20the%20IP%20in%20the%20DNS%20record%20(%24EI).%20%20no%20update%20needed%22%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1569514182%22%20id%3D%22toc-hId--1569514182%22%20id%3D%22toc-hId--1569514182%22%20id%3D%22toc-hId--1569514182%22%20id%3D%22toc-hId--1569514182%22%20id%3D%22toc-hId--1569514182%22%20id%3D%22toc-hId--1569514182%22%20id%3D%22toc-hId--1569518023%22%3E%3CSPAN%3EAzure%20Function%20Setup%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%3CSPAN%3EIn%20the%20Azure%20portal%20I%20navigated%20to%20the%20Resource%20Group%20where%20my%20DNS%20Zone%20is%20lo0cation%20and%20created%20an%20Azure%20Function%20%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22dns4.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F285818iFC5BBCA2E7A94592%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22dns4.png%22%20alt%3D%22dns4.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EI%20ensured%20to%20create%20the%20function%20with%20the%20PowerShell%20Core%20stack%20and%20using%20a%20consumption%20(Serverless)%20plan%20type.%20%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22dns5.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F285819i4D0D071D62A9AD15%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22dns5.png%22%20alt%3D%22dns5.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%E2%80%83%3C%2FP%3E%0A%3CP%3EOnce%20the%20function%20was%20created%2C%20I%20navigated%20to%20the%20function%20App%20and%20clicked%20on%20the%20Functions%20menu%20item%20on%20the%20left%20of%20the%20screen.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22dns6.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F285820i5C1187361FE7E05F%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22dns6.png%22%20alt%3D%22dns6.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%E2%80%83%3C%2FP%3E%0A%3CP%3EAnd%20clicked%20the%20%E2%80%9C%2B%20Add%E2%80%9D%20selection%20on%20the%20next%20screen%20and%20fill%20in%20the%20details%20like%20Using%20the%20Portal%20for%20development%20(1)%2C%20%E2%80%9CHTTP%20Trigger%E2%80%9D%20from%20the%20template%20list%20(2)%2C%20a%20function%20name%20(3)%20and%20%E2%80%9CFunction%E2%80%9D%20as%20the%20authorization%20level.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22dns7.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F285821iB5B67EB934D184E5%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22dns7.png%22%20alt%3D%22dns7.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%E2%80%83%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EGoing%20back%20to%20the%20DNSUpdate1%20function%20screen%20I%20made%20sure%20to%20navigate%20to%20the%20%E2%80%9CIdentity%E2%80%9D%20section%20and%20turn%20on%20the%20system%20assigned%20identity.%20And%20save%20the%20changes%20and%20confirm%20on%20the%20next%20screen.%20That%20way%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Factive-directory%2Fmanaged-identities-azure-resources%2Foverview%3FWT.mc_id%3Dmodinfra-30397-pierrer%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Efunction%20has%20an%20identity%20we%20can%20use%20to%20restrict%20access%3C%2FA%3E.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22dns8.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F285822i27D28F941E99FA76%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22dns8.png%22%20alt%3D%22dns8.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%E2%80%83%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EWhen%20the%20changes%20are%20saved%2C%20you%20can%20click%20the%20Azure%20role%20assignments%20button%20to%20assign%20the%20proper%20rights.%26nbsp%3B%20In%20my%20case%20I%20only%20assigned%20%E2%80%9C%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Frole-based-access-control%2Frole-assignments-list-portal%3FWT.mc_id%3Dmodinfra-30397-pierrer%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EDNS%20Zone%20contributor%3C%2FA%3E%E2%80%9D%20only%20in%20the%20specific%20resource%20group%20in%20my%20subscription.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22dns9.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F285823i199911B8E9D4C0F7%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22dns9.png%22%20alt%3D%22dns9.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%E2%80%83%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ENow%20that%20my%20function%20is%20created%20and%20has%20the%20proper%20rights%2C%20we%20can%20create%20the%20PowerShell%20code%20in%20the%20%E2%80%9CCode%20%2B%20Test%E2%80%9D%20section%20and%20insert%20our%20code.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22dns10.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F285824i10EEFEBA90F649F6%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22dns10.png%22%20alt%3D%22dns10.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20code%20will%20capture%20the%20HTTP%20request%2C%20and%20the%20body%20of%20the%20request%20and%20process%20the%20information.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Eusing%20namespace%20System.Net%0A%0A%23%20Input%20bindings%20are%20passed%20in%20via%20param%20block.%0Aparam(%24Request%2C%20%24TriggerMetadata)%0A%0A%23%20Write%20to%20the%20Azure%20Functions%20log%20stream.%0AWrite-Host%20%22PowerShell%20HTTP%20trigger%20function%20processed%20a%20request.%22%0A%0A%23%20Interact%20with%20query%20parameters%20or%20the%20body%20of%20the%20request.%0A%0A%24RG%20%3D%20%22WiredCanuck%22%0A%24ZoneName%20%3D%20%22Wiredcanuck.com%22%0A%0A%24name%20%3D%20%24Request.Query.Name%0Aif%20(-not%20%24name)%20%7B%0A%20%20%20%20%24name%20%3D%20%24Request.Body.Name%0A%7D%0A%0A%24NewIP%20%3D%20%24Request.Query.IP%0Aif%20(-not%20%24NewIP)%20%7B%0A%20%20%20%20%24IP%20%3D%20%24Request.Body.IP%0A%7D%0A%0A%24RecordSet%20%3D%20Get-AzDnsRecordSet%20-ResourceGroupName%20%24RG%20-ZoneName%20%24ZoneName%20-Name%20%24name%20-RecordType%20A%0A%0Aif%20(-not%20%24RecordSet)%20%7B%0A%20%20%20%20%24RecordSet%20%3D%20New-AzDnsRecordSet%20-Name%20%24name%20-RecordType%20A%20-ZoneName%20%24ZoneName%20-ResourceGroupName%20%24RG%20-Ttl%203600%20-DnsRecords%20(New-AzDnsRecordConfig%20-IPv4Address%20%24NewIP)%0A%7D%0A%24OldIP%20%3D%20%24recordset.Records.Ipv4Address%0A%0Aif%20(%24NewIP%20-ne%20%24OldIP)%20%7B%0A%20%20%20%20%24body%20%3D%20%22DNS%20address%20%24name.%24ZoneName%20does%20not%20match.%20%24NewIP%20will%20be%20created%22%0A%20%20%20%20%24Records%20%3D%20%40()%0A%20%20%20%20%24Records%20%2B%3D%20New-AzDnsRecordConfig%20-IPv4Address%20%24NewIP%0A%20%20%20%20%24RecordSet%20%3D%20New-AzDnsRecordSet%20-Name%20%24name%20-RecordType%20A%20-ZoneName%20%24ZoneName%20-ResourceGroupName%20%24RG%20-Ttl%203600%20-DnsRecords%20%24Records%20-Confirm%3A%24False%20-Overwrite%0A%7D%0AElse%20%7B%0A%20%20%20%20%24body%20%3D%20%22Dynamic%20address%20(%24NewIP)%20and%20DNS%20address%20(%24OldIP)%20match.%20%20No%20update%20needed%22%0A%7D%0A%0A%23%20Associate%20values%20to%20output%20bindings%20by%20calling%20'Push-OutputBinding'.%0APush-OutputBinding%20-Name%20Response%20-Value%20(%5BHttpResponseContext%5D%40%7B%0A%20%20%20%20StatusCode%20%3D%20%5BHttpStatusCode%5D%3A%3AOK%0A%20%20%20%20Body%20%3D%20%24body%0A%7D)%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOnce%20the%20code%20is%20saved%2C%20I%20can%20click%20the%20%E2%80%9CGet%20function%20URL%E2%80%9D%20to%20get%20the%20Webhook%20address%20for%20the%20on-prem%20portion%20of%20this%20solution.%26nbsp%3B%20The%20URL%20will%20include%20the%20Default%20function%20key%20as%20highlighted%20below%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdnsupdate1.azurewebsites.net%2Fapi%2FHttpTrigger1%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdnsupdate1.azurewebsites.net%2Fapi%2FHttpTrigger1%3C%2FA%3E%3CFONT%20color%3D%22%23000000%22%3E%3CSTRONG%3E%3Fcode%3DFPX43K329H7PEeJPl68ZC8LZfYCwkDyKJElShadzPWSxtc3LQASlbA%3D%3D%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22dns11.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F285825i7CA5F247F92EEB1F%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22dns11.png%22%20alt%3D%22dns11.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EAnd%20now%2C%20every%2012%20hours%20(as%20per%20my%20CRON%20job)%20the%20IP%20address%20of%20my%20home%20network%20will%20be%20checked%20and%20updated%20if%20need%20be.%26nbsp%3B%20You%20can%20monitor%20the%20results%20in%20the%20monitor%20section%20and%20see%20all%20the%20invocation%20of%20the%20function.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22dns12.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F285826iAFF0736999004060%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22dns12.png%22%20alt%3D%22dns12.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId--1580017286%22%20id%3D%22toc-hId--1580017286%22%20id%3D%22toc-hId--1580017286%22%20id%3D%22toc-hId--1580017286%22%20id%3D%22toc-hId--1580017286%22%20id%3D%22toc-hId--1580017286%22%20id%3D%22toc-hId--1580017286%22%20id%3D%22toc-hId--1580021127%22%3E%3CSPAN%3EConclusion%3C%2FSPAN%3E%3C%2FH1%3E%0A%3CP%3E%3CSPAN%3EThis%20just%20proves%20that%20even%20if%20some%20functionalities%20don%E2%80%99t%20exist%20yet%2C%20you%20can%20leverage%20multiple%20Azure%20services%20and%20configuration%20to%20address%20on-prem%20issues%20efficiently%20and%20securely.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EAre%20you%20running%20into%20challenges%20that%20could%20be%20addressed%20with%20cloud%20services%20and%20a%20bit%20of%20imaginations%3F%26nbsp%3B%20Let%20me%20know.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ECheers!%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EPierre%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2411008%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EI%20want%20to%20access%20my%20%E2%80%9Con-prem%20lab%E2%80%9D%20environment%20from%20anywhere%20but%20my%20ISP%20cycles%20my%20IP%20address%20regularly.%26nbsp%3B%26nbsp%3B%3C%2FSPAN%3EI%20could%20always%20use%20a%20service%20like%20noip.com%20or%20dyndns%20but%20I%20already%20have%20a%20DNS%20zone%20managed%20in%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fdns%2Fdns-overview%2F%3FWT.mc_id%3Dmodinfra-30397-pierrer%22%20rel%3D%22noopener%20noreferrer%22%20target%3D%22_blank%22%3EAzure%20DNS%3C%2FA%3E%20and%20I%20thought%20it%20would%20be%20fun%20to%20figure%20out%20how%20to%20use%20it%20for%20my%20needs.%3C%2FP%3E%0A%3CP%3EYou%20can%20leverage%20multiple%20Azure%20services%20and%20configuration%20to%20address%20on-prem%20issues%20efficiently.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2411008%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20DNS%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20Function%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPierre%20Roman%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2416279%22%20slang%3D%22en-US%22%3ERe%3A%20Faking%20DDNS%20with%20Azure%20Services%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2416279%22%20slang%3D%22en-US%22%3E%3CP%3EI%20use%20something%20quite%20similar%20but%20imho%20simpler%20%3A%20the%20machine%2Frpi%2Frouter%20calling%20every%20XX%20minutes%20(or%20even%20better%20on%20the%20dhcp%20hooks%20of%20the%20router)%20an%20azure%20function%20with%20these%20inputs%20%3A%20the%20zone%20name%20and%20the%20recordset%20to%20update%20(and%20the%20function%20key%20of%20course%20to%20protect%20it).%20The%20function%20itself%20gets%20the%20the%20client%20IP%20originating%20the%20request%20so%20I%20don't%20have%20any%20script%20running%20locally%2C%20just%20a%20curl%20call%20to%20the%20function%20in%20my%20crontab%3C%2FP%3E%3C%2FLINGO-BODY%3E
Co-Authors
Version history
Last update:
‎Jun 02 2021 09:36 PM
Updated by: