Search code examples
amazon-web-servicesamazon-ecsaws-cdk

AWS CDK - Vague error while creating an AWS ECS service


I have the following CDK code to create an ECS service.

Note: The stack is not fully configured as yet, specifically I don't have the docker image asset or any capacity attached to the cluster. My intention is to what minimum code is needed to set up the infrastructure, how different pieces of code contribute to the Cloudformation template and the defaults that are picked.

CDK code:

index.ts

#!/usr/bin/env node
import 'source-map-support/register';
import cdk = require('@aws-cdk/cdk');
import { HelloCdkStack } from '../lib/hello-cdk-stack';

const app = new cdk.App();
new HelloCdkStack(app, 'hellocdk-stack');

lib/hello-cdk-stack.ts

import cdk = require('@aws-cdk/cdk');
import ecs = require('@aws-cdk/aws-ecs');
import ec2 = require('@aws-cdk/aws-ec2');

export class HelloCdkStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const td = new ecs.Ec2TaskDefinition(scope, 'hellocdk-taskdef');

    const vpc = new ec2.Vpc(scope, 'hellocdk-vpc');

    const cluster = new ecs.Cluster(scope, 'hellocdk-cluster', {
      vpc: vpc,
    });

    new ecs.Ec2Service(scope, 'hellocdk-service', {
      taskDefinition: td,
      cluster: cluster,
    });
  }
}

Running cdk synth or cdk bootstrap (with an npm run build) generates the following error -

⇒  cdk bootstrap

/Users/nija/workplace/cdk/hello-cdk/node_modules/@aws-cdk/cdk/lib/stack.ts:65
        throw new Error(`No stack could be identified for the construct at path ${construct.node.path}`);
              ^
Error: No stack could be identified for the construct at path hellocdk-taskrole/Resource
    at _lookup (/Users/nija/workplace/cdk/hello-cdk/node_modules/@aws-cdk/cdk/lib/stack.ts:65:15)
    at _lookup (/Users/nija/workplace/cdk/hello-cdk/node_modules/@aws-cdk/cdk/lib/stack.ts:68:14)
    at _lookup (/Users/nija/workplace/cdk/hello-cdk/node_modules/@aws-cdk/cdk/lib/stack.ts:68:14)
    at Function.of (/Users/nija/workplace/cdk/hello-cdk/node_modules/@aws-cdk/cdk/lib/stack.ts:57:12)
    at new CfnElement (/Users/nija/workplace/cdk/hello-cdk/node_modules/@aws-cdk/cdk/lib/cfn-element.ts:58:24)
    at new CfnRefElement (/Users/nija/workplace/cdk/hello-cdk/node_modules/@aws-cdk/cdk/lib/cfn-element.ts:165:1)
    at new CfnResource (/Users/nija/workplace/cdk/hello-cdk/node_modules/@aws-cdk/cdk/lib/cfn-resource.ts:90:5)
    at new CfnRole (/Users/nija/workplace/cdk/hello-cdk/node_modules/@aws-cdk/aws-iam/lib/iam.generated.ts:946:9)
    at new Role (/Users/nija/workplace/cdk/hello-cdk/node_modules/@aws-cdk/aws-iam/lib/role.ts:212:18)
    at new HelloCdkStack (/Users/nija/workplace/cdk/hello-cdk/lib/hello-cdk-stack.ts:10:22)
Subprocess exited with error 1

Output of cdk doctor -

⇒  cdk doctor
ℹ️ CDK Version: 0.35.0 (build c5e43e2)
ℹ️ AWS environment variables:
  - AWS_ACCESS_KEY_ID = ASIA<redacted>
  - AWS_SECRET_ACCESS_KEY = <redacted>
  - AWS_SESSION_TOKEN = <redacted>
ℹ️ No CDK environment variables```

Solution

  • The first parameter of your Resources should be the Stack(this) not App(scope).

    export class HelloCdkStack extends cdk.Stack {
      constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
        super(scope, id, props);
    
        const td = new ecs.Ec2TaskDefinition(this, 'hellocdk-taskdef');
    
        const vpc = new ec2.Vpc(this, 'hellocdk-vpc');
    
        const cluster = new ecs.Cluster(this, 'hellocdk-cluster', {
          vpc: vpc,
        });
    
        new ecs.Ec2Service(this, 'hellocdk-service', {
          taskDefinition: td,
          cluster: cluster,
        });
      }
    }