'RoleArn not accessible for ECS Role in CDK

In the following code snippet, I am getting the error while building the project. Eventhough I have defined ecs service some lines above the usage, why is the role being shown as undefined ?

TypeError: Cannot read property 'roleArn' of undefined

I can see that roleArn is an accessible field of Role. what am I doing wrong here ?

    let ecsService: FargateStack;
    
    .... (lines of code)

    const service = target.serviceDefinition;
    const serviceName = generateResourceName(service.shortName, stageName, airportCode, account, cell);
    ecsService = new FargateStack(app, serviceName, {
      softwareType: SoftwareType.LONG_RUNNING_SERVICE,
      ...(other params)
      httpsListener: cluster.httpsListner,
    });
    ecsService.addDependency(cluster);
    servicesStacks.push(ecsService);

    if (!awsAccountsWithOpenSearchCreated.has(accountRegion) && ecsService.serviceName == ServiceName.CONFIG_STORE){
      const openSearchStackName = generateResourceName('opensearch', stageName, airportCode, account, cell);
      const openSearchStack = new OpenSearchStack(app, openSearchStackName, {
        env: deploymentEnvironment,
        ... (other params)
        role: ecsService.role.roleArn
      });
      regionalResources.push(openSearchStack);

    }

FargateStack is defined like given below and is initialized in the constructor

export class FargateServiceStack extends DeploymentStack {
  public readonly role: Role;

  public readonly alarms: Alarm[];

  .....(lines of code)
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source