Home > Mobile >  RoleArn not accessible for ECS Role in CDK
RoleArn not accessible for ECS Role in CDK

Time:01-31

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);

    }

CodePudding user response:

Your ecsService variable is of type FargateStack. It presumably extends cdk.Stack, which has no role property.

You are probably looking for the role on ecs.[Fargate | Ec2 ]Service's task definition. If so, expose the ECS Service as a public instance field in FargateStack:

// FargateStack.ts
export class FargateStack extends cdk.Stack {
  readonly service: ecs.FargateService;

  constructor(scope: Construct, id: string, props: MyFargateStackProps) {
    // ... code ...
    this.service = new ecs.FargateService(...)

The role is available on the service's task definition:

const { service } = new FargateStack(...)

const role: iam.Role = service.taskDefinition.taskRole
  •  Tags:  
  • Related