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

task_definition.add_volume: Error occuring during cdk synth with the module in cdk python #29517

Open
rtejwani1309 opened this issue Mar 16, 2024 · 4 comments
Labels
@aws-cdk/aws-ecs Related to Amazon Elastic Container bug This issue is a bug. p2 package/tools Related to AWS CDK Tools or CLI

Comments

@rtejwani1309
Copy link

Describe the bug

When trying to use task_definition.add_volume method in cdk python, it fails with the error below.

.venv)@b0be836c5e1c pythoncdk % cdk synth
Traceback (most recent call last):
  File "/Users//Desktop/pythoncdk/app.py", line 10, in <module>
    PythoncdkStack(app, "PythoncdkStack",
  File "/Users//Desktop/pythoncdk/.venv/lib/python3.11/site-packages/jsii/_runtime.py", line 118, in __call__
    inst = super(JSIIMeta, cast(JSIIMeta, cls)).__call__(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users//Desktop/pythoncdk/pythoncdk/pythoncdk_stack.py", line 52, in __init__
    task_definition.add_volume(volume)
TypeError: TaskDefinition.add_volume() takes 1 positional argument but 2 were given

Subprocess exited with error 1

Code used by me:

from aws_cdk import (
    # Duration,
    Stack,
    Size,
    aws_rds as rds,
    # aws_sqs as sqs,
    aws_ec2 as ec2,
    aws_ecs as ecs,
    aws_appconfig as aws_appconfig,
)

from constructs import Construct

class PythoncdkStack(Stack):

    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # The code that defines your stack goes here
        cluster = ecs.Cluster(self, '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)

Expected Behavior

Expected behaviour of this method is to add the volume and synthesize template without any issue. This is possible using typescript, only errors out in python.

Current Behavior

Error occurs during synth:

(.venv)@b0be836c5e1c pythoncdk % cdk synth
Traceback (most recent call last):
  File "/Users//Desktop/pythoncdk/app.py", line 10, in <module>
    PythoncdkStack(app, "PythoncdkStack",
  File "/Users/Desktop/pythoncdk/.venv/lib/python3.11/site-packages/jsii/_runtime.py", line 118, in __call__
    inst = super(JSIIMeta, cast(JSIIMeta, cls)).__call__(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users//Desktop/pythoncdk/pythoncdk/pythoncdk_stack.py", line 52, in __init__
    task_definition.add_volume(volume)
TypeError: TaskDefinition.add_volume() takes 1 positional argument but 2 were given

Reproduction Steps

Use the code below to reproduce the same:

from aws_cdk import (
    # Duration,
    Stack,
    Size,
    aws_rds as rds,
    # aws_sqs as sqs,
    aws_ec2 as ec2,
    aws_ecs as ecs,
    aws_appconfig as aws_appconfig,
)

from constructs import Construct

class PythoncdkStack(Stack):

    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # The code that defines your stack goes here
        cluster = ecs.Cluster(self, '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)

Possible Solution

NA

Additional Information/Context

NA

CDK CLI Version

2.133.0 (build dcc1e75)

Framework Version

aws-cdk-lib==2.133.0 constructs>=10.0.0,<11.0.0

Node.js Version

v20.0.0

OS

MacOs

Language

Python

Language Version

Python 3.11.7

Other information

NA

@rtejwani1309 rtejwani1309 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Mar 16, 2024
@github-actions github-actions bot added the package/tools Related to AWS CDK Tools or CLI label Mar 16, 2024
@pahud
Copy link
Contributor

pahud commented Mar 19, 2024

This works for me in TS:

export class DummyStack extends Stack {
  constructor(scope: Construct, id: string, props: StackProps) {
    super(scope, id, props);  
    
    const task = new ecs.FargateTaskDefinition(this, 'Task', { cpu: 512, memoryLimitMiB: 1024 });
    const sampleContainer = task.addContainer('container', {
      image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
      portMappings: [
        { containerPort: 80 },
      ],
    });

    const vol = new ecs.ServiceManagedVolume(this, 'Vol', {
      name: 'dummy',
    });

    vol.mountIn(sampleContainer, {
      containerPath: '/opt',
      readOnly: false,
    });

    task.addVolume(vol);

  } 
}

But this fails in Py

class PythoncdkStack(Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        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,
            )]
        )

        volume = ecs.ServiceManagedVolume(self, "EBSVolume",
            name="ebs1",
        )

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

        task_definition.add_volume(volume)

Traceback (most recent call last):
File "/Users/hunhsieh/repos/issue-triage-py/app.py", line 11, in
PythoncdkStack(app, 'PythoncdkStack')
File "/Users/hunhsieh/repos/issue-triage-py/.venv/lib/python3.9/site-packages/jsii/_runtime.py", line 118, in call
inst = super(JSIIMeta, cast(JSIIMeta, cls)).call(*args, **kwargs)
File "/Users/hunhsieh/repos/issue-triage-py/issue_triage_py/issue_triage_py_stack.py", line 63, in init
task_definition.add_volume(volume)
TypeError: add_volume() takes 1 positional argument but 2 were given

Subprocess exited with error 1

Both in 2.133.0.

Looks like a JSII issue but I am not sure. Will bring this up for review.

@pahud pahud added p1 @aws-cdk/core Related to core CDK functionality jsii This issue originates in jsii, or this feature must be implemented in jsii. and removed needs-triage This issue or PR still needs to be triaged. labels Mar 19, 2024
@clarkm5
Copy link

clarkm5 commented Apr 11, 2024

try

        task_definition.add_volume(name=volume.name)

@3pow
Copy link

3pow commented Apr 30, 2024

This is a problem in .Net also... works when

taskDefinition.AddVolume(new Amazon.CDK.AWS.ECS.Volume() { Name=volume.Name, ConfiguredAtLaunch = true })

@rix0rrr
Copy link
Contributor

rix0rrr commented Jun 3, 2024

task_definition.add_volume(volume)

This is not a JSII issue. Look at the error message:

TypeError: add_volume() takes 1 positional argument but 2 were given

As suggested by @clarkm5, the correct way to call this function is:

task_definition.add_volume(name='...', )

Why does it work in TypeScript?

The example is actually incorrect. The argument to addVolume() is Volume, which is a struct with some data fields, not a subclass of a Volume construct.

It just so happens that ServiceManagedVolume exposes the same properties as the properties that are expected by the taskDefinition.addVolume() call, which is why the written code here compiles successfully due to TypeScript's structural typing (even though I would bet dollars to donuts that code doesn't do the right thing):

taskDefinition.addVolume(volumeFromSnapshot);

The example should be rewritten, probably to look something like this (warning, untested and no domain knowledge!):

taskDefinition.addVolume({
  name: 'vol0',
  // Unsure how a ServiceManagedVolume needs to be attached to a TaskDefinition 
  // Refer to the service documentation for this. 
  dockerVolumeConfiguration: {
    
  }

  // Perhaps there needs to be a 'serviceManagedVolumeConfiguration' property here
});

@rix0rrr rix0rrr removed the jsii This issue originates in jsii, or this feature must be implemented in jsii. label Jun 3, 2024
@comcalvi comcalvi added @aws-cdk/aws-ecs Related to Amazon Elastic Container p2 and removed @aws-cdk/core Related to core CDK functionality p1 labels Sep 12, 2024
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 bug This issue is a bug. p2 package/tools Related to AWS CDK Tools or CLI
Projects
None yet
Development

No branches or pull requests

6 participants