AWS CDK-创建AWS ECS服务时出现模糊错误

时间:2019-06-24 14:20:42

标签: amazon-web-services amazon-ecs aws-cdk

我具有以下CDK代码来创建ECS服务。

注意:堆栈尚未完全配置,特别是我没有Docker映像资产或群集没有任何容量。我的意图是建立基础架构所需的最少代码,如何对Cloudformation模板贡献不同的代码以及所选择的默认值。

CDK代码:

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

运行cdk synthcdk bootstrap(带有npm run build)会产生以下错误-

⇒  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

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```

2 个答案:

答案 0 :(得分:1)

您的资源的第一个参数应该是Stack(此)而不是App(范围)。

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

答案 1 :(得分:0)

我认为AWS CDK具有陡峭的学习曲线,使创建已经形成的cloudformation模板的任务更加艰巨。您可以尝试cloudkast尝试一下。这是一个在线cloudformation模板生成器。