2017-06-14 111 views

回答

0

貌似有相關GitHub上與localstack建立API網關的開放問題:

https://github.com/atlassian/localstack/issues/129

你可以嘗試以下的答案了,提供的步驟。

從GitHub的問題複製:

「」」

一種選擇是使用無服務器架構(https://github.com/serverless/serverless),否則,您可以直接調用LocalStack服務(通過CLI或SDK。 )創建API網關資源+方法+整合,並將它們連接到您的lambda表達式

這裏有幾個指標,這可能會有所幫助: https://ig.nore.me/2016/03/setting-up-lambda-and-a-gateway-through-the-cli/(「創建角色」部分可以跳過) https://github.com/atlassian/localstack/issues/101 https://github.com/temyers/serverless-localstack 「」」

0

演練與API網關每CLI創建的NodeJS LAMBDA在一起:

首先,我們創建一個簡單的NodeJS LAMBDA:

const apiTestHandler = (payload, context, callback) => { 
console.log(`Function apiTestHandler called with payload ${JSON.stringify(payload)}`); 
callback(null, { 
    statusCode: 201, 
    body: JSON.stringify({ 
     somethingId: payload.pathParameters.somethingId 
    }), 
    headers: { 
     "X-Click-Header": "abc" 
    } 
}); 
} 
module.exports = { 
    apiTestHandler, 
} 

它放入一個zip文件名爲apiTestHandler。拉鍊並上傳到localstack:

aws lambda create-function \ 
--region us-east-1 \ 
--function-name api-test-handler \ 
--runtime nodejs6.10 \ 
--handler index.apiTestHandler \ 
--memory-size 128 \ 
--zip-file fileb://apiTestHandler.zip \ 
--role arn:aws:iam::123456:role/role-name --endpoint-url=http://localhost:4574 

現在,我們可以創建REST的API:

aws apigateway create-rest-api --region us-east-1 --name 'API Test' --endpoint-url=http://localhost:4567 

這給出瞭如下回應:

{ 
"name": "API Test", 
"id": "487109A-Z548", 
"createdDate": 1518081479 
} 

隨着我們在這裏得到了ID,我們可以詢問其父母ID:

aws apigateway get-resources --region us-east-1 --rest-api-id 487109A-Z548 --endpoint-url=http://localhost:4567 

響應:

{ 
"items": [ 
    { 
     "path": "/", 
     "id": "0270A-Z23550", 
     "resourceMethods": { 
      "GET": {} 
     } 
    } 
] 
} 

現在我們所擁有的一切與它的路徑共同創造我們的資源:

aws apigateway create-resource \ 
--region us-east-1 \ 
--rest-api-id 487109A-Z548 \ 
--parent-id 0270A-Z23550 \ 
--path-part "{somethingId}" --endpoint-url=http://localhost:4567 

響應:

{ 
"resourceMethods": { 
    "GET": {} 
}, 
"pathPart": "{somethingId}", 
"parentId": "0270A-Z23550", 
"path": "/{somethingId}", 
"id": "0662807180" 
} 

創造需要我們在這裏得到的ID我們鏈接GET方法:

aws apigateway put-method \ 
--region us-east-1 \ 
--rest-api-id 487109A-Z548 \ 
--resource-id 0662807180 \ 
--http-method GET \ 
--request-parameters "method.request.path.somethingId=true" \ 
--authorization-type "NONE" \ 
--endpoint-url=http://localhost:4567 

我們幾乎沒有 - 最後要做的事情之一是創建與已經上傳拉姆達我們的整合:

aws apigateway put-integration \ 
--region us-east-1 \ 
--rest-api-id 487109A-Z548 \ 
--resource-id 0662807180 \ 
--http-method GET \ 
--type AWS_PROXY \ 
--integration-http-method POST \ 
--uri arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:000000000000:function:api-test-handler/invocations \ 
--passthrough-behavior WHEN_NO_MATCH \ 
--endpoint-url=http://localhost:4567 

最後但並非最不重要的:部署我們的API給我們所需的階段:

aws apigateway create-deployment \ 
--region us-east-1 \ 
--rest-api-id 487109A-Z548 \ 
--stage-name test \ 
--endpoint-url=http://localhost:4567 

現在我們可以測試一下:

curl http://localhost:4567/restapis/487109A-Z548/test/_user_request_/HowMuchIsTheFish 

響應:

% Total % Received % Xferd Average Speed Time Time  Time Current 
          Dload Upload Total Spent Left Speed 
100 34 100 34 0  0  9  0 0:00:03 0:00:03 --:--:--  9 
{"somethingId":"HowMuchIsTheFish"} 

我希望這有助於。

提示1:爲了方便使用,我建議安裝AWSCLI本地(https://github.com/localstack/awscli-local) - 使用這個工具,你可以使用命令「awslocal」並不必鍵入「--endpoint-URL = ... 「每個命令

演練使用無服務器架構和Localstack:

您也可以使用無服務器架構(https://serverless.com/)。

首先通過NPM安裝:

npm install serverless -g 

現在,您可以創建基於的NodeJS-AWS模板示例應用程序:

serverless create --template aws-nodejs 

爲了有一個HTTP端點,你要編輯serverless.yml並添加相應的事件:

functions: 
    hello: 
    handler: handler.hello 
    events: 
     - http: 
      path: ping 
      method: get 

爲了運行此對你的localstack安裝,你必須使用無服務器-localstack插件(https://github.com/temyers/serverless-localstack):

npm install serverless-localstack 

現在你必須編輯您serverless.yml再次,添加插件和調整你的端點。在我的情況localstack是碼頭工人的工具箱內運行,所以它的IP是192.168.99.100 - 你可能要改變這localhost,這取決於你的使用:

plugins: 
- serverless-localstack 
custom: 
    localstack: 
    debug: true 
    stages: 
     - local 
     - dev 
    host: http://192.168.99.100 
    endpoints: 
     S3: http://192.168.99.100:4572 
     DynamoDB: http://192.168.99.100:4570 
     CloudFormation: http://192.168.99.100:4581 
     Elasticsearch: http://192.168.99.100:4571 
     ES: http://192.168.99.100:4578 
     SNS: http://192.168.99.100:4575 
     SQS: http://192.168.99.100:4576 
     Lambda: http://192.168.99.100:4574 
     Kinesis: http://192.168.99.100:4568 

現在你可以試着將它部署:

serverless deploy --verbose --stage local 

這將創建一個S3存儲桶,上傳您的lambda並創建一個cloudformation堆棧。但是,由於本地堆棧與AWS相比存在一些不一致性,因此該過程將失敗。不要驚惶雖然,創建cloudformation模板正常工作,你只需要一個額外的要求,就大功告成了:

awslocal cloudformation update-stack --template-body file://.serverless/cloudformation-template-update-stack.json --stack-name aws-nodejs-local 

現在你的拉姆達部署和可測試:

curl http://192.168.99.100:4567/restapis/75A-Z278430A-Z/local/_user_request_/ping 

響應:

% Total % Received % Xferd Average Speed Time Time  Time Current Dload Upload Total Spent Left Speed 
100 364 100 364 0  0 111  0 0:00:03 0:00:03 --:--:-- 111 
{"message":"Go Serverless v1.0! Your function executed successfully!","input":{"body":null,"headers":{"host":"192.168.99.100:4567","accept":"*/*","user-agent":"curl/7.49.1"},"resource":"/restapis/75A-Z278430A-Z/local/_user_request_/ping","queryStringParameters":{},"httpMethod":"GET","stageVariables":{},"path":"/ping","pathParameters":{},"isBase64Encoded":false}} 

希望這會有所幫助。