Adding environment variable with AWS CloudFormation resets AWS Beanstalk app

wsgipath elastic beanstalk
elastic beanstalk static files
elastic beanstalk launch configuration
elastic beanstalk entry point
wsgipassauthorization on elastic beanstalk
elastic beanstalk memory limit
aws elastic beanstalk create environment
elastic beanstalk container default

I have an AWS Beanstalk application and environment created using the following CloudFormation template:

    Type: 'AWS::ElasticBeanstalk::Application'
        ApplicationName: MyAppName

    Type: 'AWS::ElasticBeanstalk::ApplicationVersion'
        ApplicationName: !Ref MyApp
        Description: Version 1.0
            S3Bucket: !Sub 'elasticbeanstalk-samples-${AWS::Region}'

    Type: 'AWS::ElasticBeanstalk::Environment'
        ApplicationName: !Ref MyApp
        Description: staging
        TemplateName: !Ref AppConfigurationTemplate
        VersionLabel: !Ref InitialApplication

    Type: 'AWS::ElasticBeanstalk::ConfigurationTemplate'
        - MySecurityGroup
        ApplicationName: !Ref MyApp
        Description: My Configuration Template
        SolutionStackName: '64bit Amazon Linux 2018.03 v2.11.2 running Multi-container Docker 18.03.1-ce (Generic)'
            # Lots of options here

            # Application environment variables
            - Namespace: aws:elasticbeanstalk:application:environment
              OptionName: MY_APP_OPTION
              Value: SOME_VALUE

The problem is that if I add an environment variable to my application (i.e. in the aws:elasticbeanstalk:application:environment namespace), Beanstalk resets the application version of the environment to the initial application. So suppose I created the CloudFormation stack a year ago and have since deployed 50 versions of my app... If I then add an environment variable through CloudFormation, the environment's application is reset to the sample application. This will of course break everything, as databases have changed, etc. What I want, is of course to add/modify environment variables without changing the deployed application version within the Beanstalk environment.

Why does Beanstalk reset my application when making this change, and is there any way to prevent it?

Thanks in advance!

An important concept to understand here is why actually using CloudFormation to deploy your app, if you can initialize and then upload new versions of your app through the EB Console (as you state you were doing) or the EB API. CloudFormation is kind of a "recipe" for your application and it's designed to keep the infrastructure definition under source control. This is the reason why you were observing a rollback when updating only the environmental variables part in your "recipe" - the changes made in CloudFormation were triggering an update in the app and definitions from the template were applied - where the old app version was hard-coded. This is why ideally your CloudFormation template should be parametrized - each time you want to make an update in your app, you should pass the newest version name to the template.

Of course you can manually update the EB version in its Console, but in that case the whole idea of using CloudFormation becomes pointless and will lead to complications that you are observing.

As you specified in the comment, you can be uploading new app versions to S3 and then updating CloudFormation through the API passing the version as a parameter. This way the updates can be automatized. An example API call to make some updates in the stack (explanation in the AWS docs):

aws cloudformation update-stack --stack-name mystack --template-url
--parameters ParameterKey=VPCID,ParameterValue=SampleVPCID ParameterKey=SubnetIDs,ParameterValue=SampleSubnetID1\\,UpdatedSampleSubnetID2

In your case the parameter to update will be AWS::ElasticBeanstalk::ApplicationVersion and especially the SourceBundle.

OLD ANSWER (updating the app in the EB Console, not touching CloudFormation)

I understand that you do not deploy the application using the Cloud Formation template (as it was not updated for a year), therefore you must be doing it programatically with eb deploy or in the Elastic Beanstalk Console. Why not updating the environmental variables there in the console, then?

You can do that by going to the app's console, clicking on "Configuration" in the menu on the left, then the "Modify" button in the "Software" section. In the bottom you can add environmental variables that will exist in your app after each new deployment.

Adding Beanstalk environment variable, If I add an environment variable to the app through CloudFormation, it's reset to the sample application, because the new Beanstalk application  The AWS::ElasticBeanstalk::Environment resource is an AWS Elastic Beanstalk resource type that specifies an Elastic Beanstalk environment.

Keeping cloudformation up to date constantly is best practice and certainly recommended. If you don’t do this then you will end up in awkward scenarios like you are now experiencing, it’s unfortunate but fixable. You will need to update the bundled source inside the CF template to the most up to date version so that it will deploy, it may require to rebuild the infra as it’s a big change that it doesn’t know has happened

Environment properties and other software settings, elastic beanstalk config.yml environment variables aws:elasticbeanstalk:​application:environment cloudformation aws cli elastic beanstalk environment variables The OptionSettings property of the AWS::ElasticBeanstalk::Environment resource contains a list of OptionSetting property types. For a list of possible namespaces and option values, see Option Values in the AWS Elastic Beanstalk Developer Guide .

You need to execute the Environment Variable change through ChangeSet so that only enviorment variable change is executed and rest your stack remain as-is.

Elastic Beanstalk guide. Keywords: elastic bean beanstalk , Using CloudFormation; Blue/Green deployments; FAQ Google App Engine vs AWS Elastic Beanstalk; Popular commands; Supported The following is shows how to set up an environment variable as well as the autoscaling: true", "setup": "npm run reset-folders && cp -a env/$ENV/*.config .ebextensions && cp -a  To set configuration options during environment creation (Elastic Beanstalk console) Open the Elastic Beanstalk console, and then, in the Regions list, select your AWS Region. In the navigation pane, choose Applications. Choose or create an application.

AWS: aws_elastic_beanstalk_environment, tier - The environment tier specified. application – The Elastic Beanstalk Application specified for this environment. setting – Settings specifically set for this  To add a DB instance to your environment. Open the Elastic Beanstalk console, and then, in the Regions list, select your AWS Region. In the navigation pane, choose Environments, and then choose the name of your environment from the list.

[PDF] AWS Elastic Beanstalk, AWS resources created for the example application . For more information about setting environment variables for your application, see the section called. To configure environment properties in the Elastic Beanstalk console Open the Elastic Beanstalk console, and then, in the Regions list, select your AWS Region. In the navigation pane, choose Environments, and then choose the name of your environment from the list.

Customizing software on Windows servers, You may want to customize and configure the software that your application depends on. In Windows environments, Elastic Beanstalk supports downloading and installing (Optional) The name of a AWS CloudFormation authentication method to use. (Optional) Set environment variables prior to running the command,  A common misconception is that Elastic Beanstalk is difficult to work with because it is not customizable. I fell into this trap myself when I first took a look at AWS Elastic Beanstalk. After…