Stas Dashkovsky
Stas Dashkovsky

Reputation: 111

Configuring ASP.NET application on AWS Elastic Beanstalk

I need to deploy the application to different environments in AWS Beanstalk. So, I can create the bundle using MSBuild, upload zip file to S3 bucket, and deploy to AWS EB. However, I want to change certain web.config parameters depending on environment. E.g I want to change connection strings. Right now I'm changing these settings in my web.config manually using RDP or have to create the new package with changed config. But I want to have Continuous Deployment and promotion between environments, so these options do not work.

Upvotes: 1

Views: 940

Answers (1)

user4478810
user4478810

Reputation:

To do that you can use a configuration script (.config) + some aws environment variables.

The idea behind that is pretty straight forward. You get the environment name from the aws settings using the function Fn::GetOptionSetting. Then you can use powershell to edit files for example.

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/ebextensions.html

I'm using a similar approach to set the windows environment variable in asp.net core. The script below creates a PWS script and simply executes it.

I have attached the script I use as an example:

files:
   "c:/cfn/set-aspnetcore-environment.ps1":
       content: |
          $envtype = "`{"Fn::GetOptionSetting": {"Namespace": "aws:elasticbeanstalk:application:environment", "OptionName": "AspNetCoreEnv"}}`"
          $envtype | Out-File c:\cfn\set-aspnetcore-environment.txt 
          [Environment]::SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", $envtype, "Machine")
commands:
    01_writeenv:
       command: powershell.exe -ExecutionPolicy Bypass -File c:\\cfn\\set-aspnetcore-environment.ps1

Upvotes: 3

Related Questions