Package: requests-auth-aws-sigv4
requests-auth-aws-sigv4Use AWS signature version 4 Authentication with the python requests moduleThis package provides an authentication class that can be used with the popularrequestspackage to add theAWS Signature Version 4authentication information.The signing code is inspired by the python example provided by AWS.This package should support any/all AWS API's, including API Gateway API's (execute-api),
Elasticsearch clusters, and others.  AWS Credentials may be pulled from the environment
in an easy and familiar way.
The signature is added as a header to the request.Installationpip install requests-auth-aws-sigv4Usageimportrequestsfromrequests_auth_aws_sigv4importAWSSigV4r=requests.request('POST','https://sts.us-east-1.amazonaws.com',data=dict(Version='2011-06-15',Action='GetCallerIdentity'),auth=AWSSigV4('sts'))print(r.text)Ifboto3is available, it will attempt to use credentials that have been configured for the AWS CLI or SDK's,
as documented inBoto3 User Guide: Credentials.
Otherwise, ifboto3is not available, credentials must be provided using either environment variables or parameters.Example using environment variablesEnvironment variable names are the same as documented for AWS CLI and SDK's.exportAWS_ACCESS_KEY_ID=MYACCESSKEYexportAWS_SECRET_ACCESS_KEY=THISISSECRETexportAWS_SESSION_TOKEN=THISISWHERETHESUPERLONGTOKENGOESimportrequestsfromrequests_auth_aws_sigv4importAWSSigV4aws_auth=AWSSigV4('ec2')# If not provided, check for AWS Credentials from Environment Variablesr=requests.request('GET','https://ec2.us-east-1.amazonaws.com?Version=2016-11-15&Action=DescribeRegions',auth=aws_auth)print(r.text)Example using parametersPassing credentials as parameters overrides all other possible sources.importrequestsfromrequests_auth_aws_sigv4importAWSSigV4aws_auth=AWSSigV4('ec2',aws_access_key_id=ACCESS_KEY,aws_secret_access_key=SECRET_KEY,aws_session_token=SESSION_TOKEN,)r=requests.request('GET','https://ec2.us-east-1.amazonaws.com?Version=2016-11-15&Action=DescribeRegions',auth=aws_auth)print(r.text)Usage with Elasticsearch Client (elasticsearch-py)fromelasticsearchimportElasticsearch,RequestsHttpConnectionfromrequests_auth_aws_sigv4importAWSSigV4es_host='search-service-foobar.us-east-1.es.amazonaws.com'aws_auth=AWSSigV4('es')# use the requests connection_class and pass in our custom auth classes_client=Elasticsearch(host=es_host,port=80,connection_class=RequestsHttpConnection,http_auth=aws_auth)es_client.info()Debug LoggingAll log messages are at the module level.importlogginglogging.basicConfig()# Setup basic logging to stdoutlog=logging.getLogger('requests_auth_aws_sigv4')log.setLevel(logging.DEBUG)Command Line UsageThe module can be run from the command line in a way that is similar to how cURL works.$python3-mrequests_auth_aws_sigv4https://sampleapi.execute-api.us-east-1.amazonaws.com/test/-v
>GET/test/HTTP/1.1
>Host:sampleapi.execute-api.us-east-1.amazonaws.com
>User-Agent:python-requests/2.23.0auth-aws-sigv4/0.2
>Accept-Encoding:gzip,deflate
>Accept:*/*
>Connection:keep-alive
>X-AMZ-Date:20200513T180549Z
>Authorization:AWS4-HMAC-SHA256Credential=AKIASAMPLEKEYID/20200513/us-east-1/execute-api/aws4_request,SignedHeaders=host;x-amz-date,Signature=EXAMPLESIGNATUREISHERE
>
<HTTP/1.1200OK
<Connection:keep-alive
<Content-Length:25<Content-Type:application/json
<Date:Wed,13May202018:05:49GMT
<Server:Server
<x-amz-apigw-id:MeExampleiMFs99=<x-amzn-RequestId:7example-7b7b-4343-9a9a-9bbexampleaf
helloTemporary Security CredentialsCredentials issued fromAWS STSto grant temporary access can be used normally.  Set the token by passing theaws_session_tokenparameter,
setting theAWS_SESSION_TOKENenvironment variable, or configure the credential for boto3 as normal.Using boto3 (or botocore) for AWS CredentialsThe packagesboto3andbotocoreare not requirements to use this module.As mentioned above, ifboto3is available, a boto3.Session will be created to attempt to get credentials
and configure the default region.  This will happen automatically if credentials are not provided as parameters.