DEV Community

mahamaresh
mahamaresh

Posted on

Creating the stack which can execute in two subscriptions/providers

Problem statement : If your pulumi stack has to be executed in two subscriptions you can use the below steps.

Example
Subscription 1 : Resource Group, Appservice/Azure Function, Storage account
Subscription 2 : API Management , API , Policy.

For executiing the stack we will be connected to the subscription.

Code snippet 1 creating an APIPolicy using Pulumi Classic :

Getting the subscription details into a provider.

using AzureClassic = Pulumi.Azure;

var apimsubscription = new AzureClassic.Provider("apimsubscription", new AzureClassic.ProviderArgs()
{
SubscriptionId = "###provide your subscription details"
});

Now we have created the provider, we then need to use this for any resources we want to deploy to that subscription. To do this, we use the “CustomResourceOptions” section to set this.

// api.Name is derived from the variable that you created the API,APIM name is the API management name, rgID is the resource group name, xml content is the policy that you need to apply.

var ApiPolicy = new AzureClassic.ApiManagement.ApiPolicy("" + stackName + "ApiPolicy", new AzureClassic.ApiManagement.ApiPolicyArgs
{
ApiName = api.Name,
ApiManagementName = APIM.Name,
ResourceGroupName = rgid.Name,
XmlContent = apipolicy,
}
, new CustomResourceOptions() { Provider = apimsubscription }
);

// Custom resource option is the code snippet which derives this block of code to use the other subscription.

Code snippet 2 using pulumi native :

var apimnativesubscription = new Pulumi.AzureNative.Provider("apimsubscription", new Pulumi.AzureNative.ProviderArgs()
{
SubscriptionId = "### please provide the subscription details"
});

var ApipolicyNative = new Pulumi.AzureNative.ApiManagement.ApiPolicy("apiPolicy", new Pulumi.AzureNative.ApiManagement.ApiPolicyArgs
{
ApiId = apiBanking.Name,

ResourceGroupName = rgid.Name,
Value = apipolicy,
}
, new CustomResourceOptions() { Provider = apimnativesubscription }
);

Note : Subscription derived through classic method should be used only for classic and the same applies for native.
Other wise we will get an error: Resource type azure:apimanagement/api:Api not found

Discussion (0)