1

I get 401 Unauthorized when I do a 301 redirect from an AWS Lambda to the top page of my site hosted through AWS Amplify.

I also get the error Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at ... (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 401.

And finally, I get the error, Uncaught (in promise) TypeError: NetworkError when attempting to fetch resource.

CORS is set for the API, as are OPTIONS. I set Access-Control-Allow-Origin to * in the Lambda. I also set Access-Control-Allow-Credentials to True in the Lambda.

I have done this 301 redirect for 2 other lambdas that support this site without 401 issues; I just don't seem to know enough to make this third one work. I am missing something, but I cannot see it for the life of me. Thanks for the help.

Here is the Lambda:

import boto3
import simplejson as jsona

def lambda_handler(event, context):

    
    title = event['queryStringParameters']['title']
    
    session = boto3.Session()
    credentials = session.get_credentials()

    dynamodb = boto3.resource('dynamodb',
        aws_session_token=credentials.token,
        aws_access_key_id=credentials.access_key,
        aws_secret_access_key=credentials.secret_key,
        region_name='us-west-2'
    )
    
    table = dynamodb.Table('recipes')
    
    response = table.delete_item(
        Key={'recipe_index': int(event['queryStringParameters']['index'])}
    )

    location = 'https://' + event['queryStringParameters']['host'] + '/'
    
    return {
        'statusCode': 301,
        'headers': {
          'Access-Control-Allow-Origin': '*', 
          'Access-Control-Allow-Credentials': True,
          'Location': location
        },
    }

0 Answers0