Best way to update environment variables?

In a flow I used before cfn-modules, I would update a resource for a task definition to add new environment variables. However, adding or removing environment variables does no update the task definition.

Is the best flow to add the env var to the template.yaml and then manually add/remove it to the task definition?

Ho Jason!

Thanks for posting your question!

Can you post a small snippet that demonstrates what you do?

Usually, if you change the Environment of a container definition a new task definition revision is created (you can not update them without creating a new revision).

Translated to the fargate-service module: If you change any of:

  • ProxyEnvironment[N]Key
  • ProxyEnvironment[N]Value
  • ProxyEnvironment[N]SecretModule
  • AppEnvironment[N]Key
  • AppEnvironment[N]Value
  • AppEnvironment[N]SecretModule
  • SidecarEnvironment[N]Key
  • SidecarEnvironment[N]Value
  • SidecarEnvironment[N]SecretModule

… you create a new task definition revision.

Warning: You should NEVER update infrastructure manually that is managed by CloudFormation! This can result in a catastrophe.


I am using the code provided in the Rapid Docker on AWS for the PHP basic example. In the AppService resource, there are environment variables used like so:

Type: 'AWS::CloudFormation::Stack'
    VpcModule: !GetAtt 'Vpc.Outputs.StackName'
    ClusterModule: !GetAtt 'Cluster.Outputs.StackName'
    TargetModule: !GetAtt 'AppTarget.Outputs.StackName'
    AlertingModule: !GetAtt 'Alerting.Outputs.StackName'
    ClientSgModule1: !GetAtt 'AuroraServerlessClientSg.Outputs.StackName'
    ProxyImage: !Ref ProxyImage
    ProxyPort: '80'
    AppImage: !Ref AppImage
    AppPort: '9000'
    AppEnvironment1Key: 'DB_PASSWORD'
    AppEnvironment1SecretModule: !GetAtt 'DatabaseSecret.Outputs.StackName'
    AppEnvironment2Key: 'DB_HOST'
    AppEnvironment2Value: !GetAtt 'AuroraServerlessCluster.Outputs.DnsName'
    AppEnvironment3Key: 'DB_NAME'
    AppEnvironment3Value: 'dbname'
    AppEnvironment4Key: 'DB_USER'
    AppEnvironment4Value: 'dbuser'
    Cpu: '0.25'
    Memory: '0.5'
    DesiredCount: '2'
    MaxCapacity: '10'
    MinCapacity: '1'
    LogsRetentionInDays: '14'
  TemplateURL: './node_modules/@cfn-modules/fargate-service/module.yml'

However, if I an an environment variable there, it does not update the Task Definition. Perhaps I need to add a new Fargate resource and reference that instead?

What value exactly have you changed?
Have you run aws cloudformation package ... & aws cloudformation deploy ... after your change?

I added a new environment variable, as well as modified the names of the existing variables to match my application.

I guess maybe what I am missing from the templates provided in the book is a task definition?

Nevermind, I see what I did wrong. Thank you!

Hi jason,

the TaskDefinition is created by the module itself based on your input (including environment variables).

Can you share with us what solved the issue for you?