amazon-web-servicesamazon-elastic-beanstalkaws-codestar

Elastic Beanstalk environment lost when using SourceConfiguration


I've created a new WebApplication using Codestar on AWS and generally it's working pretty well. The big problem I've got right now, is that the environment variables which I set inside Beanstalk's software configuration don't persist between deployments.

I very quickly found that I can use SourceConfiguration inside template.yml to achieve this, so here is what I did:

  1. Deployed my app
  2. Once ready, I defined all my variables in software configuration and waited for the app to re-deploy
  3. Went to Actions -> Save Configuration, and saved everything successfully
  4. In my template.yml, I put the name of the new configuration and deployed the app again

The above process worked the first time I did it.

However when I make changes to the configuration, save them again under a new name, and redeploy the app with new SourceConfiguration it doesn't use the latest configuration I created and goes back to the previous one.

If I manually load the saved config after deployment, it successfully restores the env I set.

Am I missing something very obvious?

Here is my redacted template.yml in case I'm doing something wrong.

AWSTemplateFormatVersion: 2010-09-09
Transform:
- AWS::CodeStar

Resources:
  EBConfigurationTemplate:
    Description: The AWS Elastic Beanstalk configuration template to be created for this project, which defines configuration settings used to deploy different versions of an application.
    Type: AWS::ElasticBeanstalk::ConfigurationTemplate
    Properties:
      ApplicationName: !Ref 'EBApplication'
      Description: The name of the sample configuration template.
      OptionSettings:
      - Namespace: aws:elasticbeanstalk:environment
        OptionName: EnvironmentType
        Value: LoadBalanced
      - Namespace: aws:elasticbeanstalk:environment
        OptionName: ServiceRole
        Value: !Ref 'EBTrustRole'
      - Namespace: aws:elasticbeanstalk:healthreporting:system
        OptionName: SystemType
        Value: enhanced
      SolutionStackName: !Ref 'SolutionStackName'
      SourceConfiguration:
        ApplicationName: !Ref 'EBApplication'
        TemplateName: "my-saved-vars"      <---- This is where i define my old configuration

Solution

  • Turns out I was going totally wrong about all this.

    Environment should be set up in pipeline configuration and template.yml, not beanstalk itself.

    Redacted example:

    AWSTemplateFormatVersion: 2010-09-09
    Transform:
    - AWS::CodeStar
    
    Parameters:
      ApiPublicKey:
        Type: String
        Description: API Token
      ApiUrl:
        Type: String
        Description: API Url
    
    Resources:
      EBConfigurationTemplate:
        Description: The AWS Elastic Beanstalk configuration template to be created for this project, which defines configuration settings used to deploy different versions of an application.
        Type: AWS::ElasticBeanstalk::ConfigurationTemplate
        Properties:
          ApplicationName: !Ref 'EBApplication'
          Description: The name of the sample configuration template.
          OptionSettings:
          - Namespace: aws:elasticbeanstalk:application:environment
            OptionName: API_PUBLIC_KEY
            Value: !Ref 'ApiPublicKey'
          - Namespace: aws:elasticbeanstalk:application:environment
            OptionName: API_URL
            Value: !Ref 'ApiUrl'