Deploying API Management XML Policy with unique endpoints to multiple environments #6092
-
Hi! I am attempting to deploy a different API Management policy file dependent on environment. main.bicep
apimpolicy.bicep
The above snip works great for one environment. However, we have multiple environments (dev/sit/uat/prod etc) that each have their own unique policy settings (CORS, Backend Service Id, Auth Managed Id etc). I have tried creating a separate policy file per environment such as
but that generates an error saying the value must be a compile-time constant. ${env} is set from a deployment pipeline variable. Is there any way I can either:
var apimXmlPolicyFile = loadTextContent('modules/apimpolicy/${env}apimpolicy.xml') Failing that, how would I go about deploying a different XML policy file depending on the deployment environment (dev/sit/uat/prod)? Hope that makes sense, thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Hi @c64breadbin There is likely a few different ways to achieve this and also a few different formats. I will suggest a few options. In the most simplest I think is Use Replacement strings along with placeholders e.g. {0}, {1}, {2} etc. Then save the XML text file, since we will use loadtextcontext <policies>
<inbound>
<base />
<cors allow-credentials="true">
<allowed-origins>
<origin>{0}</origin>
</allowed-origins>
<allowed-methods preflight-result-max-age="1209600">
<method>*</method>
</allowed-methods>
<allowed-headers>
<header>*</header>
</allowed-headers>
</cors>
<set-backend-service id="apim-generated-policy" backend-id="{1}" />
<authentication-managed-identity resource="{2}" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies> @allowed([
'dev'
'prod'
])
param enviro string = 'dev'
var content = loadTextContent('./loadtextcontent/policy.xml')
var uri = enviro == 'dev' ? 'https://dev.com' : 'https://prod.com'
var beid = enviro == 'dev' ? 'devid' : 'prodid'
var uai = enviro == 'dev' ? 'devuai' : 'produai'
var policy = replace(replace(replace(content, '{0}', uri),'{1}',beid),'{2}',uai)
output policy string = policy Which would provide you with the following output.
<policies>
<inbound>
<base />
<cors allow-credentials="true">
<allowed-origins>
<origin>https://dev.com</origin>
</allowed-origins>
<allowed-methods preflight-result-max-age="1209600">
<method>*</method>
</allowed-methods>
<allowed-headers>
<header>*</header>
</allowed-headers>
</cors>
<set-backend-service id="apim-generated-policy" backend-id="devid" />
<authentication-managed-identity resource="devuai" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies> Other than that there are other ways to do the lookup, since you could have an object for Prod and and Object for Dev etc., then untilise the properties of the object in the replace. etc. Happy to share some more examples. I think this method would scale better than attemping to use different files for each environment etc. |
Beta Was this translation helpful? Give feedback.
Hi @c64breadbin
There is likely a few different ways to achieve this and also a few different formats.
I will suggest a few options.
In the most simplest I think is Use Replacement strings along with placeholders
e.g. {0}, {1}, {2} etc.
Then save the XML text file, since we will use loadtextcontext