Search code examples
amazon-web-servicesapiaws-lambdaaws-api-gatewayhttp-status-code-403

Permission Issue at an AWS API using Lambda


I'm testing my newly deployed AWS API using https://www.apitester.com/. As you can see i cant access the API. The API is deployed and the Lambda code looks as following.

const AWS = require('aws-sdk');
var bucket = new AWS.S3();

exports.handler = (event, context, callback) => {

     let data =JSON.parse(event.body);
     
     var params = {
       "Body": data,
       "Bucket": "smartmatressbucket",
      // "Key": filePath  
    };
    bucket.upload(params, function(err, data){
       if(err) {
           callback(err, null);
       } else {
           let response = {
        "statusCode": 200,
        "headers": {
            "my_header": "my_value"
        },
        "body": JSON.stringify(data),
        "isBase64Encoded": false
    };
           callback(null, response);
    }
    });
    
};


Solution

  • Looking at the response log, it seems the API Gateway generates "ForbiddenException". I believe the most possible reason is using an incorrect API URL (eg- https://ogk2hm09j0.execute-api.eu-central-1.amazonaws.com/).

    Suppose you configure the Lambda function to a GET method of a resource name "resourceA". Then you deploy the API to a stage named "dev". Then the correct URL should be https://ogk2hm09j0.execute-api.eu-central-1.amazonaws.com/dev/resourceA

    But looking at the API URL in the logs, it seems the stage name or the resource name is not specified.