public connectToService()

in lib/consul-mesh-extension.ts [556:615]


    public connectToService(otherService: Service, connectToProps: ConnectToProps = {}) {
        const otherConsulMesh = otherService.serviceDescription.get('consul') as ECSConsulMeshExtension;
        const serviceContainer = this.parentService.serviceDescription.get('service-container') as Container;

        if(serviceContainer == undefined){
            throw new Error(`Cannot find service-container`);
        }

        if (otherConsulMesh == undefined) {
            throw new Error(`Upstream service doesn't have consul mesh extension added to it`);
        }

        // Do a check to ensure that these services are in the same environment
        if (otherConsulMesh.parentService.environment.id !== this.parentService.environment.id) {
            throw new Error(`Unable to connect services from different environments`);
        }

        /**
         * Allow other service to accept traffic from parent service.
         * open port 20000 for proxy to route the traffic from parent service
         * to the other service
         */
        otherService.ecsService.connections.allowFrom(
            this.parentService.ecsService,
            Port.tcp(20000),
            `Accept inbound traffic from ${this.parentService.id}`,
        );

        const upstreamName = otherConsulMesh.serviceDiscoveryName;

        this.upstreamStringArray.push(upstreamName + ":" + (connectToProps.local_bind_port ?? this.upstreamPort));

        var cfnTaskDefinition = this.parentService?.ecsService?.taskDefinition?.node.defaultChild as ecs.CfnTaskDefinition;

        if (cfnTaskDefinition == undefined) {
            throw new Error(`The task definition is not defined`);
        }

        //Override command for consul-mesh-init-container here to have upstream details as we only use connectTo()
        //to add upstream details
        cfnTaskDefinition.addPropertyOverride('ContainerDefinitions.2.Command', ["mesh-init",
            "-envoy-bootstrap-dir=/consul/data",
            "-port=" + serviceContainer.trafficPort,
            "-upstreams=" + this.buildUpstreamString,
            "-health-sync-containers=" + this.healthSyncContainerName,
            "-checks=" + (JSON.stringify(this.consulChecks) ?? ""),
            "-service-name=" + this.serviceDiscoveryName]);


        if (this.parentServiceEnvironments.length == 0) { // add environment variables from app container only once
            for (const [key, val] of Object.entries(environment)) {
                this.parentServiceEnvironments.push({ Name: key, Value: val });
            }
        }

        this.parentServiceEnvironments.push({ Name: upstreamName.toUpperCase() + '_URL', Value: 'http://localhost:' + (connectToProps.local_bind_port ?? this.upstreamPort++)})

        //Also add required environment variables
        cfnTaskDefinition.addPropertyOverride('ContainerDefinitions.0.Environment', Array.from(this.parentServiceEnvironments.values()));
    }