How to create AWS Lambda, Azure Function, Google Cloud Function with Serverless Framework CLI

Serverless framework is a CLI to build cloud applications. There are more than 10 Serverless Infrastructure Providers like AWS, Azure and GCP as shown below screenshot.

Find more Serverless Framework CLI command in Azure Serverless TypeScript project in GitHub

According to my experience, Serverless Framework gives a better support for AWS provider, for example, if you see project template below, there are many project templates for AWS but very less support for other providers.

Find more details in https://www.serverless.com/framework/docs/getting-started

Continue reading

Python Azure Function Debugging in Conda virtual environment

Creating virtual environment

$ mkdir python_demo
$ cd python_demo

don't forget to update name of environment name, and i added pip, ptvsd packages to debug azure function.

$ cat > environment.yml <<EOL
name: python_demo-3.9
channels:
  - defaults
dependencies:
  - python=3.9
  - pip
  - pip:
    - ptvsd
EOL
$ conda env create -f environment.yml

Collecting package metadata (repodata.json): done
Solving environment: done

Downloading and Extracting Packages

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
Installing pip dependencies: - Ran pip subprocess with arguments:
['/opt/homebrew/Caskroom/miniconda/base/envs/python_azure_function_conda_virtual_env-3.9/bin/python', '-m', 'pip', 'install', '-U', '-r', '/Users/kenanhancer/Documents/projects/python-projects-kenanhancer/python_azure_function_conda_virtual_env/condaenv.5uk0y5ie.requirements.txt', '--exists-action=b']
Pip subprocess output:
Collecting ptvsd (from -r /Users/kenanhancer/Documents/projects/python-projects-kenanhancer/python_azure_function_conda_virtual_env/condaenv.5uk0y5ie.requirements.txt (line 1))
  Using cached ptvsd-4.3.2-py2.py3-none-any.whl (4.9 MB)
Installing collected packages: ptvsd
Successfully installed ptvsd-4.3.2

done
#
# To activate this environment, use
#
#     $ conda activate python_azure_function_conda_virtual_env-3.9
#
# To deactivate an active environment, use
#
#     $ conda deactivate
Continue reading

How to install Azure CLI and Azure Functions Core Tools CLI with Homebrew

Verifying Homebrew

$ brew --version

Homebrew 3.2.6
Homebrew/homebrew-core (git revision c4d5aac8ec; last commit 2021-08-07)
Homebrew/homebrew-cask (git revision 1169dcb641; last commit 2021-08-07)

Demo repository is in the following link.

https://github.com/nodejs-projects-kenanhancer/serverless-azure-functions-typescript-demo

Azure CLI

Azure CLI, or Azure Command Line Interface, is a set of commands used to create and manage Azure resources. It is a cross-platform command-line tool that's available for Windows, macOS, and Linux and can be run in the Azure Cloud Shell directly from a web browser.

Azure CLI is designed to be easy to learn and get started with but powerful enough to manage your entire Azure environment. It's optimized for automation and can be used in a DevOps pipeline. You can use Azure CLI to create and manage resources such as virtual machines, storage accounts, and web apps, among others.

Before using Azure CLI, you need to authenticate with your Azure account. You can do this by using the az login command, which opens a web browser for you to sign in with your Azure credentials.

Azure CLI Usage Pattern

$ az [group] [subgroup] [command]

Example – to create a new resource group

$ az group create

Installing

$ brew update && brew install azure-cli

Verifying

$ az --version

azure-cli                         2.26.1 *

core                              2.26.1 *
telemetry                          1.0.6

Python location '/usr/local/Cellar/azure-cli/2.26.1/libexec/bin/python'
Extensions directory '/Users/kenanhancer/.azure/cliextensions'

Python (Darwin) 3.8.11 (default, Jun 29 2021, 03:08:07) 
[Clang 12.0.5 (clang-1205.0.22.9)]

Legal docs and information: aka.ms/AzureCliLegal


You have 2 updates available. Consider updating your CLI installation with 'az upgrade'

Please let us know how we are doing: https://aka.ms/azureclihats
and let us know if you're interested in trying out our newest features: https://aka.ms/CLIUXstudy
Continue reading

Creating Azure Function App for Python with Azure Functions Core Tools CLI

We cannot specify the Python runtime version with FUNCTIONS_EXTENSION_VERSION. The FUNCTIONS_EXTENSION_VERSION setting is specifically for specifying the version of the Azure Functions runtime, not the version of Python.

The Azure Functions runtime is the host that runs your functions. It's a separate concept from the Python runtime. Each version of the Azure Functions runtime has a range of versions of Python (and other languages) that it supports, but you can't use the FUNCTIONS_EXTENSION_VERSION setting to choose a specific Python version.

The version of Python that's used in your function app is typically determined by the environment in which the app is running. When running locally, this will be the version of Python that's available in your local environment. When running in Azure, the Python version is determined by the configuration of your Azure Functions host.

If you want to specify a Python version for local development, you can do so using a Python version management tool like pyenv, and creating a .python-version file in the root of your function app project. For deployment in Azure, you specify the Python version when you create the Function App.

Remember to check the Azure Functions runtime support for your chosen Python version. Azure Functions 4.x runtime supports Python 3.7, 3.8, 3.9 and 3.10 for now.

Prerequisite check for Python

$ az --version
$ az login
$ func --version
$ python --version

Create and activate a virtual environment with virtualenv package manager

virtualenv (for Python 2) and venv (for Python 3) allow you to manage separate package installations for different projects.

$ mkdir azure-demo
$ cd azure-demo
$ pyenv local 3.10.5
$ python -m venv .venv
Continue reading

Azure Functions Basics

Before you begin, you must have the following requirements in place:

Prerequisite check

# to check that the Azure Functions Core Tools are version v4.0.5095 or above.
$ func --version

4.0.5198
# to check that the Azure CLI version is 2.4 or later.
$ az --version

azure-cli                         2.49.0

core                              2.49.0
telemetry                          1.0.8

Dependencies:
msal                              1.20.0
azure-mgmt-resource               22.0.0

Python location '/opt/homebrew/Cellar/azure-cli/2.49.0/libexec/bin/python'
Extensions directory '/Users/kenanhancer/.azure/cliextensions'

Python (Darwin) 3.10.12 (main, Jun  7 2023, 00:38:32) [Clang 14.0.3 (clang-1403.0.22.14.1)]

Legal docs and information: aka.ms/AzureCliLegal


Your CLI is up-to-date.
# to sign in to Azure and verify an active subscription.
$ az login

Continue reading

Azure DevOps: CI/CD IaC with YAML Pipeline

Sign in to your organisation (https://dev.azure.com/{yourorganization})

Azure DevOps looks like the following screenshot.

Find more details about key consepts in Azure Pipelines Key Concepts

Find more details about extending Azure Pipelines Templates in Azure Pipelines Templates

Find more details about Azure template parameters in Azure Pipeline Template Parameters

Find more details about Azure template predefined variables in Azure Pipeline Template Predefined Variables

Find more details about Azure template script variables in Azure Pipeline Template Script Variables

Find more details about usage of expressions in Azure Pipelines Expressions

In a pipeline, template expression variables (${{ variables.var }}) get processed at compile time, before runtime starts. Macro syntax variables ($(var)) get processed during runtime before a task runs. Runtime expressions ($[variables.var]) also get processed during runtime but are intended to be used with conditionsand expressions

Find more details about usage of Azure template variables in Azure Pipeline Template Variable Usage

Azure DevOps Organizations Page

Continue reading

Azure DevOps REST API

When we need to create, retrieve, update or delete access to the Azure DevOps services's resources, we can use Azure DevOps REST API.

Find more Azure DevOps REST services in https://docs.microsoft.com/en-us/rest/api/azure/devops/?view=azure-devops-rest-7.1

Process REST API

Get Process Id in your organization

Find more details in https://docs.microsoft.com/en-us/rest/api/azure/devops/core/processes/list?view=azure-devops-rest-7.1

ORGANIZATION=$1

PROCESS_NAME=$2

PAT=$3

curl --silent --user :$PAT \
--request GET "https://dev.azure.com/$ORGANIZATION/_apis/process/processes?api-version=6.0" | jq -r '.value[] | select(.name=="'$PROCESS_NAME'") | .id'
$ . ./getProcessId.sh kenanhancer Agile blablabla

adcc42ab-9882-485e-a3ed-7678f01f66bc

Get Process in your organization

Find more details in https://docs.microsoft.com/en-us/rest/api/azure/devops/core/processes/get?view=azure-devops-rest-7.1

echo -n "Organization: " && read ORGANIZATION

echo -n "Process Name: " && read PROCESS_NAME

echo -n "PAT: " && read PAT

PROCESS_ID=$(. ./getProcessId.sh $ORGANIZATION $PROCESS_NAME $PAT)

curl --silent --user :$PAT \
--request GET "https://dev.azure.com/$ORGANIZATION/_apis/process/processes/$PROCESS_ID?api-version=6.0" | jq -r .
$ . ./getProcess.sh
Organization: kenanhancer
Process Name: Agile
PAT: blablabla

{
  "id": "adcc42ab-9882-485e-a3ed-7678f01f66bc",
  "description": "This template is flexible and will work great for most teams using Agile planning methods, including those practicing Scrum.",
  "isDefault": true,
  "_links": {
    "self": {
      "href": "https://dev.azure.com/kenanhancer/_apis/process/processes/adcc42ab-9882-485e-a3ed-7678f01f66bc"
    }
  },
  "type": "system",
  "url": "https://dev.azure.com/kenanhancer/_apis/process/processes/adcc42ab-9882-485e-a3ed-7678f01f66bc",
  "name": "Agile"
}
Continue reading

Azure DevOps: Creating Personal Access Token (PAT)

Sign in to your organisation (https://dev.azure.com/{yourorganization})

Treat and use a PAT like your password and keep it a secret.

Use your PAT anywhere your user credentials are required for authentication in Azure DevOps.

You can use a personal access token (PAT) as an alternate password to authenticate into Azure DevOps.

A personal access token contains your security credentials for Azure DevOps. A PAT identifies you, your accessible organizations, and scopes of access. As such, they're as critical as passwords, so you should treat them the same way.

If you're working within Microsoft tools, then your Microsoft account (MSA) or Azure Active Directory (Azure AD) is an acceptable and well-supported approach. But, if you're working with third-party tools that don't support Microsoft or Azure AD accounts – or you don't want to provide your primary credentials to the tool – use PATs to limit your risk.

find more details about creating PAT in Azure DevOps

Continue reading

Azure Cosmos DB Emulator and Storage Emulator

Azurite emulator provides a free local environment for testing your Azure blob, queue storage, and table storage applications. Find more details in https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azurite?tabs=docker-hub

Cosmosdb emulator provides a local environment that emulates the Azure Cosmos DB service for development purposes. Using the Azure Cosmos DB Emulator, you can develop and test your application locally. You can develop applications using Azure Cosmos DB Emulator with the SQLCassandraMongoDBGremlin/Graph, and Table API accounts. Currently the data explorer in the emulator fully supports viewing SQL data only; the data created using MongoDB, Gremlin/Graph and Cassandra client applications it is not viewable at this time. 

Creating docker-compose.yml file

version: '3.4'

services:
  cosmosdb:
    container_name: cosmosdb
    image: "mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator"
    tty: true
    restart: always
    mem_limit: 3g
    cpu_count: 2
    environment:
      - AZURE_COSMOS_EMULATOR_PARTITION_COUNT=10
      - AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE=true
      - AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE=${HOST_IP}
    ports:
        - "8081:8081"
        - "10251:10251"
        - "10252:10252"
        - "10253:10253"
        - "10254:10254"
    volumes:
        - vol_cosmos:/data/db

  azurite:
    container_name: azurite
    image: "mcr.microsoft.com/azure-storage/azurite"
    restart: always
    ports:
        - "10000:10000"
        - "10001:10001"
        - "10002:10002"
    volumes:
        - vol_cosmos:/data

volumes:
  vol_cosmos:
Continue reading