Access AWS API Gateway with IAM roles from Python

后端 未结 3 2260
暗喜
暗喜 2021-02-19 18:00

I have an AWS API Gateway that I would like to secure using IAM Roles .

I am looking for a package to help me accessing it using Python. I am trying to avoid implementin

3条回答
  •  长情又很酷
    2021-02-19 18:16

    If you want to make a call using the IAM role, you should use BotoAWSRequestsAuth from aws-requests-auth:

    import requests
    from aws_requests_auth.boto_utils import BotoAWSRequestsAuth
    auth = BotoAWSRequestsAuth(
        aws_host="API_ID.execute-api.us-east-1.amazonaws.com",
        aws_region="us-east-1",
        aws_service="execute-api"
    )
    response = requests.get("https://API_ID.execute-api.us-east-1.amazonaws.com/STAGE/RESOURCE", auth=auth)
    

    This will use botocore to retrieve a key and secret from the AWS metadata service rather than you needing to pass them yourself.

    Thanks to Ka Hou Leong for the suggestion of the aws-requests-auth library.

提交回复
热议问题