no module named 'requests_aws4auth' when trying to import in a lambda

  • Last Update :
  • Techknowledgy :

e.g. On Linux:

mkdir project - dir
cp myhandler.py project - dir
pip install module - name - t / path / to / project - dir

# zip the contents of project - dir, this is your deployment package
cd project - dir
zip - r deployme.zip.

You can replace the following

access_key = os.environ.get('AWS_ACCESS_KEY_ID')
secret_key = os.environ.get('AWS_SECRET_ACCESS_KEY')

with the values you got from the assume role request like this

access_key = responseAssumeRole['Credentials']['AccessKeyId']
secret_key = responseAssumeRole['Credentials']['SecretAccessKey']

Suggestion : 2

Last updated: 2022-04-12

{
   "Version": "2012-10-17",
   "Statement": [{
      "Sid": "VisualEditor0",
      "Effect": "Allow",
      "Action": "lambda:PublishLayerVersion",
      "Resource": "*"
   }]
}
$ sudo amazon - linux - extras install python3 .8
$ curl - O https: //bootstrap.pypa.io/get-pip.py
   $ python3 .8 get - pip.py--user
$ mkdir python
$ python3 .8 - m pip install pandas - t python /
$ zip - r layer.zip python
$ aws lambda publish - layer - version--layer - name pandas - layer--zip - file fileb: //layer.zip --compatible-runtimes python3.8 --region us-east-1

Suggestion : 3

I need my lambda to call an API Gateway and have the following code in place as inline code for the lambda in my cloud formation template., 2 days ago Apr 09, 2018  · Zip the new_lambda folder by right-clicking it and selecting 'compress'. My results: Unable to import module 'lambda_function': No module named 'lambda_function'. To reiterate, my file is named lambda_function.py and contains a function called lambda_handler, which accepts two arguments (as seen above). This information matches that seen in ... , The problem is that your local numpy and pandas are compiled for the local machine's architecture. Since AWS Lambda uses custom Linux, they are probably not compatible. Compile dependencies on EC2 instance which uses the same Amazon Linux version as AWS Lambda and create a deployment package. , requests is not a standard library in AWS lambda. 1- Import it from the Botocore libraries stack as: (Option to be deprecated after the answer was given) 2- Create a deployment package with virtualenv. This is because it is missing the requests library when running in the lambda - its likely that its installed globally on your local machine.


from requests_aws4auth
import AWS4Auth def handler(event, context): client = boto3.client('sts') responseAssumeRole = client.assume_role(DurationSeconds = 3600, RoleArn = 'arn', // real arn of the api gateway invocation role             RoleSessionName='Bob',           )           credentials = responseAssumeRole['Credentials']           auth = AWS4Auth(aws_access_key=responseAssumeRole['Credentials']['AccessKeyId'],                                  aws_secret_access_key=responseAssumeRole['Credentials']['SecretAccessKey'],                                  aws_host='host.execute-api.us-east-1.amazonaws.com',                                  aws_region='us-east-1',                                  aws_service='execute-api')           headers= {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'}           response = requests.get('https://host.execute-api.us-east-1.amazonaws.com/test',                                   auth=auth, headers=headers) 

mkdir project - dir cp myhandler.py project - dir pip install module - name - t / path / to / project - dir # zip the contents of project - dir, this is your deployment package cd project - dir zip - r deployme.zip.

Suggestion : 4

I am trying to create one lambda function and also try to read one url inside that lambda function. For that I am using request function, but I am getting this below error.,According to my knowledge requests is not a standard library in AWS lambda. So to use this library you can Import it from the Botocore libraries.,66818/no-module-named-requests-on-the-aws-python-lambda-function, No module named requests on the AWS Python...

I am trying to create one lambda function and also try to read one url inside that lambda function. For that I am using request function, but I am getting this below error.

Unable to
import module 'lambda_function': No module named requests

According to my knowledge requests is not a standard library in AWS lambda. So to use this library you can Import it from the Botocore libraries.

from botocore.vendored
import requests

Suggestion : 5

Connection parameters are included in the tests for the AWS Support API, should you have access and want to try it. The documentation says it supports auth v4 so it should work if you have a subscription. Do pass on your results!,The package passes all tests in the AWS auth v4 test_suite, and contains tests against the supported live services. See docstrings in test/requests_aws4auth_test.py for details about running the tests.,This means that AWS4Auth now extracts and parses dates from the values of X-Amz-Date and Date headers. Supported date formats are:,Requests authentication for all AWS services that support AWS auth v4

Install via pip:

$ pip install requests - aws4auth

Basic usage

>>> import requests
>>> from requests_aws4auth import AWS4Auth
>>> endpoint = 'http://s3-eu-west-1.amazonaws.com'
>>> auth = AWS4Auth('<ACCESS ID>', '<ACCESS KEY>', 'eu-west-1', 's3')
      >>> response = requests.get(endpoint, auth=auth)
      >>> response.text
      <?xml version="1.0" encoding="UTF-8"?>
      <ListAllMyBucketsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01">
         <Owner>
            <ID>bcaf1ffd86f461ca5fb16fd081034f</ID>
            <DisplayName>webfile</DisplayName>
            ...

STS Temporary Credentials

>>> from requests_aws4auth import AWS4Auth
>>> auth = AWS4Auth('<ACCESS ID>', '<ACCESS KEY>', 'eu-west-1', 's3',
      session_token='<SESSION TOKEN>')
         ...

To allow automatic key regeneration, the secret key is stored in the AWS4Auth instance, in the signing key object. If you do not want this to occur, instantiate the instance using an AWS4Signing key which was created with the store_secret_key parameter set to False:

>>> sig_key = AWS4SigningKey(secret_key, region, service, date, False) >>>
   auth = StrictAWS4Auth(access_id, sig_key)

prep:

python3 - m pip install--user--upgrade setuptools wheel testresources twine