要获取AWS Fargate容器的API网关URL,您可以按照以下步骤进行操作:
以下是使用AWS CLI获取AWS Fargate容器的API网关URL的示例代码:
#!/bin/bash
# 获取容器任务的ARN
container_task_arn=$(aws ecs describe-tasks --cluster --tasks --query 'tasks[0].taskArn' --output text)
# 获取容器的ENI ID
eni_id=$(aws ecs describe-tasks --cluster --tasks --query 'tasks[0].attachments[0].details[?name==`networkInterfaceId`].value' --output text)
# 获取容器的Private IP
private_ip=$(aws ec2 describe-network-interfaces --network-interface-ids $eni_id --query 'NetworkInterfaces[0].PrivateIpAddress' --output text)
# 获取容器的端口映射信息
port_mappings=$(aws ecs describe-tasks --cluster --tasks --query 'tasks[0].containers[0].networkBindings')
# 遍历端口映射信息,找到所需的端口
for port_mapping in $(echo "$port_mappings" | jq -r '.[] | @base64'); do
_jq() {
echo ${port_mapping} | base64 --decode | jq -r ${1}
}
container_port=$(_jq '.containerPort')
host_port=$(_jq '.hostPort')
# 判断是否为API网关的端口(默认为80)
if [[ $host_port == "80" ]]; then
api_gateway_url="http://${private_ip}:${host_port}"
echo "API Gateway URL: ${api_gateway_url}"
break
fi
done
请注意,上述脚本假设您已经安装了AWS CLI,并正确配置了您的AWS凭证。您需要将
和
替换为您的集群名称和任务ID。