Let’s setup an end-to-end Serverless SPA on Azure.
This is a continuation. For the first post click here
Things to keep in mind:
- Authentication / Authorization are not a part of this simple architectural tutorial
- Neither is Security – our API is wide open and can be accessed by typing the API URL into the address bar
- In order to upload static files to an Azure Blob Container, I recommend using Microsoft Azure Storage Explorer
- On Azure Portal, the expanded “tabs” are called blades(e.g. in the image below we see two: Marketplace & Compute)
We’ll utilize 3 Azure services to complete our “serverless” demo:
- Function App – for serverless computing
- Blob Storage – for storing static files
- CDN – we’ll use a CDN(Content Delivery Network) not only to distribute files closer to clients, but, more importantly, to give us control over what file we’ll target as the origin for our app(index.html) by applying a url rewrite. We’ll follow instructions based on this wonderful write-up by Hao
High Level Tasks
- Create a Function App(source)
- Get the API Endpoint and drop it into React’s App.js, then build the app.
- Upload the static files into an Azure Blob
- Create a CDN and pre-load our files
- Create the re-write rules to target index.html
- Add the CDN domains into Function’s CORS settings to allow communication
Step by Step instructions
- Log into portal.azure.com
1. Create a Function App
- From the Dashboard, click on the green + New near the top left corner
- Enter a meaningful App Name, but keep in mind that this will not be public facing
- Create a new Resource Group.
- For the purposes of this tutorial, we’ll keep the Hosting Plan as Consumption.
- Choose a Location that’s closest to you(or your clients)
- Create a Storage account
- Click Create at the bottom of the blade
- Once the Function App has been created, we’ll move on to creating the function that will serve as our API
- In the Function App blade, click Functions
- Then, under Get started on your own, click Custom Function
- If you wish, Name your function – this will serve as the method for the http endpoint
- Leave Authorization Level as Function. This will require a key to be attached to the API call to retrieve our JSON payload – a key that you can securely store within the Azure environment and retrieve at runtime.
- Click Create
- Once index.js(function file on Azure) has loaded, replace the code with this
- Save and Test the code
- Copy the URL for the function by clicking on the </> Get function URL to the right and above the code pane
2. Drop API Endpoint into React app
- Open the app code in your favorite editor, and replace ‘INSERT YOUR API ENDPOINT URL’ with the API URL that we’ve copied in the last section on line 23
- In the root of your application run “yarn build”. This will minimize and bundle the code, and get it ready for production. You can find the stand-alone application in the newly created /build folder.
3. Upload static files to Azure Blob
- Locate the storage account that we’ve created when creating the Function App by going to the Dashboard->All resources and click it to open up the storage blade.
- Click on Blobs
- At the top left of the blade, click on +Container
- Name it web, and set the Access type to Container. Click OK
- Close the Blob Service blade by clicking the X, at the top right corner, or by pressing the Esc key
- Click on your Storage account again, from All resources tab, and click Open in Explorer(download) button
- Once the Storage Explorer has opened, expand your your Azure Storage until you see web
- Drag the contents of your app’s /build directory into the Storage Explorer pane
- The files within the blob should now be accessible via a URL
- The convention is https://<blobName>.blob.core.windows.net/<container>/<file>
- In the case of the recorded demo: https://m52serverless.blob.core.windows.net/web/index.html – however this will be offline as soon as the demo has been recorded
- If you try to access your index.html file, you will get a blank page. That is because your assets are not being pulled directly, due to there being /web/ in the URL. We’ll fix issue with the CDN in the next steps.
4. Create a CDN
- In Azure Portal click on the + New icon in the left-most menu
- In the Search the Marketplace input field type CDN
- Click on the first CDN option in the dropdown
- In the CDN blade, click the blue Create button at the bottom
- Fill in the Name
- Under Resource Group, select Use existing. In the dropdown, select the resource group that was created along with the Function App, in Task 1
- For the Pricing Tier, make sure to select Verizon Premium, as it is currently the only plan that let’s us rewrite the rules to target the index.html file as our starting point for the application, as per Hao’s post
- Click Create
- Once the CDN is created and its’ blade open up, click + Endpoint
- Fill in the Name. This will be public facing – <name>.azureedge.net
- Under Origin Type, pick Storage
- Under Origin Hostname, pick the Storage Account under which you’ve created the /web Container
- For the Origin Path, enter /web
- Click Add
- It will take a while for the files from the blob to propagate through the CDN. In the meantime, let’s create our rewrite rules
- At the top of the CDN blade, click Manage
- For this step, I’m going to, once again, refer you to Hao’s post, Steps 5 & 6
- After the rules are added, it may take up to 4 hours for them to take effect
- Once the rewrite rules and the content has propagated through the CDN, you should be able to go to your <cdn-endpoint>.azureedge.net URL and hopefully see something like this:
- The static files are up, but there is no communication with our Function due to CORS restrictions. Let’s fix that!
5. Add the CDN URL to Function CORS domains
- From the Portal’s left-most menu click on Dashboard
- Find your Function App and click it
- In the Function Apps blade, click the Platform features tab(the blade opens up on Overview)
- Under API, find and click on CORS, then enter your CDN Endpoint URL(https://<endpoint>.azureedge.net) into an empty input field
- Click Save at the top of the CORS blade
- Point your browser to the CDN Endpoint
- And, Voila!
Congrats on your first completely “serverless” deployment on Azure!
Comments and discussions are very welcome!
PS. Don’t forget to delete the resources in order for you not to incur perpetual cost.