When you want to return the API result with curl etc., it is necessary to calculate the header (SigV4 signature) of the HTTP request, but note that the material is not found See other sites for how to make API Gateway IAM authenticated. There is a way to calculate the hash value, but I'm not sure, so when I need it, I look inside the botocore library and create it.
check_iam.py
import os
from botocore.awsrequest import AWSRequest
from botocore.auth import SigV4Auth
from botocore.endpoint import URLLib3Session
from botocore.credentials import Credentials
import requests
url_ = "https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/v1"
### 1.Credential generation
credentials = Credentials(os.environ['AWS_ACCESS_KEY_ID'], os.environ['AWS_SECRET_ACCESS_KEY'])
### 2.AWS Request generation
request = AWSRequest(method="GET", url=url_)
### 3.AWS request signature
SigV4Auth(credentials, 'execute-api', ap-northeast-1).add_auth(request)
### 4.API issuance
headers = {
'Authorization': request.headers['Authorization'],
'Host':'xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com',
'X-Amz-Date':request.context['timestamp']
}
response = requests.get(url_,headers=headers)
print(response.text)
Recommended Posts