pulumi / pulumi-awsx

AWS infrastructure best practices in component form!
https://www.pulumi.com/docs/guides/crosswalk/aws/
Apache License 2.0
222 stars 104 forks source link

ECS Windows container instance dynamic port mapping is not supported? #461

Open jasonpatt opened 4 years ago

jasonpatt commented 4 years ago

I am attempting to create a Windows ECS service with dynamic port mapping of the container instances. The AWS documentation states that the hostPort in the container definition needs to be set to 0. (https://aws.amazon.com/premiumsupport/knowledge-center/dynamic-port-mapping-ecs/) However, pulumi always sets the hostPort equal to the containerPort. This will never work with dyanamic port mapping of Windows container instances.

I have found the line of code that is causing the problems, in container.ts:

`function convertMappings(mappings: aws.ecs.PortMapping[]) {
    const result: aws.ecs.PortMapping[] = [];
    for (const mapping of mappings) {
        const copy = { ...mapping };

        if (copy.hostPort === undefined) {
            // From https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html:
            // > For task definitions that use the awsvpc network mode, you should only specify
            // > the containerPort. The hostPort can be left blank or it must be the same value
            // > as the containerPort.
            //
            // However, if left blank, it will be automatically populated by AWS, potentially
            // leading to dirty diffs even when no changes have been made. Since we are
            // currently always using `awsvpc` mode, we go ahead and populate it with the same
            // value as `containerPort`.
            //
            // See https://github.com/terraform-providers/terraform-provider-aws/issues/3401.
            copy.hostPort = copy.containerPort;
        }

        result.push(copy);
    }

    return result;
}`

If I delete the line, copy.hostPort = copy.containerPort, then everything works.

My question, what is the best approach to support this use case?

I am happy to create a pull request to support this, I just need some guidance on the best strategy.

Any thoughts?

Thanks,

CyrusNajmabadi commented 4 years ago

Hey Jason, sorry for the delay on this. When specifying the container for the ECS service, can you not just do { portMappings: [{ hostPort: 0 }] }?

hemantgs commented 4 years ago

@CyrusNajmabadi I am facing a similar issue Doing the above creates a service without a Loadbalancer Is there any way to do this ?

jasonpatt commented 4 years ago

This is my workaround:

class WindowsEC2DynamicPortApplicationListener extends awsx.elasticloadbalancingv2.ApplicationListener{
    constructor(name: string, args: awsx.elasticloadbalancingv2.ApplicationListenerArgs, opts?: pulumi.ComponentResourceOptions) {        
        super(name, args, opts);
    }

    public containerPortMapping(name: string, parent: pulumi.Resource) {
        const portMapping = super.containerPortMapping(name, parent);
        const withHostPort = pulumi.output(portMapping).apply(pm => {
            return ({...pm, hostPort: 0});
        });
        return withHostPort;
    } 
}