ar90n / serverless-s3-local

Serverless s3 local plugin.
MIT License
215 stars 70 forks source link

UnknownEndpoint: Inaccessible host: `localhost'. This service may not be available in the `us-east-1' region. #116

Open Nicoxys opened 3 years ago

Nicoxys commented 3 years ago

Hi!

I state that I am only doing some simple tests to see if all this is for me. First, an example.

serverless.yml:

service: local-serverless

plugins:
  - serverless-s3-local
  - serverless-plugin-typescript
  - serverless-offline

provider:
  name: aws
  runtime: nodejs12.x
  stage: v1
  apiGateway:
    shouldStartNameWithService: true
  httpApi:
    cors: true
  environment:
    keysBucket: ${self:custom.keysBucket}
  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - "s3:GetObject"
      Resource:
        Fn::Join:
          - ""
          - - "arn:aws:s3:::"
            - Ref: ${self:custom.keysBucket}

functions:
  hello:
    handler: src/hello.handler
    events:
      - http:
          path: hello
          method: GET

custom:
  serverless-offline:
    noPrependStageInUrl: true
    noTimeout: true
    port: 3000
    host: localhost
  s3:
    address: localhost
    host: localhost
    port: 9501
    directory: src/helpers/jwt/keys
  keysBucket: JWTKeys

resources:
  Resources:
    JWTKeys:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: ${self:custom.keysBucket}

src/hello.ts:

import * as AWS from "aws-sdk";

const keysBucketName = process.env.keysBucket || '';

exports.handler = async function (event, context, callback) {
    const s3 = new AWS.S3({
        s3ForcePathStyle: true,
        accessKeyId: 'S3RVER',
        secretAccessKey: 'S3RVER',
        endpoint: new AWS.Endpoint('localhost:9501'),
    });

    const privateKey = await s3.getObject({
        Bucket: keysBucketName,
        Key: 'jwtRS256.key'
    }).promise();
    console.log(privateKey);

    return {
        statusCode: 200,
        body: JSON.stringify(event),
    };
};

So... With this setup, I receive this error:

UnknownEndpoint: Inaccessible host: `localhost'. This service may not be available in the `us-east-1' region.
at Request.ENOTFOUND_ERROR (/Users/nico/Desktop/serverless/lambda-app/node_modules/aws-sdk/lib/event_listeners.js:507:46)
at Request.callListeners (/Users/nico/Desktop/serverless/lambda-app/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
at Request.emit (/Users/nico/Desktop/serverless/lambda-app/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
at Request.emit (/Users/nico/Desktop/serverless/lambda-app/node_modules/aws-sdk/lib/request.js:688:14)
at ClientRequest.error (/Users/nico/Desktop/serverless/lambda-app/node_modules/aws-sdk/lib/event_listeners.js:339:22)
at ClientRequest.<anonymous> (/Users/nico/Desktop/serverless/lambda-app/node_modules/aws-sdk/lib/http/node.js:96:19)
at ClientRequest.emit (node:events:327:20)
at ClientRequest.EventEmitter.emit (node:domain:486:12)
at TLSSocket.socketErrorListener (node:_http_client:478:9)
at TLSSocket.emit (node:events:327:20)
at TLSSocket.EventEmitter.emit (node:domain:486:12)
at emitErrorNT (node:internal/streams/destroy:188:8)
at emitErrorCloseNT (node:internal/streams/destroy:153:3)
at processTicksAndRejections (node:internal/process/task_queues:80:21)

It seems like that it cannot connect to local s3.

Obv I use serverless offline start to start all the services. Am i missing something?

ar90n commented 3 years ago

@Nicoxys Thanks for your report!! As long as I read what you said, I guess it was caused by using HTTPS to access S3. Please try the following code at initializing the S3 object.

        endpoint: new AWS.Endpoint('http://localhost:9501'),
andreieuganox commented 3 years ago

Default Port: 4569

endpoint: new AWS.Endpoint('localhost:9501'), -> endpoint: new AWS.Endpoint('localhost:4569'),

reilg commented 2 years ago

I have the same issue and a similar simple setup except that I'm using new AWS.Endpoint('http://localhost:4569')

"UnknownEndpoint: Inaccessible host: `localhost'. This service may 
    not be available in the `us-east-1' region.
bertrandmc commented 1 year ago

I had to deal with this issue today, turns out I was passing the incorrect attribute when configuring the S3 service, forcePathStyle: true is for SDK 3, make sure you pass s3ForcePathStyle: true if you are using AWS SDK 2