AWS AppSync gives Unauthorized Access

0
session = requests.Session()
session.auth = AWS4Auth(
    # An AWS 'ACCESS KEY' associated with an IAM user.
    AWS_ACCESS_KEY_ID,
    # The 'secret' that goes with the above access key.                    
    AWS_SECRET,    
    # The region you want to access.
    AWS_REGION_NAME,
    # The service you want to access.
    'appsync'
)
# As found in AWS Appsync under Settings for your endpoint.
APPSYNC_API_ENDPOINT_URL = APPSYNC_ENDPOINT
# Use JSON format string for the query. It does not need reformatting.
query = """query MyQuery {
          getCustomer(id: "38c76f4f-c495-42e6-8599-647738a12692") {
            first_name
          }
        }"""
# Now we can simply post the request...
response = session.request(
    url=APPSYNC_API_ENDPOINT_URL,
    method='POST',
    json={'query': query}
)
print(response.text)

I want to be able to Read/Write data from AWS AppSync's dynamodb through python. I came across this solution online. But when I run it gives me the following error:

{
  "errors" : [ {
    "errorType" : "UnauthorizedException",
    "message" : "You are not authorized to make this call."
  } ]
}

I have given Administrator Access to the IAM User. What am I doing wrong here?

1 Answer
0

This error implies that you have a different authorization method enabled on AppSync than "AWS Identity and Access Management (IAM)". The default authorization method is "API key".

Changing the authorization method:

  1. Navigate to the AppSync resource in the AWS Console
  2. Click on the Settings option in the left-hand menu
  3. Select default authorization mode as shown below Enter image description here

Using API Key Sample

To change your python code to use API key, please refer to the below sample

import requests

session = requests.Session()

session.headers = {'x-api-key': '<INSERTAPIKEYHERE'} 
# As found in AWS Appsync under Settings for your endpoint.
APPSYNC_API_ENDPOINT_URL = APPSYNC_ENDPOINT
# Use JSON format string for the query. It does not need reformatting.
query = """query MyQuery {
          getCustomer(id: "38c76f4f-c495-42e6-8599-647738a12692") {
            first_name
          }
        }"""
# Now we can simply post the request...
response = session.request(
    url=APPSYNC_API_ENDPOINT_URL,
    method='POST',
    json={'query': query}
    
)
print(response.text)
profile pictureAWS
answered 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions