我正在使用 AWS Lambda。到目前为止,我已经编辑了 /etc/hosts,但无法在 Lambda 中执行此操作。
我有一个将 Ping 发送到 Google 的脚本。它看起来像:
import subprocess
def aws_handler(a, b):
ipaddress = 'google.com' # guess who
proc = subprocess.Popen(['/usr/bin/ping', '-c', '3', ipaddress],
stdout=subprocess.PIPE)
stdout, stderr = proc.communicate()
print(stdout, stderr)
我的 Dockerfile 如下所示:
FROM public.ecr.aws/lambda/python:3.8 as lambda-base
COPY ./ ./
RUN yum install -y iputils dnsmasq
RUN echo "nameserver 127.0.0.1" > /etc/resolv.conf
RUN cat /etc/resolv.conf
RUN echo "address=/com/127.0.0.1" >> /etc/dnsmasq.conf
RUN cat /etc/dnsmasq.conf
FROM lambda-base
CMD [ "app.aws_handler" ]
当我运行时,我得到以下输出
(142.250.187.238): icmp_seq=1 ttl=127 time=68.3 ms\n64 bytes from lhr25s34-in-f14.1e100.net
(142.250.187.238): icmp_seq=2 ttl=127 time=67.6 ms\n64 bytes from lhr25s34-in-f14.1e100.net
(142.250.187.238): icmp_seq=3 ttl=127 time=...
当我预计 Ping 为 127.0.0.1 时。为什么它没有按预期工作?
答案1
我有两行/etc/dnsmasq.conf
no-resolv
address=/com/127.0.0.1
以及相应的条目/etc/resolv.conf
nameserver 127.0.0.1
当然,重要的是要确保dnsmasq
已重新启动以注意到新配置
systemctl restart dnsmasq # For systemd
# service dnsmasq restart # Otherwise
测试
ping -c1 bbc.com
PING bbc.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.061 ms