Depicting chemicals with Azure Functions

Share on:

A guide to quickly setup an Azure function to draw chemical structures from SMILES strings.

This uses the docker image provided by the CDK project to generate image files of a molecular structure based on a SMILES string. Not only is this potentially useful as a component in a larger application, but it also demonstrates using a docker image within an Azure Function. Running this as a function allows it to be run from just pennies a day, but could also be quickly scaled for more demanding solutions.

PowerShell deployment

This should work from a local PowerShell session after using Connect-AzAccount to authenticate, or from Azure Cloud Shell.

The script builds out an Azure Function within a new App Service Plan- plus an associated Storage Account and Application Insights. The Azure function pulls the Docker Image from the Docker Hub.

 1#Script to deploy SMILES depiction function
 2
 3#Parameters
 4#Set these variables 
 5$appName=           "cdkdepict01"               #Short Name for Application used in Naming Convention
 6$tags=              @{"tag-service"="SMILES"}   #Tags to apply to the Resource Group and Resources.
 7$subscriptionName=  "Pay-As-You-Go"             #The Subscription to deploy to
 8$location=          "Central US"                #The Azure Region to deploy to.
 9
10#Determine the resource names using a naming convention
11$resourceGroupName=     $AppName+"-rsg"
12$functionName=          $AppName+"-fun"
13$appServicePlanName =   $AppName+"-asp"
14$storageAccountName =   $AppName+"sto"
15
16#Switch to the correct subscription
17    $context = Set-AzContext -SubscriptionName $subscriptionName
18
19"-- Creating Resource Group "+$resourceGroupName +" in " + $location
20    $resourceGroup = New-AzResourceGroup -Location $location -Name $resourceGroupName -tag $Tags
21
22"-- Creating App Service Plan "+$appServicePlanName
23    $appServicePlan = New-AzAppServicePlan -Location $location -Tag $tags -ResourceGroupName $resourceGroupName `
24                        -Name $appServicePlanName -Tier "Basic" -WorkerSize "Small" -NumberofWorkers 1 -Linux
25
26"-- Creating Storage Account "+$storageAccountName
27    $storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName `
28                        -SkuName "Standard_LRS" -Location $location -Kind Storage -Tag $tags
29
30"-- Creating Function "+$functionName+" from Docker Hub image"
31    $newFunction = New-AzFunctionApp -Name $functionName -ResourceGroupName $resourceGroupName -DockerImageName "simolecule/cdkdepict" `
32                        -PlanName $appServicePlanName -StorageAccountName $storageAccountName -Tag $tags
33
34"-- Finished"

Once executed the resulting web page, with examples, should be available at https://appname-fun.azurewebsites.net/ (replace appname with the value used in the script.)

Example Output