How to trigger a python script on Azure cloud? - azure

I need to trigger a python script lying on an Azure Cloud and parsing data in a folder.
How can this script be triggered when a new folder is added to the cloud?
I´m completly clueless how to do this and would be very thankful for any advice or hint.

I'm guessing this folder isn't a git repo? if it was, you could potentially have a pipeline run your script based on a trigger.
Wherever this folder is, you could also set a scheduled task/job somewhere that:
does a DIR basically
finds new folders created in the past X minutes/hours
throws the folders into an array then iterates through it, running your script
You'd need to ensure that your script won't cause issues if it runs multiple times for the same folder, and also how to deal with failed runs (say it runs every 15 minutes, one run fails, whatever folder created there is then never processed).
You can also look at something more advanced, like monitoring for changes using .NET: https://www.howtogeek.com/devops/how-to-monitor-a-windows-folder-for-new-files-and-take-action/
It all depends on where these folders are, how frequently there'll be new ones, etc

This question is a bit too broad, Azure Cloud Service has too many services.
I notice you added those tags: azure-functions, azure-devops, azure-pipelines. So I will provide two feasible solutions based on these technical combinations in the following(azure-active-directory is related to authentication, I ignored this tag).
1, azure blob storage and azure function.
I notice the azure function tag that you have added, but if you use azure function, you need to make the azure function can monitor the changing on target places. Use the azure blob storage as the source and upload Folder and files to azure blob storage is a possible way. In this situation, you can set up a python blob trigger azure function to run the scripts when files been added.
Code Sample:
__init__.py
import logging
import azure.functions as func
def main(myblob: func.InputStream):
#Put your logic here.
logging.info(f"Python blob trigger function processed blob \n"
f"Name: {myblob.name}\n"
f"Blob Size: {myblob.length} bytes")
function.json
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "myblob",
"type": "blobTrigger",
"direction": "in",
"path": "samples-path/{name}",
"connection": "bowmanxxx_STORAGE"
}
]
}
local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "python",
"bowmanxxx_STORAGE": "DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=xxx;EndpointSuffix=core.windows.net"
}
}
The above code and configurations are just for local testing, if you deploy to azure function app, just need to add the connection string 'bowmanxxx_STORAGE' of storage account to configuration settings of azure function app is ok.
2, Azure DevOps git repository and Azure DevOps YAML pipeline.
The idea of using this combination of technologies is to transfer the folder to the DevOps repository, and then use the CI trigger that comes with the Azure DevOps pipeline in the Azure Git Repository to capture the changes to the specified path.
azure-pipelines.yml
trigger:
branches:
include:
- '*'
paths:
include:
- upload_here
pool:
vmImage: ubuntu-latest
steps:
- task: PythonScript#0
inputs:
scriptSource: 'inline'
script: |
#Put your python logic here.
print("Put your python logic on the above.")
Repository Structure:
I think the key of the solution of your question is the trigger.
See the trigger documents of the above technologies:
Python Blob Storage Trigger
Azure Git Repository CI Trigger

Related

How to make Azure Function fire on queue change when using containers?

I've created an Azure Functions app and dockerized it. The container is running and doesn't give me any errors. Now I want this function app to be triggered by a storage queue insertion. I've read the microsoft docs and added the trigger in my function.json file.
{
"name": "myQueueItem",
"type": "queueTrigger",
"direction": "in",
"queueName": "searches",
"connection": "StorageAccountConnectionString"
}
The StorageAccountConnectionString is an app setting which I added to the functions app in the azure portal under configuration.
I verified the existence of the searches queue as well.
Now nothing seems to be happening when I insert a message into the searches queue. The dequeue counts doesn't increment, and the function execution counter also doesn't change.
Does anyone know how I can make this work?
If you are deploying the docker image to the function app then you can setup the environment variable in the docker file itself.
just add the following command in your dockerfile.
ENV StorageAccountConnectionString = <Your Connection String>
This way there will be a reference to the variable in the docker container. Here my storage queue trigger was working after I add the command.
You can also refer to this article by Kevin lee
I have no idea, but it was fixed by generating a function from VS Code Azure Functions extension. Then the function picked up the queue change.

How to manipulate remote Terraform state files on Azure Blob storage

I'm working with a subscription that has a few different deployed environments (dev, test, staging, etc.). Each environment has its own storage account, containing an associated Terraform state file. These environments get deployed via Azure DevOps Pipelines.
It's easy enough to get at the .tfstate files that have been created this way, through the portal, CLI, etc.
But is it possible to access these state files using the 'terraform state' commands, for example using Azure Cloud Shell? If so, how do you point them at the right location?
I've tried using the terraform state commands in a Cloud Shell, but it's not clear how to point them to the right location or if this is indeed possible.
For these requirement, you need AzurePowerShell task to achieve your requirement.
1, First, if you can achieve your requirement via powershell feature in azure portal, then it is possible using the AzurePowerShell task to achieve the same thing(AzurePowerShell is running on the agent based on the service connection/service principal you provided.).
- task: AzurePowerShell#5
inputs:
azureSubscription: 'testbowman_in_AAD' #This service connection related to service principal on Azure side.
ScriptType: 'InlineScript'
Inline: |
# Put your logic here.
# Put your logic here.
azurePowerShellVersion: 'LatestVersion'
2, Second, you can use AzCopy to download the file and then do operations to it. DevOps microsoft host agent support this tool.
running this command : terraform state pull > state.tfstate (you can give like thils dev.tfstate extension tfstate is important)in the Azure cloud shell.
All you need to move to the terraform file directory
enter image description here
and run this command terraform state pull > dev.tfstate
enter image description here

Azure App Service Deploy - Not able to override appsettings.json

I have an Azure App Service Deploy task in my pipeline to deploy my Web Api Core app to the Azure App Service. The task has the following yaml -
- task: AzureRmWebAppDeployment#4
inputs:
ConnectionType: 'AzureRM'
azureSubscription: 'myserviceconnection'
appType: 'webAppLinux'
WebAppName: 'mytestwebapp'
packageForLinux: '$(Build.ArtifactStagingDirectory)/**/*.zip'
AppSettings: 'TestWebApp/TestWebApp/appsettings.json'
I have been following this document on how to update the settings in .json files. From what I could understand, I had to create a variable in the pipeline which matches the key I need to update. Let's say I have the following Json structure in my appsettings.json -
{
"AllowedHosts": "*",
"ServiceConfiguration": {
"Key1": "value1",
"Key2": {
"Key3": "value3",
"Key4": "value4"
}
}
}
Suppose I have to update key1 to somenewvalue1 and key2.key3 to someothervalue3 and so on. So I created new variables by hitting the Variables button on the pipeline and added Key1 and key2.key3 as variables with appropriate values (as a side note, the value is a constant string, but I want this to be a dynamic value which will be provided by another task in the pipeline). Also, I provided the path for the appsetting file as shown in the image below -
But, when I run the pipeline, I get the following error-
Error: BadRequest - Parameter name cannot be empty. (CODE: 400)
I came across this SO question and also created the app setting on azure portal, but this also didn't work
What am I doing wrong here.
As a side question, As seen in the first image, what is the difference between File Transformation & Variable Substitution Options and Application and Configuration Settings and when to use what.
EDIT
Based on the comment, I was able to resolve the issue, so no more errors and I was able to verify the updated setting in the azure portal.
However, when I see the appsetting.json from Kudu inside the Site wwwroot folder under Browse Directory, I couldn't see it updated. Why are these valuees different, and if so, which value is actually considered.
Thanks
So it seems that one expects JSON containing the settings, and not a file path.
As for the settings file not updating, that is expected.
This is updating the settings in the Configuration tab of the App Service, which will be passed to your app as environment variables.
It won't update the file.
Instead it sets settings that override settings in the file (at least by default if you the default host builder).
It'll only override settings you specify.

Azure Data Factory V2 Copy activity Mapping deployment issue

Consider following test Mapping for Data Factory Copy activity:
"translator": {
"columnMappings": "#json('{\"from\":\"to\"}')",
"type": "TabularTranslator"
}
After deploying pipeline with the help of Set-AzureRmDataFactoryV2Pipeline PowerShell cmdlet we get normally deployed pipeline with the exact columnMappings value as specified in source code. But if you try to be more dynamic:
"translator": {
"columnMappings": "#json(pipeline().parameters.Mapping)",
"type": "TabularTranslator"
}
then after deployment you'll find that translator element is completely missing in pipeline. A workaround - set translator in Azure Portal Data Factory pipeline editing UI (either in Designer or JSON modes - both options work). But if after these manipulations you save pipeline JSON to the file and attempt to deploy it via Set-AzureRmDataFactoryV2Pipeline PowerShell cmdlet - bang, translator becomes missing. Expected result - deployment shall preserve translator element, because Portal JSON Editor preserves it.
We are doing automated deployment of pipelines (as you already figured out - with the help of Set-AzureRmDataFactoryV2Pipeline) and this bug breaks our automated deployment because it requires manual postdeployment pipeline editing on Azure Portal UI.
What may be the reason of such a buggy behavior? Can you suggest an idea how to work around this bug in automated way, or how to fix the code so it can be properly deployed with Set-AzureRmDataFactoryV2Pipeline?
You could try whether "Update-Module -Name AzureRm.DataFactoryV2" helps. It might be caused by that your powershell module is out of date.

Visual Studio Team Services: Raw link to build artifacts

I see several examples of Azure Resource Manager templates, referencing artifacts directly in Git Hub.
As in the following example, taken from this quick start template:
"modulesUrl": {
"type": "string",
"defaultValue": "https://github.com/Azure/azure-quickstart-templates/raw/master/dsc-extension-azure-automation-pullserver/UpdateLCMforAAPull.zip",
"metadata": {
"description": "URL for the DSC configuration package. NOTE: Can be a Github url(raw) to the zip file (this is the default value)"
}
As an orgnaisation, we can't use free Git Hub as code is public and as we pay for VSTS already... At the moment, we have to upload artifacts to Azure Storage Accounts using the VSTS build task Azure Resource Group Deployment task and reference them from there. It would be nice if we could remove this step.
So, is there a way to reference artifacts directly from a VSTS repository in a similar way to Git Hub? I assume the URI would require some form of authentication, such as a PAT token.
All I can find is this, but I think it is referring to packages. I don't need to create packages for ARM templates and DSC configurations.
There is a task called Azure Resource Group Deployment Task, we use this to deploy the ARM template.
According to you sample template, it's using publicly accessible http/https URLs in GitHub. Afraid this is not accessible via vsts url. In VSTS you need to follow below process (Need to use a SAS Token):
You could provide some extra parameters using the output variables defined in the Azure File Copy Task (storageURI, storageToken). This are needed because in the template we use the _artifactsLocation and _artifactsLocationSasToken parameters to build the storage URL to the files.
More details please refer this blog: Setting up VSTS with ARM Templates

Resources