Skip to content
v2.274.0

EnvironmentFile Class ​

Constructs for types of environment files.

Example ​

ts
declare const secret: secretsmanager.Secret;
declare const dbSecret: secretsmanager.Secret;
declare const parameter: ssm.StringParameter;
declare const taskDefinition: ecs.TaskDefinition;
declare const s3Bucket: s3.Bucket;

const newContainer = taskDefinition.addContainer('container', {
  image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
  memoryLimitMiB: 1024,
  environment: { // clear text, not for sensitive data
    STAGE: 'prod',
  },
  environmentFiles: [ // list of environment files hosted either on local disk or S3
    ecs.EnvironmentFile.fromAsset('./demo-env-file.env'),
    ecs.EnvironmentFile.fromBucket(s3Bucket, 'assets/demo-env-file.env'),
  ],
  secrets: { // Retrieved from AWS Secrets Manager or AWS Systems Manager Parameter Store at container start-up.
    SECRET: ecs.Secret.fromSecretsManager(secret),
    DB_PASSWORD: ecs.Secret.fromSecretsManager(dbSecret, 'password'), // Reference a specific JSON field, (requires platform version 1.4.0 or later for Fargate tasks)
    API_KEY: ecs.Secret.fromSecretsManagerVersion(secret, { versionId: '12345' }, 'apiKey'), // Reference a specific version of the secret by its version id or version stage (requires platform version 1.4.0 or later for Fargate tasks)
    PARAMETER: ecs.Secret.fromSsmParameter(parameter),
  },
});
newContainer.addEnvironment('QUEUE_NAME', 'MyQueue');
newContainer.addSecret('API_KEY', ecs.Secret.fromSecretsManager(secret));
newContainer.addSecret('DB_PASSWORD', ecs.Secret.fromSecretsManager(secret, 'password'));

Initializer ​

ts
new EnvironmentFile()
python
EnvironmentFile()
java
new EnvironmentFile()
csharp
new EnvironmentFile()
go
NewEnvironmentFile()

Methods ​

bind(scope: Construct): EnvironmentFileConfigdef bind(self, scope: Construct) -> EnvironmentFileConfigpublic EnvironmentFileConfig bind(Construct scope)public EnvironmentFileConfig Bind(Construct scope)func Bind(Scope Construct) EnvironmentFileConfig#
Called when the container is initialized to allow this object to bind to the stack.
1 parameter

Parameters

  • scopescopescopescopeScope — The binding scope.
fromAsset(path: string, options?: AssetOptions): AssetEnvironmentFile@staticmethod def from_asset(path: str, options: typing.Optional[AssetOptions] = None) -> AssetEnvironmentFilepublic static AssetEnvironmentFile fromAsset(String path, AssetOptions options)public static AssetEnvironmentFile FromAsset(string path, AssetOptions? options = null)func FromAsset(Path *string, Options AssetOptions) AssetEnvironmentFilestatic#
Loads the environment file from a local disk path.
1 parameter

Parameters

  • pathpathpathpathPath — Local disk path.
fromBucket(bucket: IBucket, key: string, objectVersion?: string): S3EnvironmentFile@staticmethod def from_bucket(bucket: IBucket, key: str, object_version: typing.Optional[str] = None) -> S3EnvironmentFilepublic static S3EnvironmentFile fromBucket(IBucket bucket, String key, String objectVersion)public static S3EnvironmentFile FromBucket(IBucket bucket, string key, string? objectVersion = null)func FromBucket(Bucket IBucket, Key *string, ObjectVersion *string) S3EnvironmentFilestatic#
Loads the environment file from an S3 bucket.
3 parameters · returns

Parameters

  • bucketbucketbucketbucketBucket — The S3 bucket.
  • keykeykeykeyKey — The object key.
  • objectVersionobject_versionobjectVersionobjectVersionObjectVersion — Optional S3 object version.

Returns: S3EnvironmentFile associated with the specified S3 object.