question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

LoadBalancedFargateService does not expose environment variables

See original GitHub issue

When using the higher level LoadBalancedFargateService construct to create a load balanced Fargate service, it’s not possible to configure the container at runtime with environment variables.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
eladbcommented, Jan 13, 2019

@PaulMaddox the reason the code above doesn’t work is because the TaskDef child is not a CfnTaskDefinition (L1), it’s a FargateTaskDefinition (L2). Furthermore, since ContainerDefinition is not an L1 resource (but rather part of another L1 resource), you can’t really assign it to containerDefinitions.

Anyway, here’s a way to achieve this:

const taskDef = nginxService.node
  .findAll()
  .find(x => x instanceof ecs.CfnTaskDefinition) as ecs.CfnTaskDefinition;

taskDef.addPropertyOverride('ContainerDefinitions.0.Environment', [
  { name: 'TEST_ENV', value: 'TEST_VALUE' }
]);

Not very elegant, and relies on the fact that the container definition you wish to override is the first in the array, but should work.

1reaction
PaulMaddoxcommented, Jan 13, 2019

Hi @SoManyHs - to start with, i was starting with just trying to set basic environment variables, but suspect customers would also want to use ECS Secrets longer term.

I would expect the API to mirror the way environment variables and secrets are provided in task definitions. For example:

// Create a nginx service
const nginxService = new ecs.LoadBalancedFargateService(this, 'NginxService', {
  cluster: cluster,
  image: ecs.ContainerImage.fromDockerHub('nginx:latest'),
  environment: [ { name: "TEST_ENV", value: "test value" } ],
  secrets: [ { name: "TEST_SECRET",  valueFrom: "arn:aws:ssm:region:aws_account_id:parameter/parameter_name" } ],
})

https://docs.aws.amazon.com/AmazonECS/latest/developerguide/specifying-sensitive-data.html

Read more comments on GitHub >

github_iconTop Results From Across the Web

LoadBalancedFargateService does not expose environment ...
Hi @SoManyHs - to start with, i was starting with just trying to set basic environment variables, but suspect customers would also want...
Read more >
aws-cdk/aws-ecs-patterns module - AWS Documentation
If you need to encrypt the traffic between the load balancer and the ECS tasks, you can set the targetProtocol to HTTPS ....
Read more >
python 3.x - Can't set environment variables for container in ...
It neither deploy s nor synth s. The CF template for the stack in cdk.out/ doesn't contain the env variables. – Raffael. Feb...
Read more >
awslabs/aws-cdk - Gitter
I've made a JSON file with each account id, then under that the variables corresponding to the accountid that would change in each,...
Read more >
Variables set in task definition are not available to the job with ...
Environment description. Running custom AWS Fargate executor. Used GitLab Runner version. Running with Running with gitlab-runner 14.1.0 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found