Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically altering Service Fabric Environment Variables

I have a service in Service Fabric containing an Environment Variable defined in the service manifest that I want to alter dynamically after deployment. What is the best way to do so?

As far as I've been able to tell this necessarily involves updating the application and creating a new version for the service.

like image 361
aag1992 Avatar asked Oct 27 '25 08:10

aag1992


1 Answers

  1. Specify the environment variables and values in your service manifests. If you already have environment variables specified you probably already have these.
<CodePackage Name="MyCode" Version="CodeVersion1">
        <EnvironmentVariables>
              <EnvironmentVariable Name="MyEnvVariable" Value="DefaultValue"/>
              <EnvironmentVariable Name="HttpGatewayPort" Value="19080"/>
        </EnvironmentVariables>
</CodePackage>
  1. Add the environment variable overrides in the application manifest
<ServiceManifestImport>
    <ServiceManifestVersion="1.0.0" />
    <EnvironmentOverrides CodePackageRef="MyCode">
      <EnvironmentVariable Name="MyEnvVariable" Value="OverrideValue"/>
    </EnvironmentOverrides>
  </ServiceManifestImport>
  1. In the application manifest, instead of a specific overridden value, specify the environment variable value as an application parameter.
<ServiceManifestImport>
    <ServiceManifestVersion="1.0.0" />
    <EnvironmentOverrides CodePackageRef="MyCode">
      <EnvironmentVariable Name="MyEnvVariable" Value="[MyEnvVariableOverride]"/>
    </EnvironmentOverrides>
  </ServiceManifestImport>

and

<Parameters>
   <Parameter Name="MyEnvVariableOverride" DefaultValue="DefaultOverrideValue" />
</Parameters>

  1. Do an application upgrade that changes the application parameters via the ApplicationParameters hashtable (in PS - mapping of parameter name to parameter value). For example you would set "MyEnvVariableOverride" to "FinalValue". This will flow down and cause the environment variable value that the service sees to change. Note that changing environment variables requires that the process restart.

Relevant docs: 1, 2, 3, 4

like image 81
masnider Avatar answered Oct 29 '25 07:10

masnider