DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Effective Secrets Management: Retrieving Secrets From Azure Key Vault With Powershell Script
  • Microsoft Azure Active Directory
  • Android Cloud Apps with Azure
  • Delivering Your Code to the Cloud With JFrog Artifactory and GitHub Actions

Trending

  • Event-Driven Microservices: How Kafka and RabbitMQ Power Scalable Systems
  • How to Create a Successful API Ecosystem
  • Apple and Anthropic Partner on AI-Powered Vibe-Coding Tool – Public Release TBD
  • Using Java Stream Gatherers To Improve Stateful Operations
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. How to Deploy Azure Function Apps With Powershell

How to Deploy Azure Function Apps With Powershell

Today, see a quick'n'dirty way to easily deploy your Azure Function Apps using Powershell.

By 
David Guida user avatar
David Guida
·
Nov. 24, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
11.5K Views

Join the DZone community and get the full member experience.

Join For Free

Hi All! Today I want to show a quick’n’dirty way to easily deploy your projects to Azure using Powershell.

I’ve been working a lot recently with Azure Functions and Web Apps. And of course, each time I’m confident with my code, I want to see it deployed on the Cloud.

Of course in an ideal world, we all would have a nice CI/CD pipeline, potentially on Azure DevOps. It might happen, however, that for one reason or another, you can only get up to CI, without being able to deploy.

So the only option you have is to manually handle deployments, most likely from your local machine. But what happens if you have to deploy it to multiple destinations?

In my case, for example, I had to deploy a Function App and a Web App to multiple client subscriptions. Of course, you can always do this directly from Visual Studio, but it still feels like a lot of manual work.

What if instead you can have a very nice script that handles all the grunt work for you?

Moreover, you could potentially reuse it when you finally manage to get to the Continuous Deployment part.

So, the first step is to create the Release Artifact. I am assuming, of course, that you’ve run already your Tests and everything went fine.

My weapon of choice for these scripts today, will be Powershell:

PowerShell
 




x
25


 
1
function publish{
2
    param(
3
        $projectName        
4
    )
5
 
          
6
    $projectPath="src/$($projectName)/$($projectName).csproj"
7
    $publishDestPath="publish/" + [guid]::NewGuid().ToString()
8
 
          
9
    log "publishing project '$($projectName)' in folder '$($publishDestPath)' ..." 
10
    dotnet publish $projectPath -c Release -o $publishDestPath
11
 
          
12
    $zipArchiveFullPath="$($publishDestPath).Zip"
13
    log "creating zip archive '$($zipArchiveFullPath)'"
14
    $compress = @{
15
        Path = $publishDestPath + "/*"
16
        CompressionLevel = "Fastest"
17
        DestinationPath = $zipArchiveFullPath
18
    }
19
    Compress-Archive @compress
20
 
          
21
    log "cleaning up ..."
22
    Remove-Item -path "$($publishDestPath)" -recurse
23
 
          
24
    return $zipArchiveFullPath
25
}


Here I’m building a temporary path using a GUID and calling dotnet publish to compile the Project and output the binaries to it. Then we generate a Zip archive and get rid of the publish folder.

The log function is just a simple wrapper over Write-Host, I just added some fancy colors to highlight the text:

PowerShell
 




xxxxxxxxxx
1


 
1
function log{
2
    param(
3
        $text
4
    )
5
 
          
6
    write-host $text -ForegroundColor Yellow -BackgroundColor DarkGreen
7
}


Now that we have our Artifact, the next step is to deploy it to Azure. If you, like me, are working with Azure Functions, this is the script for you:

PowerShell
 




xxxxxxxxxx
1
11


 
1
function deploy{
2
    param(
3
        $zipArchiveFullPath,
4
        $subscription,
5
        $resourceGroup,        
6
        $appName
7
    )    
8
 
          
9
    log "deploying '$($appName)' to Resource Group '$($resourceGroup)' in Subscription '$($subscription)' from zip '$($zipArchiveFullPath)' ..."
10
    az functionapp deployment source config-zip -g "$($resourceGroup)" -n "$($appName)" --src "$($zipArchiveFullPath)" --subscription "$($subscription)"   
11
}


It simply takes the full path to the zip archive we produced before and the name of the destination Azure Subscription, Resource Group and Application. Easy peasy.

Now, I’ve found particularly handy to set some basic application settings, right after the deployment. For this, I keep a simple JSON file with key/value pairs and deploy it using this script:

PowerShell
 




xxxxxxxxxx
1
10


 
1
function setConfig{
2
    param(
3
        $subscription,
4
        $resourceGroup,        
5
        $appName,
6
        $configPath
7
    )
8
    log "updating application config..."
9
    az functionapp config appsettings set --name "$($appName)" --resource-group "$($resourceGroup)" --subscription "$($subscription)" --settings @$configPath
10
}


The config file can be something like this:

JSON
 




xxxxxxxxxx
1


 
1
{
2
  "FUNCTIONS_WORKER_RUNTIME": "dotnet",  
3
  "ASPNETCORE_ENVIRONMENT": "DEV",
4
  "Foo": "bar"
5
}


The last step is to put everything together and call it. I would suggest creating a separate script with all the previous functions. We can use it as a “library” and if we’re lucky enough, it won’t even change much when we move to CD.

For our local deployment script we will instead need two more helper functions. The first one will take care of the Artifact:

PowerShell
 




xxxxxxxxxx
1
10


 
1
function createArtifact {
2
    param(
3
        $appName
4
    )
5
    $zipPath = publish $appName
6
    if ($zipPath -is [array]) {
7
        $zipPath = $zipPath[$zipPath.Length - 1]
8
    }
9
    return $zipPath
10
}


We can’t unfortunately call directly the publish function because seems that the output from the dotnet publish command will mess a bit with the return value. So we’ll need to do some magic tricks, but not that much.

Then we can send the artifact to the cloud:

PowerShell
 




xxxxxxxxxx
1
15


 
1
function deployInstance {
2
    param(      
3
        $zipPath,  
4
        $subscription,
5
        $resourceGroup,        
6
        $appName,
7
        $configPath
8
    )
9
 
          
10
    deploy $zipPath $subscription $resourceGroup $appName
11
 
          
12
    if(![string]::IsNullOrEmpty($configPath)){
13
        setConfig $subscription $resourceGroup $appName $configPath
14
    }
15
}


If you remember, at the top of the post I said that we might have to deploy the same artifact to multiple destinations. Now that we have everything in place, all we have to do is just put the pieces together:

PowerShell
 




xxxxxxxxxx
1


 
1
$zipPath = createArtifact "MyAwesomeProject" 
2
deployInstance $zipPath "MyFirstSubscription" "MyFirstResourceGroup" "MyAwesomeProject1" "DEV.settings.json"
3
deployInstance $zipPath "MySecondSubscription" "MySecondResourceGroup" "MyAwesomeProject2" "DEV.settings.json"
4
deployInstance $zipPath "MyThirdSubscription" "MyThirdResourceGroup" "MyAwesomeProject3" "DEV.settings.json"


…and so on and so forth. I think you got the idea.

This should cover all the basic steps to deploy your code to Azure from your machine. Most of these scripts can be adapted quite easily to be executed on Azure DevOps. And that should be, ultimately, your goal: don't let this task sit on you for too long! They will create unnecessary clutter and noise, distracting from the real project!

PowerShell azure app

Opinions expressed by DZone contributors are their own.

Related

  • Effective Secrets Management: Retrieving Secrets From Azure Key Vault With Powershell Script
  • Microsoft Azure Active Directory
  • Android Cloud Apps with Azure
  • Delivering Your Code to the Cloud With JFrog Artifactory and GitHub Actions

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!