Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Creating an Azure Function with Source Code as part of Bicep script
param LocationCode string
resource Storage 'Microsoft.Storage/storageAccounts@2021-04-01' = {
name: 'stor${LocationCode}labfunction'
location: resourceGroup().location
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
}
properties: {
encryption: {
keySource: 'Microsoft.Storage'
services: {
blob: {
enabled: true
}
file: {
enabled: true
}
queue: {
enabled: true
}
table: {
enabled: true
}
}
requireInfrastructureEncryption: true
}
allowBlobPublicAccess: false
}
}
resource StorageTable 'Microsoft.Storage/storageAccounts@2021-04-01' = {
name: 'stor${LocationCode}labfunctable'
location: resourceGroup().location
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
}
properties: {
encryption: {
keySource: 'Microsoft.Storage'
services: {
blob: {
enabled: true
}
file: {
enabled: true
}
queue: {
enabled: true
}
table: {
enabled: true
}
}
requireInfrastructureEncryption: true
}
allowBlobPublicAccess: false
}
}
resource AppService 'Microsoft.Web/serverfarms@2021-01-15' = {
name: 'asp-${LocationCode}-lab-function'
location: resourceGroup().location
sku: {
tier: 'Dynamic'
name: 'Y1'
}
}
var FunctionName = 'func-${LocationCode}-lab-function'
resource Function 'Microsoft.Web/sites@2021-01-15' = {
name: FunctionName
kind: 'Functionapp'
location: resourceGroup().location
properties: {
serverFarmId: AppService.id
siteConfig: {
appSettings: [
{
name: 'FUNCTIONS_EXTENSION_VERSION'
value: '~3'
}
{
name: 'FUNCTIONS_WORKER_RUNTIME'
value: 'node'
}
{
name: 'WEBSITE_NODE_DEFAULT_VERSION'
value: '~14'
}
{
name: 'AzureWebJobsStorage'
value: 'DefaultEndpointsProtocol=https;AccountName=${Storage.name};AccountKey=${listKeys(Storage.id, Storage.apiVersion).keys[0].value};EndpointSuffix=core.windows.net'
}
{
name: 'WEBSITE_CONTENTAZUREFILECONNECTIONSTRING'
value: 'DefaultEndpointsProtocol=https;AccountName=${Storage.name};AccountKey=${listKeys(Storage.id, Storage.apiVersion).keys[0].value};EndpointSuffix=core.windows.net'
}
{
name: 'WEBSITE_CONTENTSHARE'
value: '${toLower(FunctionName)}files'
}
{
name: 'TableConnection'
value: 'DefaultEndpointsProtocol=https;AccountName=${StorageTable.name};AccountKey=${listKeys(StorageTable.id, StorageTable.apiVersion).keys[0].value};EndpointSuffix=core.windows.net'
}
]
use32BitWorkerProcess: false
ftpsState: 'Disabled'
}
}
}
resource FunctionPostData 'Microsoft.Web/sites/functions@2021-01-15' = {
name: '${FunctionName}/PostData'
dependsOn: [
Function
]
properties: {
config: {
bindings: [
{
authLevel: 'function'
type: 'httpTrigger'
direction: 'in'
name: 'req'
methods: [
'post'
]
}
{
type: 'http'
direction: 'out'
name: 'res'
}
{
name: 'outputTable'
direction: 'out'
type: 'table'
tableName: 'Sample'
connection: 'TableConnection'
}
]
}
files: {
'index.js': loadTextContent('post.js')
}
}
}
resource FunctionGetData 'Microsoft.Web/sites/functions@2021-01-15' = {
name: '${FunctionName}/GetData'
dependsOn: [
Function
]
properties: {
config: {
bindings: [
{
authLevel: 'function'
type: 'httpTrigger'
direction: 'in'
name: 'req'
methods: [
'get'
]
}
{
name: 'sampleTable'
direction: 'in'
type: 'table'
tableName: 'Sample'
connection: 'TableConnection'
}
{
type: 'http'
direction: 'out'
name: 'res'
}
]
}
files: {
'index.js': loadTextContent('get.js')
}
}
}
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
context.res = {
body: context.bindings.sampleTable
};
}
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
let val = Math.random().toString(36).substring(7);
context.bindings.outputTable = [];
context.bindings.outputTable.push({
PartitionKey: 'Test',
RowKey: val,
Value: req.body
});
context.res = {
body: "Record Inserted - " + val
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment