Reputation: 2634
When I call DescribeServies
I am expecting an array of tasks arn or ids of tasks associated with my service. However that is not the case, unless I inspect the events
object:
Events: [
{
CreatedAt: 2020-07-03 03:57:56 +0000 UTC,
Id: "af62a356-2ab7-4b18-b51c-718bef02901c",
Message: "(service 0271a020-aa1e-4b72-a57a-6d91c5f463e4) has reached a steady state."
},
{
CreatedAt: 2020-07-03 03:57:44 +0000 UTC,
Id: "de2a4fad-8ea8-419d-8493-8c7cb5406afb",
Message: "(service 0271a020-aa1e-4b72-a57a-6d91c5f463e4) has stopped 1 running tasks: (task 1f92882b-72d0-490b-aa03-18fd32edb2e8)."
},
{
CreatedAt: 2020-07-03 01:35:09 +0000 UTC,
Id: "bd8da7f5-9bb1-4d23-b416-f79678b487a8",
Message: "(service 0271a020-aa1e-4b72-a57a-6d91c5f463e4) has reached a steady state."
},
{
CreatedAt: 2020-07-03 01:34:40 +0000 UTC,
Id: "da05f6a0-4163-4d93-b7af-22297d307620",
Message: "(service 0271a020-aa1e-4b72-a57a-6d91c5f463e4) has started 1 tasks: (task e132a2a7-ad49-4436-88cb-07b2ce399dd2)."
},
{
CreatedAt: 2020-07-02 14:29:21 +0000 UTC,
Id: "cb56a4ff-de6a-402f-aaa5-377bf236b00a",
Message: "(service 0271a020-aa1e-4b72-a57a-6d91c5f463e4) has started 1 tasks: (task e69ca53b-7b5c-4c7d-aed2-b28d9261b111)."
}
]
Which as you can see it is quite tricky to extract the task id
My end goal is to programmatically get the IP of the instance where the service has been deployed
My current clunky solution: (Errors handling is omitted for brevity)
servicesResponse, _ := s.ecsClient.DescribeServices(describeServicesParams)
var taskId string
for _, serviceEvent := range servicesResponse.Services[0].Events {
if strings.Contains(*serviceEvent.Message, "has stopped 1 running tasks") {
break
}
if strings.Contains(*serviceEvent.Message, "has started 1 tasks") {
found := strings.Split(*serviceEvent.Message, "has started 1 tasks: (task ")[1]
taskId = strings.Replace(found, ").", "", -1)
break
}
}
log.Info("taskId", taskId)
After that I call DescribeTasks
to get the containerInstanceArn
and with that I call DescribeInstances
Upvotes: 1
Views: 796
Reputation: 60056
Here is the working example that is base on AWS-CLI, you can convert that accordingly on the desired language.
My end goal is to programmatically get the IP of the instance where the service has been deployed
#!/bin/bash
CLUSTER_NAME="mycluster"
SERVICE_NAME="app-service"
REGION="us-west-2"
echo "[INFO] Fetching tasks list for Cluster: $CLUSTER_NAME and Service: $SERVICE_NAME"
TASKS_ARN=($(aws ecs list-tasks --cluster $CLUSTER_NAME --service $SERVICE_NAME --query 'taskArns[]' --output text))
for task in ${!TASKS_ARN[@]}; do
container_instances=$(aws ecs describe-tasks --cluster $CLUSTER_NAME --tasks ${TASKS_ARN[$task]} --query 'tasks[].containerInstanceArn' --output text)
ec2_instance_id=$(aws ecs describe-container-instances --cluster $CLUSTER_NAME --container-instances $container_instances --region $REGION --query 'containerInstances[*].ec2InstanceId' --output text)
ec2_ip=$(aws ec2 describe-instances --instance-id $ec2_instance_id --query 'Reservations[].Instances[].PublicIpAddress' --output text)
echo "[INFO] Task having ARN ${TASKS_ARN[$task]} runnng on EC2 machine having IP: $ec2_ip"
echo "[INFO] Fetchec completed for task number $(($task+1)) out of ${#TASKS_ARN[@]},having Task ARN: ${TASKS_ARN[$task]}"
done
get_instance_ip_for_ecs_service.sh
Upvotes: 0
Reputation: 238587
DescribeTasks
as you noted, its not very well suited to get the tasks IDs in a service.
Normally, to get the lists of tasks in a service you would use ListTasks call. For the call you can specify the service name of interest:
serviceName: The name of the service with which to filter the ListTasks results. Specifying a serviceName limits the results to tasks that belong to that service.
The ListTasks
will give you taskArns
which in turn can be used in DescribeTasks to get tasks details.
The task id is a part of its arn
, so if you only want ids, you can extract them from the arns.
Upvotes: 1