azureazure-functionsazure-functions-core-tools

azure functions created in one cloud location for example south India should be able to create resource group in location central US


azure functions created in one cloud location for example south India should be able to create resource group in location central US

I want a clear step by step approach if this is possible.

const { ResourceManagementClient } = require("@azure/arm-resources");

module.exports = async function (context, req) {
    const subscriptionId = process.env["AZURE_SUBSCRIPTION_ID"];
    const credentials = new DefaultAzureCredential();
    const resourceClient = new ResourceManagementClient(credentials, subscriptionId);

    const resourceGroupName = "demoDiffResourceGroup";
    const resourceGroupParameters = {
        location: "centralus"
    };

    context.log(`Creating resource group: ${resourceGroupName} in Central US`);

    try {
        const result = await resourceClient.resourceGroups.createOrUpdate(resourceGroupName, resourceGroupParameters);
        context.res = {
            body: `Resource group created successfully: ${result.name}`
        };
    } catch (err) {
        context.res = {
            status: 500,
            body: `Error creating resource group: ${err.message}`
        };
    }
};

Solution

  • I have used below code to create Resource group and Function App in different regions:

    Resource group location: Central US
    Function App location: South India
    

    Code Snippet:

    const { DefaultAzureCredential } = require('@azure/identity');
    const { ResourceManagementClient } = require('@azure/arm-resources');
    const { WebSiteManagementClient } = require('@azure/arm-appservice');
    
    const subscriptionId = "<Subscription_id>";
    const resourceGroupName = "kprg";
    const functionAppName = 'kpfnode';
    const resourceGroupLocation = 'CentralUS';
    const functionAppLocation = 'southindia';
    
    const credential = new DefaultAzureCredential();
    
    const resourceClient = new ResourceManagementClient(credential, subscriptionId);
    const webSiteClient = new WebSiteManagementClient(credential, subscriptionId);
    
    // Creation of Resource group in Central US region
    
    async function createResourceGroup() {
        const resourceGroupParams = { location: resourceGroupLocation };
        console.log(`Creating resource group: ${resourceGroupName}`);
        const resourceGroup = await resourceClient.resourceGroups.createOrUpdate(resourceGroupName, resourceGroupParams);
        console.log(`Resource group created: ${resourceGroup.name}`);
    }
    
    // Creation of App Service Plan
    async function createAppServicePlan() {
      const appServicePlanName = `${functionAppName}Plan`;
      const appServicePlanParams = {
          location: functionAppLocation,
          sku: {
              name: 'Y1',
              tier: 'Dynamic',
              size: 'Y1',
              family: 'Y',
              capacity: 0,
          },
      };
      
      console.log(`Creating App Service Plan: ${appServicePlanName}`);
      const appServicePlan = await webSiteClient.appServicePlans.beginCreateOrUpdateAndWait(resourceGroupName, appServicePlanName, appServicePlanParams);
      console.log(`App Service Plan created: ${appServicePlan.name}`);
    }
    
    // Creation of functionapp in South India region
    
    async function createFunctionApp() {
      const siteParams = {
          location: functionAppLocation,
          serverFarmId: `/subscriptions/${subscriptionId}/resourceGroups/${resourceGroupName}/providers/Microsoft.Web/serverfarms/${functionAppName}Plan`,
          siteConfig: {
              appSettings: [
                  {
                      name: 'FUNCTIONS_EXTENSION_VERSION',
                      value: '~3'
                  },
                  {
                      name: 'WEBSITE_NODE_DEFAULT_VERSION',
                      value: '14'
                  },
              ],
          },
          kind: 'functionapp',
      };
    
      console.log(`Creating Function App: ${functionAppName}`);
      const functionApp = await webSiteClient.webApps.beginCreateOrUpdateAndWait(resourceGroupName, functionAppName, siteParams);
      console.log(`Function App created: ${functionApp.name}`);
    }
    
    async function main() {
      try {
          await createResourceGroup();
          await createAppServicePlan();
          await createFunctionApp();
      } catch (err) {
          console.error('Error creating resources:', err);
      }
    }
    
    main();
    

    Console response:

    C:\Users\uname\nodeapp> node app.js
    Creating resource group: kprg
    Resource group created: kprg
    Creating App Service Plan: kpfnodePlan
    App Service Plan created: kpfnodePlan
    Creating Function App: kpfnode
    Function App created: kpfnode
    

    Update:

    const { DefaultAzureCredential } = require("@azure/identity");
    const { ResourceManagementClient } = require("@azure/arm-resources");
    
    module.exports = async function (context, req) {
      
        const subscriptionId = process.env["AZURE_SUBSCRIPTION_ID"];
        const resourceGroupName = "myResourceGroup";
        const location = "centralus";
    
        // Authenticate with Azure
        const credential = new DefaultAzureCredential();
        const client = new ResourceManagementClient(credential, subscriptionId);
    
        try {
            // Creation of resource group
            const result = await client.resourceGroups.createOrUpdate(resourceGroupName, {
                location: location
            });
            context.res = {
                status: 200,
                body: `Resource group created successfully: ${result.id}`
            };
        } catch (error) {
            context.res = {
                status: 500,
                body: `Error creating resource group: ${error.message}`
            };
        }
    };
    
    

    Resource group got created in Central US region:

    enter image description here

    Function App got created in South India region:

    enter image description here