Creating a NodeJS Lambda together with API Gateway per CLI:
First we create a simple NodeJS Lambda:
const apiTestHandler = (event, context, callback) => {
console.log(`Function apiTestHandler called with payload ${JSON.stringify(event)}`);
callback(null, {
    statusCode: 201,
    body: JSON.stringify({
        somethingId: payload.pathParameters.somethingId
    }),
    headers: {
        "X-Click-Header": "abc"
    }
}); 
}
module.exports = {
    apiTestHandler,
}
Put that into a zip File called apiTestHandler.zip and upload it to 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
Now we can create our Rest-Api:
aws apigateway create-rest-api --region us-east-1 --name 'API Test' --endpoint-url=http://localhost:4567
This gives the following response:
{
"name": "API Test",
"id": "487109A-Z548",
"createdDate": 1518081479
}
With the ID we got here, we can ask for its parent-ID:
aws apigateway get-resources --region us-east-1 --rest-api-id 487109A-Z548 --endpoint-url=http://localhost:4567
Response:
{
"items": [
    {
        "path": "/",
        "id": "0270A-Z23550",
        "resourceMethods": {
            "GET": {}
        }
    }
]
}
Now we have everything to create our resource together with its path:
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
Response:
{
"resourceMethods": {
    "GET": {}
},
"pathPart": "{somethingId}",
"parentId": "0270A-Z23550",
"path": "/{somethingId}",
"id": "0662807180"
}
The ID we got here is needed to create our linked GET Method:
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
We are almost there - one of the last things to do is to create our integration with the already uploaded lambda:
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
Last but not least: Deploy our API to our desired stage:
aws apigateway create-deployment \
 --region us-east-1 \
 --rest-api-id 487109A-Z548 \
 --stage-name test \
 --endpoint-url=http://localhost:4567
Now we can test it:
curl http://localhost:4567/restapis/487109A-Z548/test/_user_request_/HowMuchIsTheFish
Response:
  % 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"}
I hope this helps.
Hint 1: For easier use I recommend to install AWSCLI Local ( https://github.com/localstack/awscli-local ) - with this tool you can use the command "awslocal" and don't have to type "--endpoint-url= ..." for each command
Walkthrough for using Serverless Framework and Localstack:
You can also use the Serverless Framework (https://serverless.com/).
First install it via npm:
npm install serverless -g
Now you can create a sample application based on a nodejs-aws template:
serverless create --template aws-nodejs
In order to have an HTTP endpoint, you have to edit the serverless.yml and add the corresponding event :
functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: ping
          method: get
In order to run this against your localstack installation you have to use the serverless-localstack plugin ( https://github.com/temyers/serverless-localstack):
npm install serverless-localstack
Now you have to edit your serverless.yml again, add the plugin and adjust your endpoints. In my case localstack is running inside the Docker toolbox, so it's IP is 192.168.99.100 - you may have to change this to localhost, depending on your use:
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
Now you can try to deploy it:
serverless deploy --verbose --stage local
This will create an S3 bucket, upload your lambda and create a cloudformation stack. However, the process will fail due to some inconsistencies of localstack when compared against AWS. Don't be dismayed though, the created cloudformation template works fine and you just need an additional request and you are done:
awslocal cloudformation update-stack --template-body file://.serverless/cloudformation-template-update-stack.json --stack-name aws-nodejs-local
Now your lambda is deployed and can be tested:
curl http://192.168.99.100:4567/restapis/75A-Z278430A-Z/local/_user_request_/ping
Response:
% 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}}

Comments

  1. Golden Nugget Casino - Mapyro
    Golden 원주 출장안마 Nugget Casino is Norwalk 광명 출장마사지 Casino. Golden Nugget Casino is a 4-star property in Norwalk, New York City. 김포 출장안마 It is owned by the American Address: 3201 S. 대구광역 출장안마 Las Vegas Blvd; Las Vegas, NV 89109Phone: (702) 770-5000Transaction Name: 익산 출장샵 Golden Nugget Casino & Resort

    ReplyDelete
  2. Wynn Hotel Casino and Spa - Mapyro
    The Wynn Resort 성남 출장마사지 features spacious rooms with an expansive casino 거제 출장안마 floor, sensuous spa treatments, 강원도 출장안마 and expansive meeting space. 김천 출장샵 Guests can 당진 출장마사지 enjoy free Wi-Fi in

    ReplyDelete
  3. Found in ninety nine.99% of all casinos around the globe, Roulette is available in} multiple of} variations and kinds. However, roulette additionally provides a stunning stage of depth for the actually serious players out there. The European roulette version of the sport is played with the single-zero European wheel, which suggests have the ability to|you possibly can} count on to significantly enhance your odds. The house edge on this version is half the house fringe of the American variations, and it is set 1xbet at 2.63%. Plus, it is possible for players to additional decrease the house edge, reaching 1.4%. This occurs when the ball rests within the colored pocket with the zero-digit on it.

    ReplyDelete
  4. Provider, whose experience extends in 카지노사이트 lots of} instructions (e.g. administration software program, fantasy sports). Its choices in the area of slots include 2D/3D games that may be} deployed to Web or Mobile, and include custom-made reels and VFX. The best time to take pleasure in games is when you're up} in a positive mood, as wagering when you are in a poor mood or frustrated typically doesn't end properly. You are considerably less more likely to|prone to} adopt a deliberate method to playing and even to heed your instinct. Blackjack is a straightforward card sport with an element of talent to it — and the odds of profitable aren't too unhealthy.

    ReplyDelete

Post a Comment