Displaying AppVersion for an ASP.Net Elastic Beanstalk Web Application
ASP.NET developers are most familiar with using configuration that is driven from the Web.config by adding a key-value pair to the
appSettings section. In AWS, Elastic Beanstalk Configuration allows a developer to create key-value pairs in the same way, but this configuration is driven directly from the Elastic Beanstalk configuration and interface.
AWS will substitute a configuration key that is set up in the Elastic Beanstalk console ONLY if it is NOT present in the deployed Web.config. So, in order to use the Elastic Beanstalk configuration, it is necessary to delete the existing appSetting keys prior to deployment. This requires the use of a Config Transform which can be set up by right-clicking the Web.config and selecting Add Config Transform. The Config Transform file (which is really just a specialized XSLT) will be named based on the build configuration name, in most cases (Debug or Release, for example), the Debug configuration will use
Once a Config Transform is added, open the Config Transform for each specific configuration, and add the following to the <appSettings/> section:
<add key="AppVersion" xdt:Transform="Remove" xdt:Locator="Match(key)"/>
<add key="BuildNum" xdt:Transform="Remove" xdt:Locator="Match(key)"/>
This instructs Visual Studio to remove the
BuildNum keys from the deployed
Web.config. Without these keys present in the
Web.config, during the deployment, AWS will locate these keys from within Elastic Beanstalk and substitute them in the deployed Web.config. Developers can continue to keep these in the regular Web.config for local development. This approach can be extended to include database connection strings, custom error configuration and anything else which is configuration from within the
Displaying the Application Version in the Web App
To display the Elastic Beanstalk driven configuration, developers use the same ConfigurationManager syntax to access and use any standard appSetting. The following is a snippet that can be added to the footer of your Web Application:
©2017 MyApp.com <span class="small-text"> v.@ConfigurationManager. AppSettings["AppVersion"] (Build @ConfigurationManager.AppSettings["BuildNum"])</span>
Deployment to an Existing Elastic Beanstalk Environment
New application versions can be deployed to Elastic Beanstalk directly from within Visual Studio. The instructions below assume you have installed the AWS Toolkit for Visual Studio (https://aws.amazon.com/visualstudio/) and configured with an AWS account profile that has access to deploy to AWS.
- Open the Visual Studio Solution (e.g. MyApp-Web.sln)
- Right click the Web project and select Publish to AWS… (Figure 1)
- From the dialog, click Redeploy to an existing environment and select an environment (Figure 2).
- Under Application Settings, the AWS Toolkit will generate a default Deployment Version Label which is based on the current date. You will want to inject a standard versioning scheme between the “v” and the unique deployment version. For example, if you are deploying version 2.1.0, then change v20160126125432 to v.2.1.0-20160126125432. (Figure 3)
- For first time deployments with the new version and build configuration, add an
BuildNumkey. (Figure 4)
AppVersionto the version you are deploying, in our example this would be set to 2.1..0.
BuildNumto generated AWS deployment version without the “v”, in our example above this would be set to 2016012612441.
- Confirm settings and Deploy (Figure 5)
Deploying an Application Version to a Subsequent Environment in the Release Cycle
Once a new version is ready to be released into an environment later in the cycle (e.g. TEST or PROD), you must use Elastic Beanstalk to deploy that Application Version to the new environment. There is a manual step to update the
BuildNum once the new Application Version is deployed. This can be automated in the future. Both steps are covered below.
Deploy an Application Version using the AWS Elastic Beanstalk console as follows:
- Log in to the AWS console with an account that has permission to access and deploy from within Elastic Beanstalk.
- Go to the Elastic Beanstalk Service and select MyApp | Application Versions.
- Select the checkbox next to the version you would like to deploy. The version label will match the Deployment version label that you set during the Visual Studio deployment.
- Click the Deploy button.
- From the dropdown, select the environment where you want to deploy this version.
Update the Configuration Variables
Once the new version is deployed, manually update the version and build number within the Elastic Beanstalk console as follows:
- Log in to AWS
- Select the environment where you just deployed the release
- Select Configuration
- Under Environment Properties, update the AppVersion and BuildNum values to match the deployed version. For example, if the deployment label is v.2.1.0-2016012612441, then set AppVersion to 2.1.0 and BuildNum to 2016012612441
- Click Apply. The environment will run through a brief update and will then be available.
While there are some manual steps with this approach (we will address automated build approaches in another blog post), this allows Application version and build # to be synchronized and coordinated between the Development environment and the deployed Elastic Beanstalk environments.