Skip to content

Error retrieving credentials from the instance profile metadata server. (Client error: GET http://169.254.169.254/latest/meta-data/iam/security-credentials/ resulted in a 404 Not Found #23

Description

@Tarequzzaman

Hi
I got this error while connecting AWS Managed Elasticsearch. But My code does not provide any error on my local environment. I have shared my both environment specification below:

Local Environment:

Mac OS Catelina

AWS CLI version:

aws-cli/2.0.31 Python/3.7.4 Darwin/19.3.0 botocore/2.0.0dev35

aws-php-sdk:

"aws/aws-sdk-php": "3.90.6",

"jsq/amazon-es-php": "^0.3.0",

Dev Environment:

AWS EC2
Ubuntu 18.04.2 LTS (GNU/Linux 5.3.0-1019-aws x86_64)

AWS CLI version:

aws-cli/1.18.69 Python/3.6.9 Linux/5.3.0-1019-aws botocore/1.16.19

aws-php-sdk:

"aws/aws-sdk-php": "3.90.6",
"jsq/amazon-es-php": "^0.3.0",

I have check the aws credentials both of local and dev server using cat ~/.aws/credentials and they are the same.

[default]
aws_access_key_id =  xxxxxx
aws_secret_access_key = xxxxxxxxxxxx

I have added both of my local and dev server public ip on elasticsearch access policy.

Below I have added my PHP code:

Service class for getting client:

  class ElasticSearchService
{
    public function getElasticClient(){
        $provider = CredentialProvider::defaultProvider();
        $handler = new ElasticsearchPhpHandler('ap-southeast-1', $provider);
        $client = ClientBuilder::create()
            ->setHandler($handler)
            ->setHosts(['https://xxxx-xxxxx-xxxxxxxxxxxxxxx.ap-southeast-1.es.amazonaws.com:443'])
            ->build();
        return $client;
    }
}

Controller:

class SearchController extends Controller
{
 
   public function __construct(ElasticSearchService $elasticSearchService){
       $this->elasticSearchService = $elasticSearchService;
   }

public function  search(Request $request){
   try {
       $client = $this->elasticSearchService->getElasticClient();
       $content = $request->get("body");
         $parms = [
           "index" => "questions",
           "body" => [
               "size" => 5,
               "query" => [
                   "multi_match" => [
                       "fuzziness" => "AUTO",
                       "fields" => ["body"],
                       "query" => $content,
                       "minimum_should_match" => "50%"
                   ]
               ]
           ]
       ];
       $result = $client->search($parms);
       $data = array();
       foreach($result['hits']['hits'] as $d){
           array_push($data, $d["_source"]);
       }
       return MakeResponse::successResponse($data);
   }catch ( \Exception $exception){
       return MakeResponse::errorResponse("Something Went Wrong", $exception->getMessage());
   }
}

I got success data on my local environment. But on dev environment I got error response where the error message is :

Error retrieving credentials from the instance profile metadata server. (Client error: `GET http://169.254.169.254/latest/meta-data/iam/security-credentials/` resulted in a `404 Not Found` response:\n<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n\t\"http://www. (truncated...)\n)

Help me find my problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions