docker - 从运行 ECS 任务的容器中检索元数据时出现连接错误
问题描述
我正在尝试检索在容器中运行 ECS 任务的实例所在的实际区域。容器运行一个 python 脚本,第一个任务是获取区域,以便我可以使用 boto3 方法sqs.get_queue_by_name()
,例如需要设置区域的方法。为此,我尝试让该地区与
meta = requests.get('http://169.254.169.254/latest/dynamic/instance-identity/document', timeout=1).json()
os.environ["AWS_DEFAULT_REGION"] = meta.get("region")
但我遇到了连接错误。
当我手动构建堆栈时,没有问题,但是当堆栈由 CDK 部署时(相同的安全组、角色等),我得到了错误
requests.exceptions.ConnectionError: HTTPConnectionPool(host='169.254.169.254', port=80): Max retries exceeded with url: /latest/dynamic/instance-identity/document (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fe3d62491f0>: Failed to establish a new connection: [Errno 22] Invalid argument'))
我可以看到解决此问题的两种不同途径:
使用 CDK 部署时设置环境变量“AWS_DEFAULT_REGION”,但使用
taskDefinition.addContainer('DSPTContainer', { image: ecrImage, memoryLimitMiB: 30000, environment: { AWS_DEFAULT_REGION: props.env?.region }) 有问题
Property 'AWS_DEFAULT_REGION' is incompatible with index signature.
修改任务角色(但如何)或其他内容(如安全组)以允许连接。请注意,在实例中,我能够建立连接....
[编辑]
在容器内部(实例运行时我可以登录到容器),我可以 ping 说 google.com,但不能 ping 实例元数据 URI:
import requests
requests.get("https://www.google.com", timeout=1) ---> Response200
requests.get("http://169.254.169.254/latest/meta-data/", timeout=1) ---> ConnectTimeout Exception
[解决方案]
解决方案
推荐阅读
- javascript - OpenLayers OpenMaps Circle 在地图上绘制时大小不正确
- mysql - Mysql:与 json_extract 一起使用时,where in 不返回任何行
- linux - Linux Ncurses Pad Smearing(文本滚动尝试)
- sql - 基于访问用户输入的 SQL 动态查询问题
- javascript - 在 iOS 13.1 版本中,当输入的光标不可见时?
- c - memset 函数的分段错误问题
- java - 通过 JPanel 进行 JComponent 裁剪
- angular - 从源“xxxx”访问“xxx/.well-known/openid-configuration”处的 XMLHttpRequest 已被 CORS 阻止
- python - 如何在 Keras 中正确地制作自定义损失函数
- iot - Contiki-NG 是否支持 Radio Duty Cycling (RDC)?