简体   繁体   中英

AWS CDK add port mappings

I've been trying a lot of different things, but I can't seem to get this to work for me.

I'm trying to declare ports for my container in aws cdk within the ecs.TaskDefinition contruct.

I keep getting an error that the array type was expected even though I'm using the specififed ecs.PortMapping construct that is needed for the port_mappings parameter.

File "/home/user/.local/share/virtualenvs/AWS_Automation--K5ZV1iW/lib/python3.9/site-packages/aws_cdk/aws_ecs/__init__.py", line 27675, in add_container
    return typing.cast(ContainerDefinition, jsii.invoke(self, "addContainer", [id, props]))
  File "/home/user/.local/share/virtualenvs/AWS_Automation--K5ZV1iW/lib/python3.9/site-packages/jsii/_kernel/__init__.py", line 143, in wrapped
    return _recursize_dereference(kernel, fn(kernel, *args, **kwargs))
  File "/home/user/.local/share/virtualenvs/AWS_Automation--K5ZV1iW/lib/python3.9/site-packages/jsii/_kernel/__init__.py", line 355, in invoke
    response = self.provider.invoke(
  File "/home/user/.local/share/virtualenvs/AWS_Automation--K5ZV1iW/lib/python3.9/site-packages/jsii/_kernel/providers/process.py", line 359, in invoke
    return self._process.send(request, InvokeResponse)
  File "/home/user/.local/share/virtualenvs/AWS_Automation--K5ZV1iW/lib/python3.9/site-packages/jsii/_kernel/providers/process.py", line 326, in send
    raise JSIIError(resp.error) from JavaScriptError(resp.stack)
jsii.errors.JSIIError: Expected array type, got {"$jsii.struct":{"fqn":"aws-cdk-lib.aws_ecs.PortMapping","data":{"containerPort":8501,"hostPort":null,"protocol":null}}}

Any help would be appreciated. My relevant code is below.

from aws_cdk import (aws_ec2 as ec2, aws_ecs as ecs,
                     aws_ecs_patterns as ecs_patterns, 
                     aws_ecs as ecs,
                     aws_ecr as ecr, 
                     aws_route53 as route53,
                     aws_certificatemanager as certificatemanager,
                     aws_elasticloadbalancingv2 as elbv2)

container_port_mappings = ecs.PortMapping(container_port = 8501)

        task_def = ecs.TaskDefinition(self,
                                      'TD',
                                      compatibility = ecs.Compatibility.FARGATE,
                                      cpu = '512',
                                      memory_mib = '1024'
        )
        
        task_def.add_container("SL_container",
            image=ecs.ContainerImage.from_ecr_repository(_repo),
            port_mappings = container_port_mappings
        )       

port_mappings accepts a list of PortMapping objects:

container_port_mappings = [ecs.PortMapping(container_port = 8501)]

BTW, CDK supports Python type annotations , which help avoid these types of errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM