Skip to content

Instantly share code, notes, and snippets.

@y16ra
Created July 30, 2024 00:35
Show Gist options
  • Save y16ra/9f466dff99e41d04d8f35dc6a7f10d26 to your computer and use it in GitHub Desktop.
Save y16ra/9f466dff99e41d04d8f35dc6a7f10d26 to your computer and use it in GitHub Desktop.
This script provides to run a new task using the configuration of an existing ECS service and wait for its completion.
CLUSTER_NAME=$YOUR_CLUSTER_NAME
TASK_DEF_NAME=$YOUR_TASK_NAME
SERVICE_NAME=$YOUR_SERVICE_NAME
task_arn=$(aws ecs list-task-definitions --family-prefix "$TASK_DEF_NAME" --query "reverse(taskDefinitionArns)[0]" --output text)
# Describe the ECS service to get network configuration details
network_config=$(aws ecs describe-services --cluster $CLUSTER_NAME --services $SERVICE_NAME --query 'services[0].networkConfiguration.awsvpcConfiguration' --output json)
# Extract subnets and security groups
subnets=$(echo "$network_config" | jq -r '.subnets | join(",")')
security_groups=$(echo "$network_config" | jq -r '.securityGroups | join(",")')
# Run the ECS task
task_arn=$(aws ecs run-task \
--cluster $CLUSTER_NAME \
--task-definition "$task_arn" \
--network-configuration awsvpcConfiguration="{subnets=[$subnets],securityGroups=[$security_groups],assignPublicIp=ENABLED}" --launch-type FARGATE \
--query "tasks[0].taskArn" --output text)
echo "Task ARN: $task_arn"
aws ecs wait tasks-stopped --cluster $CLUSTER_NAME --tasks "$task_arn"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment