すべてのドメインをlocalhostにリダイレクトする

すべてのドメインをlocalhostにリダイレクトする

私はAWS Lambdaを使用しています。これまで/etc/hostsを編集しましたが、Lambdaでは編集できません。

Googleにpingを送信するスクリプトがあります。それは次のとおりです。

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

2行あります。/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

おすすめ記事