2016-11-16 113 views
1

我不完全確定我的aws api-gateway設置有什麼問題。我有兩個地區我使用,一個用於我的舞臺(歐洲西部1)環境,另一個用於我的現場(美國東部1)環境。AWS Api-網關不正確的區域

當我在staging中構建並運行我的api時,所有工作都正常。然而,當我嘗試在現場運行我的API,我得到以下錯誤:

{"message": "Internal server error"}

要嘗試和調試這我跑使用在AWS控制檯中的「測試」功能的API,並進行了GET請求。令我驚訝的是,我注意到請求標題中的區域是錯誤的(eu-west-1而不是us-east-1)。另外,測試返回下面的消息一502迴應:

{"Message":"Functions from 'us-east-1' are not reachable in this region ('eu-west-1')","Type":"User"}

這是否意味着我的API在eu-west-1區域中運行?

如果我在控制檯檢查我的區域我清楚us-east-1運行測試時。此外,我的api-gateway uri中的區域是us-east-1

HTTPS://.execute-api。 美國東1 .amazonaws.com/PROD

我可以看到我的請求頭具有:主機=拉姆達。 EU-西-1 .amazonaws.com 並在輸出I具有以下屬性:

端點請求URI:https://lambda歐盟 - 西1 .amazonaws.com/2015年3月31日/功能/ ARN:AWS:波長:美東1 ::功能:/調用

我不知道爲什麼我的地區混在一起!我認爲這可能與我的構建過程有關,但在仔細檢查所有內容後,我無法看到可能導致它的原因。我希望有一組新的眼睛可以挑選出來!

要建立我的API,我使用的是swagger.yaml文件和文件cloudformation.json的組合。

SWAGGER: 我在這個文件中的佔位符,我替換文件發送到AWS之前運行的節點腳本。

--- 
swagger: 2.0 
info: 
    title: ServerlessExpress 
basePath: /YOUR_API_GATEWAY_STAGE 
schemes: 
- https 
paths: 
    /: 
    x-amazon-apigateway-any-method: 
     produces: 
     - application/json 
     responses: 
     200: 
      description: 200 response 
      schema: 
      $ref: "#/definitions/Empty" 
     x-amazon-apigateway-integration: 
     responses: 
      default: 
      statusCode: 200 
     uri: arn:aws:apigateway:YOUR_AWS_REGION:lambda:path/2015-03-31/functions/arn:aws:lambda:YOUR_AWS_REGION:YOUR_ACCOUNT_ID:function:api-gateway-service/invocations 
     passthroughBehavior: when_no_match 
     httpMethod: POST 
     type: aws_proxy 
    options: 
     consumes: 
     - application/json 
     produces: 
     - application/json 
     responses: 
     200: 
      description: 200 response 
      schema: 
      $ref: "#/definitions/Empty" 
      headers: 
      Access-Control-Allow-Origin: 
       type: string 
      Access-Control-Allow-Methods: 
       type: string 
      Access-Control-Allow-Headers: 
       type: string 
     x-amazon-apigateway-integration: 
     responses: 
      default: 
      statusCode: 200 
      responseParameters: 
       method.response.header.Access-Control-Allow-Methods: "'GET,OPTIONS,POST'" 
       method.response.header.Access-Control-Allow-Headers: "'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'" 
       method.response.header.Access-Control-Allow-Origin: "'*'" 
     passthroughBehavior: when_no_match 
     requestTemplates: 
      application/json: "{\"statusCode\": 200}" 
     type: mock 
    /{proxy+}: 
    x-amazon-apigateway-any-method: 
     produces: 
     - application/json 
     parameters: 
     - name: proxy 
     in: path 
     required: true 
     type: string 
     responses: {} 
     x-amazon-apigateway-integration: 
     uri: arn:aws:apigateway:YOUR_AWS_REGION:lambda:path/2015-03-31/functions/arn:aws:lambda:YOUR_AWS_REGION:YOUR_ACCOUNT_ID:function:api-gateway-service/invocations 
     httpMethod: POST 
     type: aws_proxy 
    options: 
     consumes: 
     - application/json 
     produces: 
     - application/json 
     responses: 
     200: 
      description: 200 response 
      schema: 
      $ref: "#/definitions/Empty" 
      headers: 
      Access-Control-Allow-Origin: 
       type: string 
      Access-Control-Allow-Methods: 
       type: string 
      Access-Control-Allow-Headers: 
       type: string 
     x-amazon-apigateway-integration: 
     responses: 
      default: 
      statusCode: 200 
      responseParameters: 
       method.response.header.Access-Control-Allow-Methods: "'GET,OPTIONS,POST'" 
       method.response.header.Access-Control-Allow-Headers: "'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'" 
       method.response.header.Access-Control-Allow-Origin: "'*'" 
     passthroughBehavior: when_no_match 
     requestTemplates: 
      application/json: "{\"statusCode\": 200}" 
     type: mock 
definitions: 
    Empty: 
    type: object 
    title: Empty Schema 

CLOUDFORMATION:

{ 
    "AWSTemplateFormatVersion": "2010-09-09", 
    "Description": "AWS Serverless Express.", 
    "Parameters": { 
     "AwsServerlessExpressS3Bucket": { 
      "Type": "String", 
      "Description": "The S3 bucket in which the lambda function code is stored. Bucket names are region-unique, so you must change this." 
     }, 
     "LambdaFunctionS3Key": { 
      "Type": "String", 
      "AllowedPattern": ".*\\.zip", 
      "Description": "The S3 object for the lambda function code package.", 
      "Default": "lambda-function.zip" 
     }, 
     "ApiGatewaySwaggerS3Key": { 
      "Type": "String", 
      "AllowedPattern": ".*\\.yaml", 
      "Description": "The S3 object for the swagger definition of the API Gateway API.", 
      "Default": "simple-proxy-api.yaml" 
     } 
    }, 

    "Resources": { 
     "ApiGatewayApi": { 
      "Type": "AWS::ApiGateway::RestApi", 
      "Properties": { 
       "Description": "AWS Serverless Express API", 
       "BodyS3Location": { 
        "Bucket": { 
         "Ref": "ServerlessExpressBucket" 
        }, 
        "Key": { 
         "Ref": "ApiGatewaySwaggerS3Key" 
        } 
       } 
      } 
     }, 

     "ApiGatewayApiDeployment": { 
      "Type": "AWS::ApiGateway::Deployment", 
      "Properties": { 
       "RestApiId": { 
        "Ref": "ApiGatewayApi" 
       }, 
       "StageName": "YOUR_API_GATEWAY_STAGE" 
      } 
     }, 

     "LambdaApiGatewayExecutionPermission": { 
      "Type": "AWS::Lambda::Permission", 
      "Properties": { 
       "Action": "lambda:InvokeFunction", 
       "FunctionName": { 
        "Fn::GetAtt": ["LambdaFunction", "Arn"] 
       }, 
       "Principal": "apigateway.amazonaws.com", 
       "SourceArn": { 
        "Fn::Join": ["", ["arn:aws:execute-api:", { 
         "Ref": "AWS::Region" 
        }, ":", { 
         "Ref": "AWS::AccountId" 
        }, ":", { 
         "Ref": "ApiGatewayApi" 
        }, "/*/*"]] 
       } 
      } 
     }, 

     "LambdaFunction": { 
      "Type": "AWS::Lambda::Function", 
      "Properties": { 
       "Code": { 
        "S3Bucket": { 
         "Ref": "ServerlessExpressBucket" 
        }, 
        "S3Key": { 
         "Ref": "LambdaFunctionS3Key" 
        } 
       }, 
       "FunctionName": "api-gateway-service", 
       "Handler": "lambda.serverlessExpress", 
       "Description": "Service running on api-gateway", 
       "MemorySize": 128, 
       "Role": { 
        "Fn::Join": ["", ["arn:aws:iam::", { 
         "Ref": "AWS::AccountId" 
        }, ":role/service-lambda"]] 
       }, 
       "Runtime": "nodejs4.3", 
       "Timeout": 30 
      } 
     } 
    }, 

    "Outputs": { 
     "LambdaFunctionConsoleUrl": { 
      "Description": "Console URL for the Lambda Function.", 
      "Value": { 
       "Fn::Join": ["", ["https://", { 
        "Ref": "AWS::Region" 
       }, ".console.aws.amazon.com/lambda/home?region=", { 
        "Ref": "AWS::Region" 
       }, "#/functions/", { 
        "Ref": "LambdaFunction" 
       }]] 
      } 
     }, 
     "ApiGatewayApiConsoleUrl": { 
      "Description": "Console URL for the API Gateway API's Stage.", 
      "Value": { 
       "Fn::Join": ["", ["https://", { 
        "Ref": "AWS::Region" 
       }, ".console.aws.amazon.com/apigateway/home?region=", { 
        "Ref": "AWS::Region" 
       }, "#/apis/", { 
        "Ref": "ApiGatewayApi" 
       }, "/stages/YOUR_API_GATEWAY_STAGE"]] 
      } 
     }, 
     "ApiUrl": { 
      "Description": "Invoke URL for your API. Clicking this link will perform a GET request on the root resource of your API.", 
      "Value": { 
       "Fn::Join": ["", ["https://", { 
        "Ref": "ApiGatewayApi" 
       }, ".execute-api.", { 
        "Ref": "AWS::Region" 
       }, ".amazonaws.com/YOUR_API_GATEWAY_STAGE/"]] 
      } 
     } 
    } 
} 

回答

1

結果我發現我手動創建拉姆達功能API的網關的呼叫。儘管雲計算更新了這個代碼,但似乎在幕後出現了一個混淆區域。

因此,在短期,簡單地刪除在cloudformation堆棧和我手動創建lambda函數,重新運行的版本,讓cloudformation打造拉姆達功能解決了這個問題!

+1

好偵探。這真的讓我撓了撓頭,因爲你似乎在描述一種本該不可能的情況。 –