๐ฐ๏ธ ์์ฑ์ผ : 2025.02.18
Q : VPC์ ๋งคํ๋์ง ์์ Lambda์ ๊ณต์ธ IP ์ฃผ์๋ ์ด๋ป๊ฒ ๋๋์? ๋ง์ฝ ๊ณต์ธ IP๊ฐ ๋ณ๊ฒฝ๋๋ค๋ฉด ๋ณ๊ฒฝ ์ฃผ๊ธฐ๋ ์ด๋ป๊ฒ ๋ ๊น์?
A-1. Lambda์ ๊ณต์ธ IP ์ฃผ์ ๋์ญ
https://ip-ranges.amazonaws.com/ip-ranges.json
์์ ์ ๊ณตํ๊ณ ์์ต๋๋ค. ๊ฐ ๋ฆฌ์ ๋ณ, ์๋น์ค๋ณ ์ฌ์ฉํ๋ ๊ณต์ธ IP ๋์ญ์ ํ์ธ ํ ์ ์์ต๋๋ค.The IP 3.34.124.213 belongs to the AWS service: AMAZON
A-2. Lambda์ IP ๋ณ๊ฒฝ์ ์๋ก์ด ์ปดํจํ ๋ฆฌ์์ค๊ฐ ์์ฑ๋๋ Cold Start ๋ฐ์ ์ IP๊ฐ ๋ฐ๋๋ ์ฌํญ์ ํ์ธ ํ ์ ์์ต๋๋ค.
์ํฉ 1 - 1๋ถ ๋ง๋ค Lambda๋ฅผ ํธ์ถ ํ์ ๊ฒฝ์ฐ(Warm Start)
์ํฉ 2 - 10๋ถ ๋ง๋ค Lambda๋ฅผ ํธ์ถ ํ์ ๊ฒฝ์ฐ(Cold Start)
์ฐธ๊ณ - Lambda์์ ์ฌ์ฉํ ์ฝ๋
import json
from urllib.request import urlopen
import requests
import json
import ipaddress
import os
def get_ip():
response = urlopen('<https://api.ipify.org>').read().decode('utf8')
print(f"Your IP Address is: {response}")
return response
def check_ip_in_range(ip, ip_range):
return ipaddress.ip_address(ip) in ipaddress.ip_network(ip_range)
def get_service_for_ip(ip, region):
# Download the AWS IP ranges file
response = requests.get('<https://ip-ranges.amazonaws.com/ip-ranges.json>')
ip_ranges = json.loads(response.text)
# Check each prefix in the specified region
for prefix in ip_ranges['prefixes']:
if prefix['region'] == region:
if check_ip_in_range(ip, prefix['ip_prefix']):
return prefix['service']
# If no match is found
return "No matching service found"
def lambda_handler(event, context):
if not os.path.exists('/tmp/warm.txt'):
print("This is a cold start")
with open('/tmp/warm.txt', 'w') as f:
f.write('warm')
else:
print("This is a warm start")
ip_to_check = get_ip();
region = "ap-northeast-2"
service = get_service_for_ip(ip_to_check, region)
print(f"The IP {ip_to_check} belongs to the AWS service: {service}")