且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AWS 创建失败:服务已经存在.(服务:AWSServiceDiscovery;状态代码:400;错误代码:ServiceAlreadyExists;请求 ID)

更新时间:2023-09-04 17:51:04

这里是完整的答案.当您创建 AWS ECS 服务并且您还为其选择了可选的 ServiceDiscovery 时,它在 Route53 中创建了命名空间(.local)和服务(与 ECS 服务同名).

当您删除 ECS 服务时集群,它不会自动删除 Route53 命名空间/服务条目.现在,您收到 Service already exists 错误,因为您的新 ECS 服务与 ServiceDiscovery 服务具有相同的名称.您有 3 个选择.1. 清理 Route53 命名空间和服务.2. 如果您想继续使用 ServiceDiscovery 功能,请重新使用 Route53 服务.3. 创建ECS服务时不要开启服务发现集成.

选项 1 -

  • 您无法通过控制台删除 Route53 ServiceDiscovery 命名空间和服务.您将需要使用 AWS CLI.(确保您已使用与创建 ECS 的账户相同的 AWS 密钥配置 CLI)

    I am new to AWS and encountered some problem while trying to create a EC2 service in a ECS cluster.

    I was able to successfully create a brand new service (service1) in cluster1, but afterwards, I decided to delete cluster1 and create cluster2. The problem came when I try to re-create service1 in cluster2. Whenever I try to add the service, I will get the following error

    creation failed: Service already exists. (Service: AWSServiceDiscovery; Status Code: 400; Error Code: ServiceAlreadyExists; Request ID: d854025e-ebcc-11e8-84ab-b3bac906f2ef)
    

    Does anyone know how to resolve this problem? I have tried deregistering the task definition but it didn't work. cluster1 has been deleted and there are no services in cluster2.

    Here is full answer. When you created AWS ECS Service and you have also selected optional ServiceDiscovery for it as well which created an namespace(.local) and service(with same name as ECS Service) in Route53.

    When you deleted ECS service & cluster, it won't automatically delete Route53 namespace/service entries. Now, you are getting Service already exists error since your new ECS Service matches with same name as ServiceDiscovery service. You have 3 options. 1. Clean up Route53 namespaces and services. 2. Re-use Route53 Service if you want to continue to use ServiceDiscovery feature. 3. Don't enable service discovery integration when you are creating ECS Service.

    Option 1 -

    • You cannot delete Route53 ServiceDiscovery namespaces and service via Console. You will need to use AWS CLI.(Make sure you have configured CLI with AWS Keys same as the account where you have created ECS) https://docs.aws.amazon.com/cli/latest/reference/servicediscovery/index.html
    • List the Namespaces & Services with CLI using following commands and sample output you should get.
    • aws servicediscovery list-services

    { "Services": [ { "Id": "srv-x4acveybedar32mv", "Arn": "arn:aws:servicediscovery:us-east-1:1234567890:service/srv-x4acveybedar32mv", "Name": "nginx" } ] }

    • aws servicediscovery list-namespaces

    { "Namespaces": [ { "Id": "ns-3yd7pskwsxhwlq67", "Arn": "arn:aws:servicediscovery:us-east-1:1234567890:namespace/ns-3yd7pskwsxhwlq67", "Name": "local", "Type": "DNS_PRIVATE" } ] }

    • Delete the Service first with command. aws servicediscovery delete-service --id "srv-x4acveybedar32mv" . Result is empty response from CLI.
    • Delete the namespace with command. aws servicediscovery delete-namespace --id "ns-3yd7pskwsxhwlq67"

    { "OperationId": "s573v5dr62yee5d7vbfvsy5h65ybxmoh-jossalgs" }

    • That's all. Now, you can re-create ECS services which you wanted.

    Note -

    Sometimes, DNS cleanup operations will take couple of minutes to reflect properly so just give always few minutes before retrying.

    Option 2 -

    • Re-use Route53 Services by selecting exiting one instead of creating with same name.