# EcsVolume

Represents a Volume that can be mounted to a container that uses ECS.

Superclass of: [EfsVolume](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_batch.EfsVolume.html), [HostVolume](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_batch.HostVolume.html)
Used as argument in: [IEcsContainerDefinition.addVolume](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_batch.IEcsContainerDefinition.html#addvolume)

- Source: [`aws-batch/lib/ecs-container-definition.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-batch/lib/ecs-container-definition.ts#L134)
- Type last changed: v2.96.0 · 13 Sep 2023

## Example

TypeScript:

```ts
declare const myFileSystem: efs.IFileSystem;
declare const myJobRole: iam.Role;
myFileSystem.grantRead(myJobRole);

const jobDefn = new batch.EcsJobDefinition(this, 'JobDefn', {
  container: new batch.EcsEc2ContainerDefinition(this, 'containerDefn', {
    image: ecs.ContainerImage.fromRegistry('public.ecr.aws/amazonlinux/amazonlinux:latest'),
    memory: cdk.Size.mebibytes(2048),
    cpu: 256,
    volumes: [batch.EcsVolume.efs({
      name: 'myVolume',
      fileSystem: myFileSystem,
      containerPath: '/Volumes/myVolume',
      useJobRole: true,
    })],
    jobRole: myJobRole,
  }),
});
```

Python:

```python
# my_file_system: efs.IFileSystem
# my_job_role: iam.Role

my_file_system.grant_read(my_job_role)

job_defn = batch.EcsJobDefinition(self, "JobDefn",
    container=batch.EcsEc2ContainerDefinition(self, "containerDefn",
        image=ecs.ContainerImage.from_registry("public.ecr.aws/amazonlinux/amazonlinux:latest"),
        memory=cdk.Size.mebibytes(2048),
        cpu=256,
        volumes=[batch.EcsVolume.efs(
            name="myVolume",
            file_system=my_file_system,
            container_path="/Volumes/myVolume",
            use_job_role=True
        )],
        job_role=my_job_role
    )
)
```

Java:

```java
IFileSystem myFileSystem;
Role myJobRole;

myFileSystem.grantRead(myJobRole);

EcsJobDefinition jobDefn = EcsJobDefinition.Builder.create(this, "JobDefn")
        .container(EcsEc2ContainerDefinition.Builder.create(this, "containerDefn")
                .image(ContainerImage.fromRegistry("public.ecr.aws/amazonlinux/amazonlinux:latest"))
                .memory(Size.mebibytes(2048))
                .cpu(256)
                .volumes(List.of(EcsVolume.efs(EfsVolumeOptions.builder()
                        .name("myVolume")
                        .fileSystem(myFileSystem)
                        .containerPath("/Volumes/myVolume")
                        .useJobRole(true)
                        .build())))
                .jobRole(myJobRole)
                .build())
        .build();
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
IFileSystem myFileSystem;
Role myJobRole;

myFileSystem.GrantRead(myJobRole);

var jobDefn = new EcsJobDefinition(this, "JobDefn", new EcsJobDefinitionProps {
    Container = new EcsEc2ContainerDefinition(this, "containerDefn", new EcsEc2ContainerDefinitionProps {
        Image = ContainerImage.FromRegistry("public.ecr.aws/amazonlinux/amazonlinux:latest"),
        Memory = Size.Mebibytes(2048),
        Cpu = 256,
        Volumes = new [] { EcsVolume.Efs(new EfsVolumeOptions {
            Name = "myVolume",
            FileSystem = myFileSystem,
            ContainerPath = "/Volumes/myVolume",
            UseJobRole = true
        }) },
        JobRole = myJobRole
    })
});
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var myFileSystem IFileSystem
var myJobRole Role

myFileSystem.GrantRead(myJobRole)

jobDefn := batch.NewEcsJobDefinition(this, jsii.String("JobDefn"), &EcsJobDefinitionProps{
	Container: batch.NewEcsEc2ContainerDefinition(this, jsii.String("containerDefn"), &EcsEc2ContainerDefinitionProps{
		Image: ecs.ContainerImage_FromRegistry(jsii.String("public.ecr.aws/amazonlinux/amazonlinux:latest")),
		Memory: cdk.Size_Mebibytes(jsii.Number(2048)),
		Cpu: jsii.Number(256),
		Volumes: []EcsVolume{
			batch.EcsVolume_Efs(&EfsVolumeOptions{
				Name: jsii.String("myVolume"),
				FileSystem: myFileSystem,
				ContainerPath: jsii.String("/Volumes/myVolume"),
				UseJobRole: jsii.Boolean(true),
			}),
		},
		JobRole: myJobRole,
	}),
})
```

## Initializer

TypeScript:

```ts
new EcsVolume(options: EcsVolumeOptions)
```

Python:

```python
EcsVolume(options: EcsVolumeOptions)
```

Java:

```java
new EcsVolume(EcsVolumeOptions options)
```

C# / .NET:

```csharp
new EcsVolume(EcsVolumeOptions options)
```

Golang:

```go
NewEcsVolume(Options EcsVolumeOptions)
```

## Properties

- `containerPath`: string — The path on the container that this volume will be mounted to.

- `name`: string — The name of this volume.

- `readonly`: boolean = false — Whether or not the container has readonly access to this volume.

## Methods

- `efs(options: EfsVolumeOptions): EfsVolume` (static) — Creates a Volume that uses an AWS Elastic File System (EFS);

  this volume can grow and shrink as needed

- `host(options: HostVolumeOptions): HostVolume` (static) — Creates a Host volume.

  This volume will persist on the host at the specified `hostPath`.
  If the `hostPath` is not specified, Docker will choose the host path. In this case,
  the data may not persist after the containers that use it stop running.
