Skip to content

Instantly share code, notes, and snippets.

@pinakighatak
Created March 21, 2022 20:10
Show Gist options
  • Save pinakighatak/ec04d54c7c553c91092c3c01e2df230b to your computer and use it in GitHub Desktop.
Save pinakighatak/ec04d54c7c553c91092c3c01e2df230b to your computer and use it in GitHub Desktop.
Azure Functions Deployment with Bicep
// ****************************************
// Azure Bicep main template
// This bicep template demonstrates provisioning Azure function that implements OpenAPI specifications.
// Optinally this Azure Function endp-point can be published to an existing API Management - witin a Product
// Last Update : 17-Feb-2022
// ****************************************
targetScope = 'resourceGroup'
param functionRuntime string = 'dotnet'
param logAnalyticsWorkspaceName string = 'la-${uniqueString(resourceGroup().id)}'
@description('Location for all resources.')
param location string = resourceGroup().location
//pass in required trigger initiator.
@minLength(2)
param initiator string
var appServiceName = 'PGFunctions${uniqueString(resourceGroup().id)}' //will add 13 characters to the name
var functionAppName = appServiceName
var appInsightsName = appServiceName
param resourceTags object = {
ProjectType: 'AzureFunctions'
Purpose: 'Demo'
DeploymentDate: utcNow('dd-MMM-yyyy')
DeployedBy: initiator
}
//Log Analytics Workspace
resource logAnalyticsWorkspace 'Microsoft.OperationalInsights/workspaces@2021-12-01-preview' = {
name: logAnalyticsWorkspaceName
location: location
tags: resourceTags
properties: any({
retentionInDays: 30
features: {
searchVersion: 1
}
sku: {
name: 'PerGB2018'
}
})
}
//Application Inisghts
resource applicationInsights 'Microsoft.Insights/components@2020-02-02' = {
name: appInsightsName
location: location
tags: resourceTags
kind: 'web'
properties: {
Application_Type: 'web'
Flow_Type: 'Bluefield'
Request_Source: 'rest'
WorkspaceResourceId: logAnalyticsWorkspace.id
}
}
// Storage Account with Managed identity
resource storageAccount 'Microsoft.Storage/storageAccounts@2021-08-01' = {
name: 'pgfunctions${uniqueString(resourceGroup().id)}'
location: location
sku: {
name: 'Standard_LRS'
}
kind: 'StorageV2'
identity:{
type:'SystemAssigned'
}
properties: {
supportsHttpsTrafficOnly: true
allowBlobPublicAccess:false
encryption: {
services: {
file: {
keyType: 'Account'
enabled: true
}
blob: {
keyType: 'Account'
enabled: true
}
}
keySource: 'Microsoft.Storage'
}
accessTier: 'Hot'
}
tags: resourceTags
}
// Blob Services for Storage Account
resource blobServices 'Microsoft.Storage/storageAccounts/blobServices@2021-08-01' = {
parent: storageAccount
name: 'default'
properties: {
cors: {
corsRules: []
}
deleteRetentionPolicy: {
enabled: true
days: 7
}
}
}
// App Service
resource appService 'Microsoft.Web/serverfarms@2021-03-01' = {
name: appServiceName
location: location
kind: 'functionapp'
sku: {
name: 'Y1'
tier: 'Dynamic'
size: 'Y1'
family: 'Y'
capacity: 0
}
properties: {
perSiteScaling: false
maximumElasticWorkerCount: 1
isSpot: false
reserved: false
isXenon: false
hyperV: false
targetWorkerCount: 0
targetWorkerSizeId: 0
}
tags: resourceTags
}
// Function App with Managed Identity
resource functionApp 'Microsoft.Web/sites@2021-03-01' = {
name: functionAppName
location: location
kind: 'functionapp'
identity:{
type:'SystemAssigned'
}
properties: {
enabled: true
hostNameSslStates: [
{
name: '${functionAppName}.azurewebsites.net'
sslState: 'Disabled'
hostType: 'Standard'
}
{
name: '${functionAppName}.scm.azurewebsites.net'
sslState: 'Disabled'
hostType: 'Standard'
}
]
serverFarmId: appService.id
reserved: false
isXenon: false
hyperV: false
siteConfig: {
appSettings: [
{
name: 'AzureWebJobsStorage'
value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccount.name};EndpointSuffix=${environment().suffixes.storage};AccountKey=${listKeys(storageAccount.id, storageAccount.apiVersion).keys[0].value}'
}
{
name: 'WEBSITE_CONTENTAZUREFILECONNECTIONSTRING'
value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccount.name};EndpointSuffix=${environment().suffixes.storage};AccountKey=${listKeys(storageAccount.id, storageAccount.apiVersion).keys[0].value}'
}
{
name: 'APPINSIGHTS_INSTRUMENTATIONKEY'
value: applicationInsights.properties.InstrumentationKey
}
{
name: 'APPLICATIONINSIGHTS_CONNECTION_STRING'
value: 'InstrumentationKey=${applicationInsights.properties.InstrumentationKey}'
}
{
name: 'FUNCTIONS_WORKER_RUNTIME'
value: functionRuntime
}
{
name: 'FUNCTIONS_EXTENSION_VERSION'
value: '~4'
}
]
}
scmSiteAlsoStopped: false
clientAffinityEnabled: false
clientCertEnabled: false
hostNamesDisabled: false
dailyMemoryTimeQuota: 0
httpsOnly: true
redundancyMode: 'None'
}
tags: resourceTags
}
// Function App Binding
resource functionAppBinding 'Microsoft.Web/sites/hostNameBindings@2021-03-01' = {
parent: functionApp
name: '${functionApp.name}.azurewebsites.net'
properties: {
siteName: functionApp.name
hostNameType: 'Verified'
}
}
//get outputs in a variable so they can be used later in the pipeline
output appInsightsInstrumentationKey string = applicationInsights.properties.InstrumentationKey
output functionName string = functionApp.name
output pipelineInitiator string = initiator
output functionURL string = 'https://${functionApp.properties.defaultHostName}'
output functionSwaggerURL string = 'https://${functionApp.properties.defaultHostName}/api/swagger/ui'
output azfMI string = functionApp.identity.principalId //get Azure function system asssigned managed identity
output azStgMI string = storageAccount.identity.principalId //get storage system asssigned managed identity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment