Unveiling the Mystery: Decoding the Dilemma of Lambda Deployment in Private Subnets

Vision2cloud
2 min readApr 17, 2024

In this demonstration, we see why we are getting a timeout error when deploying a lambda functions to a public subnet.

I will deploy a VPC with an Internet gateway to demonstrate this error I am not going to deploy any resources in a private subnet, so for the moment I will not create a NAT gateway. After deploying, my VPC belongs as below.

I will deploy a sample Lambda function with role created by default in the vpc-1 in public subnet and I added necessary lambda layers.

Below is the code:

import requests

def lambda_handler(event, context):
url = 'https://swapi.dev/api/people/4/'
response = requests.get(url)

if response.status_code == 200:
return response.json()
else:
return {
'statusCode': response.status_code,
'body': 'Failed to fetch data from SWAPI'
}
General configuration
VPC configuration:

--

--

Vision2cloud

🌥️ Cloud & DevOps Engineer 🚀 | Passionate about optimizing and automating IT infrastructure. Sharing insights on cloud architecture and automation