AWS websocket 容器 cdk ecs

AWS websocket container cdk ecs

lass TestEcsConstruct extends core.Construct {
constructor(scope, id, props) {
    super(scope, id);

    const vpc = new ec2.Vpc(this, 'MyVpc', {
        enableDnsSupport: true,
        enableDnsHostnames: true,
        cidr: '10.0.0.0/16',
        maxAzs: 2,
        natGateways: 0,
        subnetConfiguration: [
            {
                cidrMask: 24,
                name: 'Public',
                subnetType: ec2.SubnetType.PUBLIC
            }]
    });
    const secGroup = new ec2.SecurityGroup(this, 'SecGroup',
        {
            vpc: vpc,
            description: 'Access to the ECS hosts that run containers',
            allowAllOutbound: true
        }
    );
    secGroup.connections.allowToAnyIpv4(new ec2.Port({
        protocol: ec2.Protocol.TCP,
        fromPort: 22,
        toPort: 22
    }), 'Ingress SSH from public sg');
    secGroup.connections.allowFromAnyIpv4(new ec2.Port({
        protocol: ec2.Protocol.TCP,
        fromPort: 22,
        toPort: 22
    }), 'Ingress SSH from public sg');
    secGroup.connections.allowToAnyIpv4(new ec2.Port({
        protocol: ec2.Protocol.TCP,
        fromPort: 8080,
        toPort: 8080
    }), 'Ingress Docker Port Open');
    secGroup.connections.allowFromAnyIpv4(new ec2.Port({
        protocol: ec2.Protocol.TCP,
        fromPort: 8080,
        toPort: 8080
    }), 'Ingress Docker Port Open');

    const cluster = new ecs.Cluster(this, 'Ec2Cluster', {
        vpc
    });
    const autoScaleGroup = cluster.addCapacity('DefaultAutoScalingGroup', {
        instanceType: ec2.InstanceType.of(ec2.InstanceClass.T2, ec2.InstanceSize.MICRO),
        keyName: 'default-keypair',
        minCapacity: 1,
        maxCapacity: 2,
        desiredCapacity: 1,
        // resourceSignalTimeout: core.Duration.minutes(5),
        // rollingUpdateConfiguration: {
        //     minInstancesInService: 1,
        //     maxBatchSize: 1,
        //     pauseTime: core.Duration.minutes(5),
        //     waitOnResourceSignals: true,
        //     suspendProcesses: [
        //         'HealthCheck',
        //         'ReplaceUnhealthy',
        //         'AZRebalance',
        //         'AlarmNotification',
        //         'ScheduledActions'
        //     ]
        // }
    });
    autoScaleGroup.addSecurityGroup(secGroup);

    // create a task definition with CloudWatch Logs
    const logging = new ecs.AwsLogDriver({
        streamPrefix: "websocket-app",
        logRetention: 365
    })

    const image = ecs.ContainerImage.fromAsset(path.join(path.resolve(), 'test-docker'));
    const taskDefinition = new ecs.Ec2TaskDefinition(this, "MyTaskDefinition");
    const container = taskDefinition.addContainer("AppContainer", {
        image: image,
        cpu: 256,
        memoryLimitMiB: 1024,
        environment: [('REGION', process.env.REGION),
        ('QUEUE_URL', core.Fn.importValue(
            'NetworkStack:ApiMsgQueueUrl'
        ))],
        logging: logging
    });
    container.addPortMappings({
        containerPort: 8080,
        hostPort: 8080
    });

    // Instantiate ECS Service with just cluster and image
    new ecs.Ec2Service(this, "Ec2Service", {
        cluster,
        taskDefinition
    });
}

}

我有这段代码,但似乎无法联系到 ecs 容器本地主机中的这个端点。这个例子只是为了展示一个可以从外部地址访问的开放端口,我真的很困惑我在这里遗漏了什么。似乎在我连接的容器内部只有一个 ecs 实例 docker 观察者,但没有其他 docker 实例 运行,我无法 docker 执行到确实存在的容器。请提供任何建议和/或帮助,我们将不胜感激。

这显然与内存为 1024 而不是 512 有关,在不相关的主题中我在相关代码段中配置我自己的 AutoScalingGroup 时使用了错误的图像。因此,如果有人在这方面遇到问题,请确保您的 machineImage 是正确的类型。

//'.addCapacity' creates the ASG
const prerenderCluster = new ecs.Cluster(this, 'prerenderCluster',{
  vpc,
});

prerenderCluster.addCapacity('DefaultAutoScalingGroup', {
  instanceType: ec2.InstanceType.of(ec2.InstanceClass.T2, ec2.InstanceSize.XLARGE),
  machineImage: new ecs.EcsOptimizedAmi(),

为了将来,这里有一个关于@thekevshow 正在谈论的关于正确 AMI 的示例。