<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Saleh Albuga</title>
    <description>The latest articles on DEV Community by Saleh Albuga (@saleh).</description>
    <link>https://dev.to/saleh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F286867%2F715f8755-e39f-42cb-805e-d6e699d4fc40.jpeg</url>
      <title>DEV Community: Saleh Albuga</title>
      <link>https://dev.to/saleh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/saleh"/>
    <language>en</language>
    <item>
      <title>Azure Functions in Swift: From Code to Azure</title>
      <dc:creator>Saleh Albuga</dc:creator>
      <pubDate>Thu, 17 Sep 2020 22:00:46 +0000</pubDate>
      <link>https://dev.to/saleh/azure-functions-in-swift-from-code-to-azure-p24</link>
      <guid>https://dev.to/saleh/azure-functions-in-swift-from-code-to-azure-p24</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This article is part of &lt;a href="https://aka.ms/ServerlessSeptember2020" rel="noopener noreferrer"&gt;#ServerlessSeptember&lt;/a&gt;. You'll find other helpful articles, detailed tutorials, and videos in this all-things-Serverless content collection. New articles from community members and cloud advocates are published every week from Monday to Thursday through September.&lt;/p&gt;

&lt;p&gt;Find out more about how Microsoft Azure enables your Serverless functions at &lt;a href="https://docs.microsoft.com/azure/azure-functions/?WT.mc_id=servsept20-devto-cxaall" rel="noopener noreferrer"&gt;https://docs.microsoft.com/azure/azure-functions/&lt;/a&gt;. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Serverless is changing the way we design and implement back ends. Whether you already use Server Side Swift or thinking of a simple way to write lightweight APIs for your iOS app, Swift  Azure Functions definitely have a place in your stack! &lt;/p&gt;

&lt;p&gt;A couple of months ago, I wrote an &lt;a href="https://medium.com/macoclock/write-publish-azure-function-in-swift-using-custom-handlers-35d937582db7" rel="noopener noreferrer"&gt;article&lt;/a&gt; to take your through developing and publishing Azure Functions in Swift using a custom handler. &lt;a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-custom-handlers" rel="noopener noreferrer"&gt;Custom handlers&lt;/a&gt; are a new way to support custom runtimes for Azure Function, using an HTTP worker.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://github.com/SalehAlbuga/azure-functions-swift" rel="noopener noreferrer"&gt;Azure Functions Swift worker&lt;/a&gt; is a framework that supports both worker implementations, the classic one and custom handler.&lt;/p&gt;

&lt;p&gt;In this article, I'm going to take you through the different options of building and publishing Swift Azure Functions, covering developing on macOS and Linux and deploying as Container Function app or hosting in a Consumption plan.&lt;/p&gt;

&lt;h1&gt;
  
  
  First Things First
&lt;/h1&gt;

&lt;p&gt;Here is the requirements checklist&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Swift 5.2 or later or Xcode 11 or later on macOS&lt;br&gt;
Swift installation: &lt;a href="https://swift.org/getting-started/#installing-swift" rel="noopener noreferrer"&gt;https://swift.org/getting-started/#installing-swift&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Azure &lt;a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local#install-the-azure-functions-core-tools" rel="noopener noreferrer"&gt;Functions Core Tools&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Swift Functions Tools&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Just like Core Tools, Swift Functions Tools (swiftfunc) make Swift functions development easier and much more convenient. It helps in creating projects, functions, running them locally and publishing to Azure.&lt;/p&gt;

&lt;p&gt;On &lt;strong&gt;macOS&lt;/strong&gt;, install it from Homebrew 🍺&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew install salehalbuga/formulae/swift-func
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;on &lt;strong&gt;Linux&lt;/strong&gt;, clone the repo the tools repo and install&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone https://github.com/SalehAlbuga/azure-functions-swift-tools
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;make install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;If you already have Swift Functions Tools installed, make sure you have the latest version as the project is being actively updated. on macOS run &lt;code&gt;brew upgrade salehalbuga/formulae/swift-func&lt;/code&gt; and on Linux, repeat the installation step.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Which way to go?
&lt;/h1&gt;

&lt;p&gt;As mentioned earlier, there are a couple of development and deployment options.&lt;/p&gt;

&lt;h3&gt;
  
  
  Development Options
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Classic worker
&lt;/h4&gt;

&lt;p&gt;In this mode, writing Swift functions is similar to writing JavaScript or Python Azure Functions. You'll use the &lt;a href="https://github.com/SalehAlbuga/azure-functions-swift#traditional-worker-classic" rel="noopener noreferrer"&gt;defined binding types&lt;/a&gt;. &lt;br&gt;
Take a look at the function below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;Foundation&lt;/span&gt;
&lt;span class="kd"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;AzureFunctions&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;HttpFunction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Function&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;required&lt;/span&gt; &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"HttpFunction"&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;trigger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;HttpRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"req"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;HttpRequest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;inout&lt;/span&gt; &lt;span class="kt"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;@escaping&lt;/span&gt; &lt;span class="n"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throws&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;HttpResponse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;bodyObj&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="kt"&gt;JSONSerialization&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;jsonObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[])&lt;/span&gt; &lt;span class="k"&gt;as?&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bodyObj&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;as?&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;??&lt;/span&gt; &lt;span class="s"&gt;"buddy"&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;!"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;using&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;utf8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The trigger and bindings are set in the constructor and the function logic is located in the &lt;code&gt;exec&lt;/code&gt; method. Outputs are set in &lt;code&gt;context&lt;/code&gt; object.&lt;/p&gt;

&lt;h4&gt;
  
  
  Custom handler
&lt;/h4&gt;

&lt;p&gt;As &lt;a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-custom-handlers" rel="noopener noreferrer"&gt;docs&lt;/a&gt; defines them:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Custom handlers are lightweight web servers that receive events from the Functions host. Any language that supports HTTP primitives can implement a custom handler.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Take a look at the function below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;Foundation&lt;/span&gt;
&lt;span class="kd"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;AzureFunctions&lt;/span&gt;
&lt;span class="kd"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;Vapor&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;TimerFunction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Function&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;required&lt;/span&gt; &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"TimerFunction"&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;functionJsonBindings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
            &lt;span class="p"&gt;[&lt;/span&gt;
                &lt;span class="p"&gt;[&lt;/span&gt;
                &lt;span class="s"&gt;"type"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"timerTrigger"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="s"&gt;"name"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"myTimer"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="s"&gt;"direction"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"in"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="s"&gt;"schedule"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"*/5 * * * * *"&lt;/span&gt;
                &lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="c1"&gt;//or&lt;/span&gt;
        &lt;span class="c1"&gt;//self.trigger = TimerTrigger(name: "myTimer", schedule: "*/5 * * * * *")&lt;/span&gt;

        &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="kt"&gt;PathComponent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;stringLiteral&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)],&lt;/span&gt; &lt;span class="nv"&gt;use&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;req&lt;/span&gt;&lt;span class="p"&gt;:))&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;req&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;InvocationResponse&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;InvocationResponse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendLog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Its is time!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this mode, as you can see in the function initializer, you can set the trigger/bindings in JSON (Dictionary) format in &lt;code&gt;functionJsonBindings&lt;/code&gt; property, just like you’d do manually when you create a function.json.&lt;/p&gt;

&lt;p&gt;You can also use the framework’s defined binding types by setting the other properties (trigger, inputBindings, and outputBindings)&lt;/p&gt;

&lt;p&gt;The implmentation currently uses Vapor HTTP server. The framework provides the function invocation Request and Response models used by Azure Functions host when working with customer handlers. The models conform to &lt;code&gt;Content&lt;/code&gt; protocol from Vapor.&lt;/p&gt;

&lt;p&gt;All you need to do is to initialize an &lt;code&gt;InvocationResponse&lt;/code&gt; instance and set the outputs and return it!&lt;/p&gt;

&lt;h3&gt;
  
  
  Deployment options
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Docker Container: where the Swift Functions are deployed in a custom Docker container. Container Functions can be hosted on an App Service Plan or a Premium plan.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Don't worry, the Dockerfile is provided by Swift Function CLI tools when the project is created. You'll only need to build the image :]&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Hosted on a Consumption Plan: here the functions will be deployed to Azure as a zip package, not in a container.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Swift Functions Tools
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;swiftfunc&lt;/code&gt; is going to be your friend, if you’re familiar with Azure Functions Core Tools then you're there!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;init&lt;/strong&gt;: create Functions projects&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;new&lt;/strong&gt;: create functions from templates&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;run&lt;/strong&gt;: run a Functions project locally &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;publish&lt;/strong&gt;: Publish to Azure (for hosting on Consumption plans)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's write some functions!&lt;/p&gt;

&lt;h1&gt;
  
  
  Creating a Functions project
&lt;/h1&gt;

&lt;p&gt;In a terminal run the following to create a new project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;swiftfunc init myFunctionApp &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;-hw&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note the option &lt;code&gt;-hw&lt;/code&gt; or &lt;code&gt;--http-worker&lt;/code&gt; to specify that the new project will be created as a Custom Handler project. It's optional. Without it the project will run in the classic worker mode.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Your new project has the following folder structure&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fr9axqcg40ai3rxvvc43o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fr9axqcg40ai3rxvvc43o.png" alt="project folder structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is basically a SwiftPM project with the Swift Azure Functions framework dependency and a couple of extra files for the purpose of it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd&lt;/code&gt; into the project folder and create a new HTTP function by running the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;swiftfunc new http &lt;span class="nt"&gt;-n&lt;/span&gt; helloWorld &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;-hw&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;-hw&lt;/code&gt; option usage is the same as above. Projects created to run in Custom Handler mode, need to have this option passed to the &lt;strong&gt;new&lt;/strong&gt; function command as well&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now you have an HTTP triggered function&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F51bsjmfpk6kba52jks1f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F51bsjmfpk6kba52jks1f.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open the project in Xcode (by opening Package.swift) and take a look at the code. &lt;/p&gt;

&lt;h1&gt;
  
  
  Running the project locally
&lt;/h1&gt;

&lt;p&gt;To run the project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;swiftfunc run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Swift Functions Tools will compile the code, export the project and start the Azure Functions Host for you (as if you were running &lt;code&gt;func start&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;Click on the link from the Host output to navigate to your function&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;a href="http://localhost:7071/api/helloWorld" rel="noopener noreferrer"&gt;http://localhost:7071/api/helloWorld&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Pass a name param in query string or send a POST request with a name param in the body!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Congrats! You created your first Azure Function app in Swift. Let’s deploy that!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Deploying to Azure ☁️
&lt;/h1&gt;

&lt;p&gt;This is the fun part where you get to see your code running on Azure! I'm going to take you through both deployment options: hosting on Consumption plan and Container Functions!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For more info about hosting options &lt;a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-scale" rel="noopener noreferrer"&gt;https://docs.microsoft.com/en-us/azure/azure-functions/functions-scale&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Hosting on Consumption Plan
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Linux
&lt;/h4&gt;

&lt;p&gt;On Linux, deploying to an Azure Function app hosted on a consumption plan is easy. (macOS requires an extra step described in the next section)&lt;/p&gt;

&lt;p&gt;Create a new project in the custom handler mode as you did above &lt;/p&gt;

&lt;p&gt;Open Azure Portal (Get a &lt;a href="https://azure.microsoft.com/en-us/free/" rel="noopener noreferrer"&gt;free Azure subscription&lt;/a&gt; if you don't have one yet) &lt;/p&gt;

&lt;p&gt;Click on &lt;strong&gt;Create a resource&lt;/strong&gt; and search for &lt;strong&gt;Function App&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3struxukcnto8psakn9j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3struxukcnto8psakn9j.png" alt="New Function App - Basics Tab"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Fill the basic required info, &lt;strong&gt;Resource Group&lt;/strong&gt;, &lt;strong&gt;App Name&lt;/strong&gt;, &lt;strong&gt;Region&lt;/strong&gt; and select &lt;strong&gt;Code&lt;/strong&gt; for &lt;strong&gt;Publish&lt;/strong&gt;.&lt;br&gt;
Selecting a &lt;strong&gt;Runtime Stack&lt;/strong&gt; is required at this point, select &lt;strong&gt;Node.js&lt;/strong&gt; for now. It will be changed later when you deploy. &lt;/p&gt;

&lt;p&gt;Click &lt;strong&gt;Next&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fh5oaqtey2pl4ui0zfg45.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fh5oaqtey2pl4ui0zfg45.png" alt="New Function App - Hosting Tab"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;Hosting&lt;/strong&gt; tab, make sure &lt;strong&gt;Linux&lt;/strong&gt; is the selected OS and change &lt;strong&gt;Plan type&lt;/strong&gt; to &lt;strong&gt;Consumption (Serverless)&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Click &lt;strong&gt;Review + create&lt;/strong&gt; and then &lt;strong&gt;Create&lt;/strong&gt; after the validation is done.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Great, now you have a new Function App hosted in a Consumption Plan!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Next, we need to add an App Setting to the app, to append the directory of the shipped Swift libraries to shared library loader. These are shipped by Swift Functions Tool while preparing the publish package.&lt;br&gt;
From the &lt;strong&gt;Configuration&lt;/strong&gt; blade, click on &lt;strong&gt;New application setting&lt;/strong&gt; and add the following setting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/site/wwwroot/workers/swift/lib/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's &lt;code&gt;LD_LIBRARY_PATH&lt;/code&gt; for Name and &lt;code&gt;$LD_LIBRARY_PATH:/home/site/wwwroot/workers/swift/lib/&lt;/code&gt; is the Value&lt;/p&gt;

&lt;p&gt;After adding it, click &lt;strong&gt;Save&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Finally, the fun part (for real this time!). In the project directory run the command below to login to Azure from Azure CLI:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;az login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll get the following output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;To sign &lt;span class="k"&gt;in&lt;/span&gt;, use a web browser to open the page https://microsoft.com/devicelogin and enter the code &lt;span class="o"&gt;[&lt;/span&gt;0000000] to authenticate.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Navigate to &lt;a href="https://microsoft.com/devicelogin" rel="noopener noreferrer"&gt;https://microsoft.com/devicelogin&lt;/a&gt;, enter the provided code and select your account when prompted.&lt;/p&gt;

&lt;p&gt;When Azure CLI finishes loading your subscription(s) info, run the below to publish the app&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;swiftfunc publish myswiftfunctions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;swiftfunc publish&lt;/code&gt; is going to compile, export and publish your Swift Functions project.&lt;/p&gt;

&lt;p&gt;When it finishes, Function Core Tools output will have a link to your function, click on it and try it!&lt;/p&gt;

&lt;h4&gt;
  
  
  macOS
&lt;/h4&gt;

&lt;p&gt;Publishing a project from macOS in this case requires an extra step.&lt;br&gt;
Swift Functions are hosted on Linux on Azure, if you're using a Linux machine for development, then the deployment process is pretty straightforward as you've seen. Because we're shipping the same Linux Swift libraries installed on the machine. (Why? Because of Swift's ABI compatibility status).&lt;br&gt;
But this won't work from macOS as the installed libraries are macOS binaries.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For this section, you will need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.docker.com/products/docker-desktop" rel="noopener noreferrer"&gt;Docker Desktop&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://code.visualstudio.com" rel="noopener noreferrer"&gt;VSCode&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;To solve this, I have created a customized VSCode Dev Container! &lt;br&gt;
Dev Containers provide development environments inside containers! In this case, the Swift Functions dev container is going to be the environment that you're going to deploy from.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you haven't read about Visual Studio Code &lt;a href="https://code.visualstudio.com/docs/remote/containers" rel="noopener noreferrer"&gt;Development Containers&lt;/a&gt; yet, you're missing a lot! Do it now!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I'll take you step-by-step.&lt;/p&gt;

&lt;p&gt;As described above, create a Functions project but this time passing the &lt;code&gt;-dc&lt;/code&gt; option as shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;swiftfunc init myFunctionApp &lt;span class="nt"&gt;-hw&lt;/span&gt; &lt;span class="nt"&gt;-dc&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;-dc&lt;/code&gt; or &lt;code&gt;--dev-container&lt;/code&gt; option tells swiftfunc to add the dev container folder to your project&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Create an HTTP function and open the project folder is VSCode and install the &lt;a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers" rel="noopener noreferrer"&gt;Remote - Containers&lt;/a&gt; extension &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvlspodwmx4ee05bkdw21.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvlspodwmx4ee05bkdw21.png" alt="Remote - Containers Extension"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Restart VSCode if needed and press &lt;strong&gt;Command-Shift-P&lt;/strong&gt;, search for and select &lt;strong&gt;Remote-Containers: Reopen in Container&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;VSCode is going to build the container and connect to it. The Swift Functions dev container is going to have the current project directory content and all required dependencies (Core Tools, Azure CLI, Swift Functions Tools, etc). Once ready, add a &lt;strong&gt;New Terminal&lt;/strong&gt; in VSCode, and publish the project with the same steps described in the previous section (Linux):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Login from Azure CLI&lt;/li&gt;
&lt;li&gt;Create a Function App on Azure&lt;/li&gt;
&lt;li&gt;Add the &lt;code&gt;LD_LIBRARY_PATH&lt;/code&gt; App Setting with the this value &lt;code&gt;$LD_LIBRARY_PATH:/home/site/wwwroot/workers/swift/lib/&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Run the swiftfunc publish command!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Container Functions
&lt;/h3&gt;

&lt;p&gt;The other option to deploy Swift Functions is in a Container.&lt;br&gt;
Create your project and functions and then create a Function App on Azure with &lt;strong&gt;Publish&lt;/strong&gt; set to &lt;strong&gt;Container&lt;/strong&gt; and &lt;strong&gt;Plan type&lt;/strong&gt; set to &lt;strong&gt;App Service Plan&lt;/strong&gt; or &lt;strong&gt;Premium&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F6wiz2yowq26dqfwwzg1q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F6wiz2yowq26dqfwwzg1q.png" alt="Function App - Basics Tab"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fa9nruqhzrd82ahbtodrl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fa9nruqhzrd82ahbtodrl.png" alt="Function App - Hosting Tab"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At the this point, there are 2 ways to build the image and deploy it.&lt;br&gt;
First, using the &lt;a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=macos%2Ccsharp%2Cbash#deploy-custom-container" rel="noopener noreferrer"&gt;deploy command&lt;/a&gt; from Azure Functions Core Tools&lt;/p&gt;

&lt;p&gt;The second one is the 'traditional' one, where you'll build the image, push it to a registry and set it in the Function App configuration. Let's go quickly over the steps.&lt;/p&gt;

&lt;p&gt;The framework provides you with all you need to deploy a Container Function. The provided Dockerfile is ready to go! &lt;/p&gt;

&lt;p&gt;Build the image&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker build &lt;span class="nt"&gt;-t&lt;/span&gt; &amp;lt;dockerHubNameOrRegistryUrl&amp;gt;/myswiftfunctions:v1 &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Push it to a registry of your choice (DockerHub or &lt;a href="https://azure.microsoft.com/en-us/services/container-registry/" rel="noopener noreferrer"&gt;Azure Container Registry&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;bash&lt;br&gt;
docker push &amp;lt;dockerHubNameOrRegistryUrl&amp;gt;/myswiftfunctions&lt;br&gt;
&lt;/code&gt;`&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can deploy a prebuilt sample to your Azure account using the link below, if you don’t have Docker installed!&lt;br&gt;
https//aka.ms/SwiftFunc-Deploy&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Open the &lt;strong&gt;Container Settings&lt;/strong&gt; blade&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3jy19armacgwhrlfq78i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3jy19armacgwhrlfq78i.png" alt="Container Settings blade"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Choose your &lt;strong&gt;image source&lt;/strong&gt; and enter the image &lt;strong&gt;name&lt;/strong&gt; and &lt;strong&gt;tag&lt;/strong&gt; and click &lt;strong&gt;Save&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once the Function App finishes pulling the image and starting the container (you can check the status from the logs in the blade in the screenshot above). You can see your function in the &lt;strong&gt;Functions&lt;/strong&gt; blade&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F31p35dxh200yd0uudamb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F31p35dxh200yd0uudamb.png" alt="Functions Blade"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on it and navigate to Code + Test blade. From the dropdown list you can see the files of your function, function.json and the function code file (helloWorld.swift in this example)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdhaps2aza8pi40ftjt5w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdhaps2aza8pi40ftjt5w.png" alt="Code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on &lt;strong&gt;Get function URL&lt;/strong&gt;, copy it and try it!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Now you've seen all options of developing and deploying Azure Function in Swift&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Amazing! You’ve made it this far! 💪 This project is being actively maintained and updated. For any issues or bug reports, please feel free to file an issue on &lt;a href="https://github.com/SalehAlbuga/azure-functions-swift" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;. &lt;br&gt;
Don’t forget to join the Azure Functions &lt;a href="http://discord.gg/6rDzSuM" rel="noopener noreferrer"&gt;Discord server&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>azure</category>
      <category>serverless</category>
      <category>swift</category>
      <category>cloud</category>
    </item>
    <item>
      <title>Writing an Azure Function, in Swift!</title>
      <dc:creator>Saleh Albuga</dc:creator>
      <pubDate>Sat, 07 Dec 2019 15:04:16 +0000</pubDate>
      <link>https://dev.to/saleh/writing-an-azure-function-in-swift-2np2</link>
      <guid>https://dev.to/saleh/writing-an-azure-function-in-swift-2np2</guid>
      <description>&lt;p&gt;Server-Side Swift is rapidly evolving more than ever! Server-Side Swift opens exciting new possibilities to Swift developers. Things that weren't possible not long ago! Frameworks like Kitura and Vapor expose the amazing full power of Swift. Being able to have Swift from end to end can be game-changing for Swift developers and even companies!&lt;/p&gt;

&lt;p&gt;A couple of months ago I started working on bringing Swift support to Azure Function and here I'll take you through creating your first Function!&lt;/p&gt;

&lt;h2&gt;
  
  
  Checklist
&lt;/h2&gt;

&lt;p&gt;There are a couple of requirements to get started. If you have Xcode installed and you already work with Azure Functions on your mac, then you're almost ready!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ macOS 10.13 or later&lt;/li&gt;
&lt;li&gt;✅ Xcode 11 &amp;amp; Swift 5 or later&lt;/li&gt;
&lt;li&gt;✅ &lt;a href="https://dotnet.microsoft.com/download/dotnet-core/2.2"&gt;.NET Core SDK 2.2&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;✅ &lt;a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local#install-the-azure-functions-core-tools"&gt;Azure Functions Core Tools&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;✅ Swift Functions tools, you can get them from Homebrew 🍺:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew &lt;span class="nb"&gt;install &lt;/span&gt;salehalbuga/formulae/swift-func
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;You're all set! Let's create the function!&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Creating a new Swift Functions project
&lt;/h3&gt;

&lt;p&gt;In terminal run the following to create a new project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;swiftfunc init myFunctionsProj
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're used to using Functions Core Tools, Swift Function Tools are pretty similar. The &lt;code&gt;init&lt;/code&gt; command we ran above is similar to that of the Core Tools, but it's going to create a Swift Functions Project! You're going to get the follow folder structure:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kJQkczeW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/icni2hiqy7knzgqij98r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kJQkczeW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/icni2hiqy7knzgqij98r.png" alt="new project!" width="608" height="424"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The project is basically a SwiftPM project with a couple of extra files for the purpose of Azure Functions framework.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;functions&lt;/code&gt; folder is going to be the home of your Functions! &lt;/p&gt;

&lt;h3&gt;
  
  
  Let's create a simple HTTP Function!
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;cd&lt;/code&gt; to the new project folder and run the following command to create a new HTTP Function named &lt;code&gt;hello&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;swiftfunc new http &lt;span class="nt"&gt;-n&lt;/span&gt; hello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the created &lt;code&gt;hello.swift&lt;/code&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PGj8_k1B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/p6j3s2fpo3u57x07yam1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PGj8_k1B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/p6j3s2fpo3u57x07yam1.png" alt="Hello Function file" width="606" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is optional but let's generate an Xcode project using SwiftPM for easier development.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;swift package generate-xcodeproj
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Open the Xcode project and let's examine the code of the Function we created
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;//&lt;/span&gt;
&lt;span class="c1"&gt;//  hello.swift&lt;/span&gt;
&lt;span class="c1"&gt;//  myFunctionsProj&lt;/span&gt;
&lt;span class="c1"&gt;//&lt;/span&gt;
&lt;span class="c1"&gt;//  Created on 07-12-19.&lt;/span&gt;
&lt;span class="c1"&gt;//&lt;/span&gt;

&lt;span class="kd"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;Foundation&lt;/span&gt;
&lt;span class="kd"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;AzureFunctions&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nv"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Function&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;required&lt;/span&gt; &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"hello"&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;trigger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;HttpRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"req"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;methods&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"GET"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"POST"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;HttpRequest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;inout&lt;/span&gt; &lt;span class="kt"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;@escaping&lt;/span&gt; &lt;span class="n"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throws&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Function executing!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;HttpResponse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;bodyObj&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="kt"&gt;JSONSerialization&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;jsonObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[])&lt;/span&gt; &lt;span class="k"&gt;as?&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bodyObj&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;as?&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; 
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;??&lt;/span&gt; &lt;span class="s"&gt;"buddy"&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;!"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;using&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;utf8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This an HTTP-triggered function, as you can see in the constructor. &lt;br&gt;
&lt;code&gt;exec&lt;/code&gt; is the method that's going to be called when the Function is triggered. It passes the triggering http request as the framework type &lt;code&gt;HttpRequest&lt;/code&gt; in the first parameter.&lt;/p&gt;

&lt;p&gt;The sample code searches for a &lt;em&gt;name&lt;/em&gt; parameter value in the request body or the query string, and sets it as text in the response. It falls back to &lt;em&gt;"buddy"&lt;/em&gt; if it doesn't exist anywhere.&lt;/p&gt;
&lt;h3&gt;
  
  
  Running our new Function locally
&lt;/h3&gt;

&lt;p&gt;In terminal, run the following in the project directory to run your Swift Functions project locally. It will compile the code and start the host for you &lt;em&gt;(as if you were running &lt;code&gt;func host start&lt;/code&gt;)&lt;/em&gt;. The host output should show you the URL of &lt;code&gt;hello&lt;/code&gt; function created above. Click on it to run the function and see output!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;swiftfunc run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the compilation is done, you should see the Functions Host output&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aLypVUQ0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/r5ejpm1ptmd2l8dp474o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aLypVUQ0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/r5ejpm1ptmd2l8dp474o.png" alt="Alt Text" width="800" height="250"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on the green function link and pass a &lt;code&gt;name&lt;/code&gt; value in query string!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You've run your first Swift Azure Function locally! ✅💃💃&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Deploying to Azure ☁️
&lt;/h3&gt;

&lt;p&gt;Currently there's no one magical command to deploy, you'll need to build the docker image (from the provided Dockerfile), push to a registry and set it in the Container Settings of the Function App.&lt;/p&gt;

&lt;p&gt;Build the image&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker build &lt;span class="nt"&gt;-t&lt;/span&gt; &amp;lt;imageTag&amp;gt; &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then push it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker push &amp;lt;imageTag&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;a href="https://portal.azure.com"&gt;Azure portal&lt;/a&gt;, create a new Function App with &lt;strong&gt;Docker Container&lt;/strong&gt; as the Publish option. Under Hosting options make sure Linux is selected as OS.&lt;/p&gt;

&lt;p&gt;Once the app is created or in any existing Container Function App, under &lt;strong&gt;Platform Features&lt;/strong&gt;, select &lt;strong&gt;Container settings&lt;/strong&gt; and set the registry and select image you pushed.&lt;/p&gt;

&lt;p&gt;You've made it this far! Amazing!💪 If you like the project, give it a 🌟 on &lt;a href="https://github.com/SalehAlbuga/azure-functions-swift"&gt;GitHub&lt;/a&gt;.&lt;br&gt;
This is project is an early stage and is being actively maintained and updated. For any issues or bug reports, please feel free to file an issue on &lt;a href="https://github.com/SalehAlbuga/azure-functions-swift"&gt;GitHub&lt;/a&gt;. &lt;br&gt;
Don't forget to check the docs &lt;a href="https://swiftfunc.developerhub.io"&gt;Here&lt;/a&gt;📚&lt;/p&gt;

</description>
      <category>azure</category>
      <category>serverless</category>
      <category>swift</category>
    </item>
  </channel>
</rss>
