Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(aws-ecs): expose environment from containerDefinition #17889

Merged
merged 15 commits into from
Dec 13, 2021
Merged

feat(aws-ecs): expose environment from containerDefinition #17889

merged 15 commits into from
Dec 13, 2021

Conversation

biffgaut
Copy link
Contributor

@biffgaut biffgaut commented Dec 7, 2021

Closes #17867

  • Assigned props.environment to a public readonly member
  • Added integration test that confirms the environment can be appended after the task is instantiated

Made 2 cosmetic, but no obvious changes. Environment values are specified:

name: value
name2: value

But in the test and the README.md files the sample values were:

name: something
value: something else

This is using the string 'value" as a key - which, as someone reading the code for the first time, was confusing. So I changed the sample values to more clearly display what's a key and what's a value.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@gitpod-io
Copy link

gitpod-io bot commented Dec 7, 2021

@mergify
Copy link
Contributor

mergify bot commented Dec 7, 2021

Title does not follow the guidelines of Conventional Commits. Please adjust title before merge.

@biffgaut biffgaut changed the title feature(Expose environment from containerDefinition): feat(aws-ecs): Expose environment from containerDefinition Dec 7, 2021
@github-actions github-actions bot added the @aws-cdk/aws-ecs Related to Amazon Elastic Container label Dec 7, 2021
@biffgaut biffgaut changed the title feat(aws-ecs): Expose environment from containerDefinition feat(aws-ecs): expose environment from containerDefinition Dec 7, 2021
Copy link
Contributor

@clarencetw clarencetw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great update. LGTM.

rix0rrr
rix0rrr previously requested changes Dec 8, 2021
Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the whole point of exposing this member field publicly is, so that it can be mutated: sorry, no dice.

First of all, this will not work across jsii boundaries (as dictionaries are serialized by value and not by reference there), and reaching into the state of another class is not typically a recommended design choice. It happens to work for this case (by accident), but a strategy like this will not work for all cases. For example, it will not work if the ContainerDefinition was directly represented as a resource in CloudFormation (try it on another class!).

The thing is: even though we don't have the mechanisms in place to enforce this, you are really really encouraged to treat those public members as readonly and immutable.

The better way to expose this feature is by providing an addEnvironment(key, value) method, like a Lambda Function does: https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.Function.html#addwbrenvironmentkey-value-options

@rix0rrr
Copy link
Contributor

rix0rrr commented Dec 8, 2021

Also.

You originally changed:

{
      name: 'I_WAS_TRIGGERED',
      value: 'From CloudWatch Events'
}

Into

{
      name: 'value',
      source: 'From CloudWatch Events'
}

Which I found confusing (name: 'value' threw me off here). I thought the reasoning behind this was that environment was a { name -> value } mapping, instead of a list of { name, value } records.

So in turn, I changed it to:

{
  I_WAS_TRIGGERED: 'From CloudWatch Events'
}

In order to preserve the original semantics of setting a single environment variable with a single value.

However, now that I changed it, I'm getting the following build error:

�[96m@aws-cdk.aws-ecs-README-L631.ts�[0m:�[93m52�[0m:�[93m7�[0m - �[91merror�[0m�[90m TS2322: �[0mType '{ I_WAS_TRIGGERED: string; }' is not assignable to type 'TaskEnvironmentVariable'.
  Object literal may only specify known properties, and 'I_WAS_TRIGGERED' does not exist in type 'TaskEnvironmentVariable'.

�[7m52�[0m       I_WAS_TRIGGERED: 'From CloudWatch Events'
�[7m  �[0m �[91m      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~�[0m
[jsii-rosetta] [WARN] 1 diagnostics from assemblies with 'strict' mode on (and 1 more non-strict diagnostics)

And it turns out that indeed, its not a map of { name -> value }, it is a list of records (which I should have seen, because the type IS a list):

export interface ContainerOverride {
  /**
   * Variables to set in the container's environment
   */
  readonly environment?: TaskEnvironmentVariable[];
}

/**
 * An environment variable to be set in the container run as a task
 */
export interface TaskEnvironmentVariable {
  /**
   * Name for the environment variable
   *
   * Exactly one of `name` and `namePath` must be specified.
   */
  readonly name: string;

  /**
   * Value of the environment variable
   *
   * Exactly one of `value` and `valuePath` must be specified.
   */
  readonly value: string;
}

https://github.com/aws/aws-cdk/blob/master/packages/@aws-cdk/aws-events-targets/lib/ecs-task-properties.ts/#L17

So I don't know why you didn't get an error for the original change (you should have), but the original code was correct, and all of our modifications are incorrect.

@biffgaut
Copy link
Contributor Author

biffgaut commented Dec 8, 2021

Thanks - nice object lesson for everyone to wait for AWS to comment on the original issue before writing code :-).

I will update (rewrite) and resubmit based on a simplified version of how Lambda handles the issue (Lambda has more complexity due to deployments at edge and extra CDK created environment variables).

@mergify mergify bot dismissed rix0rrr’s stale review December 8, 2021 14:23

Pull request has been modified.

@biffgaut
Copy link
Contributor Author

biffgaut commented Dec 8, 2021

I will leave the doc and example as they are as requested, but I think your example from ContainerOverride doesn't represent ContainerDefintionProps. Here's ContainerOverride:

export interface ContainerOverride {
  /**
   * Variables to set in the container's environment
   */
  readonly environment?: TaskEnvironmentVariable[];
}

Where environment is a list of { name: something, value: something } objects. But environment in ContainerDefinitionProps is not a list, it specifies each environment variable as a a new attribute:

readonly environment?: { [key: string]: string };

My intention (unless you request differently beforehand or upon review) is to have the signature of addEnvironmentVariables follow this paradigm. I'm not a fan of using attributes to represent a list (I like []), but it seems like addEnvironmentVariables should be consistent with how they are specified in props.

(I also need to dive into ContainerOverride, I'm not familiar with that and don't know if that should figure into my implementation)

Edited - I see what you mean now, the doc I changed in README was for ContainerOverride, not ContainerDefintion.

@rix0rrr
Copy link
Contributor

rix0rrr commented Dec 9, 2021

Edited - I see what you mean now, the doc I changed in README was for ContainerOverride, not ContainerDefintion.

Yes exactly.

The reason it's different for Events's RunEcsTask is because both the environment variable name, as well as the value, could be taken from the event message, and there's no other way to represent that (iirc)

rix0rrr
rix0rrr previously approved these changes Dec 9, 2021
@mergify mergify bot dismissed rix0rrr’s stale review December 9, 2021 13:06

Pull request has been modified.

@biffgaut
Copy link
Contributor Author

biffgaut commented Dec 9, 2021

FWIW - rixOrrr approved this, but before it merge it drifted from Master and refreshing it instigated the need for another approval (no change was made on my end besides the merge). I'm a first time contributor, so the workflows are not enabled yet.

rix0rrr
rix0rrr previously approved these changes Dec 10, 2021
@mergify mergify bot dismissed rix0rrr’s stale review December 10, 2021 16:56

Pull request has been modified.

@mergify
Copy link
Contributor

mergify bot commented Dec 13, 2021

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject89A8053A-LhjRyN9kxr8o
  • Commit ID: 2345d91
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit 4937cd0 into aws:master Dec 13, 2021
@mergify
Copy link
Contributor

mergify bot commented Dec 13, 2021

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this pull request Feb 21, 2022
Closes aws#17867

* Assigned props.environment to a public readonly member
* Added integration test that confirms the environment can be appended after the task is instantiated

Made 2 cosmetic, but no obvious changes. Environment values are specified:

name: value
name2: value

But in the test and the README.md files the sample values were:

name: something
value: something else

This is using the string 'value" as a key - which, as someone reading the code for the first time, was confusing. So I changed the sample values to more clearly display what's a key and what's a value.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ecs Related to Amazon Elastic Container
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(aws-ecs): Expose environment on containerDefinition
5 participants