# ServiceManagedVolume

Represents a service-managed volume and always configured at launch.

Extends: [Construct](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.Construct.html) › ServiceManagedVolume
Implements: [IConstruct](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.IConstruct.html), [IDependable](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.IDependable.html)

- Source: [`aws-ecs/lib/base/service-managed-volume.ts`](https://github.com/aws/aws-cdk/blob/v2.277.0/packages/aws-cdk-lib/aws-ecs/lib/base/service-managed-volume.ts#L209)
- Type last changed: v2.122.0 · 18 Jan 2024

## Example

TypeScript:

```ts
declare const cluster: ecs.Cluster;
const taskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef');

const container = taskDefinition.addContainer('web', {
  image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
  portMappings: [{
    containerPort: 80,
    protocol: ecs.Protocol.TCP,
  }],
});

const volume = new ecs.ServiceManagedVolume(this, 'EBSVolume', {
  name: 'ebs1',
  managedEBSVolume: {
    size: Size.gibibytes(15),
    volumeType: ec2.EbsDeviceVolumeType.GP3,
    fileSystemType: ecs.FileSystemType.XFS,
    tagSpecifications: [{
      tags: {
        purpose: 'production',
      },
      propagateTags: ecs.EbsPropagatedTagSource.SERVICE,
    }],
  },
});

volume.mountIn(container, {
  containerPath: '/var/lib',
  readOnly: false,
});

taskDefinition.addVolume(volume);

const service = new ecs.FargateService(this, 'FargateService', {
  cluster,
  taskDefinition,
  minHealthyPercent: 100,
});

service.addVolume(volume);
```

Python:

```python
# cluster: ecs.Cluster

task_definition = ecs.FargateTaskDefinition(self, "TaskDef")

container = task_definition.add_container("web",
    image=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample"),
    port_mappings=[ecs.PortMapping(
        container_port=80,
        protocol=ecs.Protocol.TCP
    )]
)

volume = ecs.ServiceManagedVolume(self, "EBSVolume",
    name="ebs1",
    managed_ebs_volume=ecs.ServiceManagedEBSVolumeConfiguration(
        size=Size.gibibytes(15),
        volume_type=ec2.EbsDeviceVolumeType.GP3,
        file_system_type=ecs.FileSystemType.XFS,
        tag_specifications=[ecs.EBSTagSpecification(
            tags={
                "purpose": "production"
            },
            propagate_tags=ecs.EbsPropagatedTagSource.SERVICE
        )]
    )
)

volume.mount_in(container,
    container_path="/var/lib",
    read_only=False
)

task_definition.add_volume(volume)

service = ecs.FargateService(self, "FargateService",
    cluster=cluster,
    task_definition=task_definition,
    min_healthy_percent=100
)

service.add_volume(volume)
```

Java:

```java
Cluster cluster;

FargateTaskDefinition taskDefinition = new FargateTaskDefinition(this, "TaskDef");

ContainerDefinition container = taskDefinition.addContainer("web", ContainerDefinitionOptions.builder()
        .image(ContainerImage.fromRegistry("amazon/amazon-ecs-sample"))
        .portMappings(List.of(PortMapping.builder()
                .containerPort(80)
                .protocol(Protocol.TCP)
                .build()))
        .build());

ServiceManagedVolume volume = ServiceManagedVolume.Builder.create(this, "EBSVolume")
        .name("ebs1")
        .managedEBSVolume(ServiceManagedEBSVolumeConfiguration.builder()
                .size(Size.gibibytes(15))
                .volumeType(EbsDeviceVolumeType.GP3)
                .fileSystemType(FileSystemType.XFS)
                .tagSpecifications(List.of(EBSTagSpecification.builder()
                        .tags(Map.of(
                                "purpose", "production"))
                        .propagateTags(EbsPropagatedTagSource.SERVICE)
                        .build()))
                .build())
        .build();

volume.mountIn(container, ContainerMountPoint.builder()
        .containerPath("/var/lib")
        .readOnly(false)
        .build());

taskDefinition.addVolume(volume);

FargateService service = FargateService.Builder.create(this, "FargateService")
        .cluster(cluster)
        .taskDefinition(taskDefinition)
        .minHealthyPercent(100)
        .build();

service.addVolume(volume);
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
Cluster cluster;

var taskDefinition = new FargateTaskDefinition(this, "TaskDef");

var container = taskDefinition.AddContainer("web", new ContainerDefinitionOptions {
    Image = ContainerImage.FromRegistry("amazon/amazon-ecs-sample"),
    PortMappings = new [] { new PortMapping {
        ContainerPort = 80,
        Protocol = Protocol.TCP
    } }
});

var volume = new ServiceManagedVolume(this, "EBSVolume", new ServiceManagedVolumeProps {
    Name = "ebs1",
    ManagedEBSVolume = new ServiceManagedEBSVolumeConfiguration {
        Size = Size.Gibibytes(15),
        VolumeType = EbsDeviceVolumeType.GP3,
        FileSystemType = FileSystemType.XFS,
        TagSpecifications = new [] { new EBSTagSpecification {
            Tags = new Dictionary<string, string> {
                { "purpose", "production" }
            },
            PropagateTags = EbsPropagatedTagSource.SERVICE
        } }
    }
});

volume.MountIn(container, new ContainerMountPoint {
    ContainerPath = "/var/lib",
    ReadOnly = false
});

taskDefinition.AddVolume(volume);

var service = new FargateService(this, "FargateService", new FargateServiceProps {
    Cluster = cluster,
    TaskDefinition = taskDefinition,
    MinHealthyPercent = 100
});

service.AddVolume(volume);
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var cluster Cluster

taskDefinition := ecs.NewFargateTaskDefinition(this, jsii.String("TaskDef"))

container := taskDefinition.AddContainer(jsii.String("web"), &ContainerDefinitionOptions{
	Image: ecs.ContainerImage_FromRegistry(jsii.String("amazon/amazon-ecs-sample")),
	PortMappings: []PortMapping{
		&PortMapping{
			ContainerPort: jsii.Number(80),
			Protocol: ecs.Protocol_TCP,
		},
	},
})

volume := ecs.NewServiceManagedVolume(this, jsii.String("EBSVolume"), &ServiceManagedVolumeProps{
	Name: jsii.String("ebs1"),
	ManagedEBSVolume: &ServiceManagedEBSVolumeConfiguration{
		Size: awscdk.Size_Gibibytes(jsii.Number(15)),
		VolumeType: ec2.EbsDeviceVolumeType_GP3,
		FileSystemType: ecs.FileSystemType_XFS,
		TagSpecifications: []EBSTagSpecification{
			&EBSTagSpecification{
				Tags: map[string]*string{
					"purpose": jsii.String("production"),
				},
				PropagateTags: ecs.EbsPropagatedTagSource_SERVICE,
			},
		},
	},
})

volume.MountIn(container, &ContainerMountPoint{
	ContainerPath: jsii.String("/var/lib"),
	ReadOnly: jsii.Boolean(false),
})

taskDefinition.AddVolume(volume)

service := ecs.NewFargateService(this, jsii.String("FargateService"), &FargateServiceProps{
	Cluster: Cluster,
	TaskDefinition: TaskDefinition,
	MinHealthyPercent: jsii.Number(100),
})

service.AddVolume(volume)
```

## Initializer

TypeScript:

```ts
new ServiceManagedVolume(scope: Construct, id: string, props: ServiceManagedVolumeProps)
```

Python:

```python
ServiceManagedVolume(scope: Construct, id: str, props: ServiceManagedVolumeProps)
```

Java:

```java
new ServiceManagedVolume(Construct scope, String id, ServiceManagedVolumeProps props)
```

C# / .NET:

```csharp
new ServiceManagedVolume(Construct scope, string id, ServiceManagedVolumeProps props)
```

Golang:

```go
NewServiceManagedVolume(Scope Construct, Id *string, Props ServiceManagedVolumeProps)
```

## Construct Props

- `managedEBSVolume`: [ServiceManagedEBSVolumeConfiguration](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.ServiceManagedEBSVolumeConfiguration.html) = undefined — Configuration for an Amazon Elastic Block Store (EBS) volume managed by ECS.

- `name`: string (required) — The name of the volume.

  This corresponds to the name provided in the ECS TaskDefinition.

## Properties

- `config`: [ServiceManagedEBSVolumeConfiguration](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.ServiceManagedEBSVolumeConfiguration.html) — Volume configuration.

- `configuredAtLaunch`: boolean — configuredAtLaunch indicates volume at launch time, referenced by taskdefinition volume.

- `name`: string — Name of the volume, referenced by task definition and mount point.

- `node`: [Node](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.Node.html) — The tree node.

- `role`: [IRole](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IRole.html) — An IAM role that allows ECS to make calls to EBS APIs.

  If not provided, a new role with appropriate permissions will be created by default.

## Methods

- `isConstruct(x: any): boolean` (static) — Checks if `x` is a construct.

  Use this method instead of `instanceof` to properly detect `Construct`
  instances, even when the construct library is symlinked.

  Explanation: in JavaScript, multiple copies of the `constructs` library on
  disk are seen as independent, completely different libraries. As a
  consequence, the class `Construct` in each copy of the `constructs` library
  is seen as a different class, and an instance of one class will not test as
  `instanceof` the other class. `npm install` will not create installations
  like this, but users may manually symlink construct libraries together or
  use a monorepo tool: in those cases, multiple copies of the `constructs`
  library can be accidentally installed, and `instanceof` will behave
  unpredictably. It is safest to avoid using `instanceof`, and using
  this type-testing method instead.

  **Parameters**

  - `x` — Any object.

  **Returns:** true if `x` is an object created from a class which extends `Construct`.

- `mountIn(container: ContainerDefinition, mountPoint: ContainerMountPoint): void` — Mounts the service managed volume to a specified container at a defined mount point.

  **Parameters**

  - `container` — The container to mount the volume on.
  - `mountPoint` — The mounting point details within the container.

- `toString(): string` — Returns a string representation of this construct.

- `with(...mixins: IMixin[]): IConstruct` — Applies one or more mixins to this construct.

  Mixins are applied in order. The list of constructs is captured at the
  start of the call, so constructs added by a mixin will not be visited.
  Use multiple `with()` calls if subsequent mixins should apply to added
  constructs.

  **Parameters**

  - `mixins` — The mixins to apply.

  **Returns:** This construct for chaining
