1

I am using AWS Lambda, Python 3.7 and SendGrid API and want to send an email but getting the error:

"Unable to import module 'lambda_function': No module named 'sendgrid'"

Is there a way to resolve this? I can see that in some similar issues that the module would be imported from somewhere but can't work out from where.

My lambda code is just the sample code from the SendGrid website which values updated with the ones I want to use:

import json
import sendgrid
import os
from sendgrid.helpers.mail import *

def lambda_handler(event, context):

    sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
    from_email = Email("test@example.com")
    to_email = Email("****")
    subject = "Sending with SendGrid is Fun"
    content = Content("text/plain", "and easy to do anywhere, even with Python")
    mail = Mail(from_email, subject, to_email, content)
    response = sg.client.mail.send.post(request_body=mail.get())

    print(response.status_code)
    print(response.body)
    print(response.headers)

Thanks

user3165854
  • 1,505
  • 8
  • 48
  • 100

1 Answers1

0

The lambda environment does not have the sendgrid module available for your code to call. In order to use dependencies that aren't apart of the aws sdk or the language (like the sendgrid library) you have to pre-build the code with the packages locally and upload a zip file. An example can be found here: aws python lambda . There is another stack overflow addressing the same question here. The second contains some tools to make uploading easier.

Datise
  • 3,683
  • 2
  • 11
  • 12