I am using boto3 to read many text files in S3 through a Lambda Python function.
My codes for the connection to S3 below
JavaScript
x
11
11
1
config = Config(
2
read_timeout=900,
3
connect_timeout=900,
4
retries={"max_attempts": 0}
5
)
6
session = boto3.Session(region_name='ap-southeast-2')
7
8
s3 = session.resource('s3', config=config)
9
bucket = s3.Bucket(bucket)
10
for obj in bucket.objects.all():
11
About 30 text files are successfully ok. But after, it gets below error message
JavaScript
1
30
30
1
[ERROR] 2021-09-01T09:35:08.628Z 6a5bf649-2057-43e4-a3eb-1b4438e582fb Exception on /process [POST]
2
Traceback (most recent call last):
3
File "/opt/python/urllib3/connection.py", line 169, in _new_conn
4
conn = connection.create_connection(
5
File "/opt/python/urllib3/util/connection.py", line 96, in create_connection
6
raise err
7
File "/opt/python/urllib3/util/connection.py", line 86, in create_connection
8
sock.connect(sa)
9
socket.timeout: timed out
10
11
During handling of the above exception, another exception occurred:
12
13
Traceback (most recent call last):
14
File "/var/runtime/botocore/httpsession.py", line 323, in send
15
urllib_response = conn.urlopen(
16
File "/var/task/serverless_sdk/vendor/wrapt/wrappers.py", line 602, in __call__
17
return self._self_wrapper(self.__wrapped__, self._self_instance,
18
File "/opt/python/urllib3/connection.py", line 353, in connect
19
conn = self._new_conn()
20
File "/opt/python/urllib3/connection.py", line 174, in _new_conn
21
raise ConnectTimeoutError(
22
urllib3.exceptions.ConnectTimeoutError: (<botocore.awsrequest.AWSHTTPSConnection object at 0x7fe7b7a3e130>, 'Connection to xxxxxxxxxxxxxxxxxxxxxx.amazonaws.com timed out. (connect timeout=5)')
23
24
During handling of the above exception, another exception occurred:
25
26
Traceback (most recent call last):
27
File "/tmp/sls-py-req/flask/app.py", line 2070, in wsgi_app
28
response = self.full_dispatch_request()
29
File "/var/runtime/botocore/httpsession.py", line 356, in send raise ConnectTimeoutError(endpoint_url=request.url, error=e) botocore.exceptions.ConnectTimeoutError: Connect timeout on endpoint URL: "https://xxxxxxxxxxxxxxxxx.s3.ap-southeast-2.amazonaws.com/?encoding-type=url"
30
Any way I can resolve this?
Advertisement
Answer
Lambda in a VPC does not have public IP and therefor can’t access internet from public subnets. Thus you can create NAT gateway in a public subnet, and place your lambda in private subnet. Once you setup route tables for any 0.0.0.0/0
connections in the private subnet to go to the NAT, your lambda will get internet access:
Instead, you can avoid using NAT and the internet with VPC interface endpoint for AWS Config.