简体   繁体   English

从 AWS CDK 中的 Fargate 集群获取所有服务

[英]Get all Services from a Fargate Cluster in AWS CDK

I need to be able to get all of the Services given only the Cluster construct in AWS CDK (TypeScript in this example but any language will do).我需要能够在 AWS CDK 中仅给定 Cluster 构造(本例中为 TypeScript,但任何语言都可以)获得所有服务。

Something like the following.像下面这样的东西。

import { Cluster, FargateService } from '@aws-cdk/aws-ecs';

private updateClusterServices(cluster: Cluster): void {
  for (const service of cluster.getServices()) {
    // do something with service
  }
}

However there is no such getServices() method on Cluster .但是Cluster上没有这样的getServices()方法。

Is there another way to do this or is this fundamentally not possible with CDK for some reason?有没有另一种方法可以做到这一点,或者由于某种原因,CDK 根本不可能做到这一点?

Turns out there is a way to do something like this if you at least have the Service name too.事实证明,如果您至少也有服务名称,那么有一种方法可以做这样的事情。

import { Cluster, FargateService } from '@aws-cdk/aws-ecs';

private updateClusterServices(scope: Construct, cluster: Cluster, serviceName: string): void {
  const service = FargateService.fromFargateServiceAttributes(scope, 'FindFargateServices', {
    cluster: cluster,
    serviceName: serviceName,
  });

  console.log(service.serviceArn);

  // do something with service
}

The result appears to be the TaskSet serviceArn.结果似乎是TaskSet serviceArn。

arn:aws:ecs:ap-southeast-2:1234567890:service/backend

From comparing it with the CLI output.通过将其与 CLI output 进行比较。

$ aws ecs describe-services --cluster blue-green-cluster --services backend
{
    "services": [
        {
            "serviceArn": "arn:aws:ecs:ap-southeast-2:1234567890:service/blue-green-cluster/backend",
            "serviceName": "backend",
            "clusterArn": "arn:aws:ecs:ap-southeast-2:1234567890:cluster/blue-green-cluster",
...
            "taskSets": [
                {
                    "taskSetArn": "arn:aws:ecs:ap-southeast-2:1234567890:task-set/blue-green-cluster/backend/ecs-svc/2091139980078414071",
                    "serviceArn": "arn:aws:ecs:ap-southeast-2:1234567890:service/backend",
                    "clusterArn": "arn:aws:ecs:ap-southeast-2:1234567890:cluster/blue-green-cluster",
...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM