normal development

  • Home

  • Archives

  • Categories

  • Tags

  • Admire

  • About

claudiajs-rest-api

Posted on 2019-01-19 | Edited on 2020-07-17 | In claudiajs

claudiajs : https://claudiajs.com

  1. make project
  2. install claudia api module
  3. modify package.json script command
  4. make api.js file
  5. make api gateway
  6. example code

make lambda project

1
2
3
mkdir example
cd example
npm init

install claudia api module : https://claudiajs.com/claudia-api-builder.html

1
npm install claudia-api-builder -S

modify package.json script command

package.json

1
2
3
4
5
6
...
"scripts": {
"start": "claudia create --name rest-api-test --region ap-northeast-2 --api-module api --profile claudia",
"deploy": "claudia update --profile claudia"
},
...

make a api.js file :

  • https://github.com/claudiajs/claudia-api-builder/blob/master/docs/api.md#api-definition-syntax
  • https://github.com/claudiajs/claudia-api-builder/blob/4f5c30df0365812765806ae2f9fd97e7a1287ed9/docs/api.md
1
2
3
4
5
6
7
8
9
var ApiBuilder = require('claudia-api-builder'),
api = new ApiBuilder(),
superb = require('superb');

module.exports = api;

api.get('/greet', function (request) {
return request.queryString.name + ' is ' + superb();
});

make api gateway : https://claudiajs.com/tutorials/hello-world-api-gateway.html

execute command

1
2
#claudia create --name [lambda function name, api gateway name] --region [region name] --api-module [main javascript file name] --profile [profile name]
npm run start

you can see below console as result

saving configuration

1
2
3
4
5
6
7
8
9
10
11
12
{
"lambda": {
"role": "rest-api-test-executor",
"name": "rest-api-test",
"region": "ap-northeast-2"
},
"api": {
"id": "XXXXXXXX",
"module": "api",
"url": "https://XXXXXXXX.execute-api.ap-northeast-2.amazonaws.com/latest"
}
}

you can connect to api.url

example code

github

인증관련 Reference

  • 카카오 : https://github.com/bskim/gamingonaws2017_serverless
  • setting authorization to call api by aws iam : https://docs.aws.amazon.com/ko_kr/apigateway/latest/developerguide/api-gateway-control-access-using-iam-policies-to-invoke-api.html

claudiajs-get-started

Posted on 2019-01-19 | Edited on 2020-07-17 | In claudiajs

claudiajs : https://claudiajs.com

  1. install claudia
  2. execute command npm init
  3. make lambda.js
  4. set aws credential on user root path
  5. create lambda function as command with profile
  6. invoke lambda with profile : claudia test-lambda --profile claudia
  7. update lambda : claudia update
  8. mongodb example project

install claudia

1
2
3
4
5
# global installation
npm install claudia -g

# check installation
claudia -version

make lambda project

1
2
3
mkdir example
cd example
npm init

make a lambda.js file : https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 1.normal example
exports.myHandler = function(event, context) {
...
}
// 2.you can use callback
exports.handler = function(event, context, callback) {
//console.log('Received event:', JSON.stringify(event, null, 2));
console.log('value1 =', event.key1);
console.log('value2 =', event.key2);
console.log('value3 =', event.key3);
console.log('remaining time =', context.getRemainingTimeInMillis());
console.log('functionName =', context.functionName);
console.log('AWSrequestID =', context.awsRequestId);
console.log('logGroupName =', context.log_group_name);
console.log('logStreamName =', context.log_stream_name);
console.log('clientContext =', context.clientContext);
if (typeof context.identity !== 'undefined') {
console.log('Cognitoidentity ID =', context.identity.cognitoIdentityId);
}
callback(null, event.key1); // Echo back the first key value
// or
// callback("some error type");
};
// 3.below is available on node 8.xx
exports.myHandler = async function(event, context) {
console.log("value1 = " + event.key1);
console.log("value2 = " + event.key2);
return "some success message";
// or
// throw new Error(“some error type”);
}

setting AWS IAM

  • https://claudiajs.com/tutorials/installing.html
  • https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html

make file .aws/credentials

first line is profile name.

1
2
3
[claudia]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_ACCESS_SECRET

create lambda function

seoul region is ap-northeast-2.

1
2
# create lambda
claudia create --region ap-northeast-2 --handler lambda.myHandler --profile claudia

invoke lambda

1
2
#'test-' prefix is required.
claudia test-lambda --profile claudia

update lambda with npm run command

package.json

1
2
3
4
5
6
...
"scripts": {
"test": "claudia test-lambda --profile claudia",
"deploy": "claudia update --profile claudia"
},
...

update command : npm run deploy

example code

github

1…5678

j2yes

15 posts
7 categories
24 tags
GitHub E-Mail
© 2020 j2yes