2016-11-16 111 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

好侦探。这真的让我挠了挠头,因为你似乎在描述一种本该不可能的情况。 –