0

I have a code using http.client library in python that sends email to others.I tested this in jupyter notebook and am able to send an email.

But when I tried the same inside lambda AWS,I get an error.Below is my code:

import http.client
import mimetypes

def lambda_handler(event, context):
    
    conn = http.client.HTTPSConnection("mail.us-east-1.aws.cloud.xxx")
    payload = "{\n \"from\": \"[email protected]\",\n \"to\": \"[email protected]\",\n \"subject\": \"Test mail\",\n \"textbody\": \"Test body\",\n \"htmlbody\": \"<h3>Test body!</h3>\"\n}"
    headers = {
     'Content-Type': 'application/json'
    }
    conn.request("POST", "", payload, headers)
    res = conn.getresponse()
    data = res.read()
    print(data.decode("utf-8"))
    print("done")

Below is my error message:

Response:
{
  "errorMessage": "[Errno -2] Name or service not known",
  "errorType": "gaierror",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 15, in lambda_handler\n    conn.request(\"POST\", \"\", payload, headers)\n",
    "  File \"/var/lang/lib/python3.8/http/client.py\", line 1230, in request\n    self._send_request(method, url, body, headers, encode_chunked)\n",
    "  File \"/var/lang/lib/python3.8/http/client.py\", line 1276, in _send_request\n    self.endheaders(body, encode_chunked=encode_chunked)\n",
    "  File \"/var/lang/lib/python3.8/http/client.py\", line 1225, in endheaders\n    self._send_output(message_body, encode_chunked=encode_chunked)\n",
    "  File \"/var/lang/lib/python3.8/http/client.py\", line 1004, in _send_output\n    self.send(msg)\n",
    "  File \"/var/lang/lib/python3.8/http/client.py\", line 944, in send\n    self.connect()\n",
    "  File \"/var/lang/lib/python3.8/http/client.py\", line 1392, in connect\n

    super().connect()\n",
        "  File \"/var/lang/lib/python3.8/http/client.py\", line 915, in connect\n    self.sock = self._create_connection(\n
 "  File \"/var/lang/lib/python3.8/socket.py\", line 787, in create_connection\n    for res in getaddrinfo(host, port, 0, SOCK_STREAM):\n",
    "  File \"/var/lang/lib/python3.8/socket.py\", line 918, in getaddrinfo\n    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):\n"
  ]
}

Here the line number 15 points to :

data=res.read()

I am new to lambda and am not sure if I am missing out something.

1 Answer 1

1

This error refers to a failure in name resolution while looking up address of the endpoint you're trying to reach.

It might happen if you try to connect to AWS resource in other region. Take a look at previous question: AWS [Errno -2] Name or service not known

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.