aws-cdk-lib.aws_ecs.EnvironmentFile

class EnvironmentFile

LanguageType name
.NETAmazon.CDK.AWS.ECS.EnvironmentFile
Gogithub.com/aws/aws-cdk-go/awscdk/v2/awsecs#EnvironmentFile
Javasoftware.amazon.awscdk.services.ecs.EnvironmentFile
Pythonaws_cdk.aws_ecs.EnvironmentFile
TypeScript (source)aws-cdk-lib » aws_ecs » EnvironmentFile

Implemented by AssetEnvironmentFile, S3EnvironmentFile

Constructs for types of environment files.

Example

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

new EnvironmentFile()

Methods

NameDescription
bind(scope)Called when the container is initialized to allow this object to bind to the stack.
static fromAsset(path, options?)Loads the environment file from a local disk path.
static fromBucket(bucket, key, objectVersion?)Loads the environment file from an S3 bucket.

bind(scope)

public bind(scope: Construct): EnvironmentFileConfig

Parameters

  • scope Construct — The binding scope.

Returns

  • EnvironmentFileConfig

Called when the container is initialized to allow this object to bind to the stack.


static fromAsset(path, options?)

public static fromAsset(path: string, options?: AssetOptions): AssetEnvironmentFile

Parameters

  • path string — Local disk path.
  • options AssetOptions

Returns

  • AssetEnvironmentFile

Loads the environment file from a local disk path.


static fromBucket(bucket, key, objectVersion?)

public static fromBucket(bucket: IBucket, key: string, objectVersion?: string): S3EnvironmentFile

Parameters

  • bucket IBucket — The S3 bucket.
  • key string — The object key.
  • objectVersion string — Optional S3 object version.

Returns

  • S3EnvironmentFile

Loads the environment file from an S3 bucket.