serverless
설치
awscli
brew install awscli
aws --version
awslogs
pip install awslogs
pip3 install awslogs
# awslogs 확인
awslogs get /aws/lambda/helloworld -S -G --watch
nodejs
# brew install node@12
brew install node
node -v
npm install -g n
n lts
node -v
v12.19.0
serverless
npm install -g serverless
AWS 인증서 설치
aws configure
cat ~/.aws/config
[profile brighant]
region = ap-northeast-2
output = json
cat ~/.aws/credentials
[brighant]
aws_access_key_id=xxx
aws_secret_access_key=xxx
[default]
로 설정하면 --profile default
명령인자를 입력하지 않아도 기본값이 default로 설정된다.
serverless
create
serverless create --template aws-nodejs --path hwlloworld --name helloworld
sls create -t aws-nodejs -p helloworld -n helloworld
Directory
└── helloworld
├── handler.js
└── serverless.yml
serverless.yml
service: helloworld
provider:
name: aws
runtime: nodejs12.x
functions:
hello:
handler: handler.hello
Run
sls invoke local --function hello --logs
sls invoke local -f hello -l
Package
sls package
Deploy
sls deploy --profile brighant
sls deploy --profile brighant --stage dev --region ap-northeast-2
sls deploy --profile brighant -s dev -r ap-northeast-2
Trigger
serverless.ymla
functions:
hello:
handler: handler.hello
events:
- http:
path: hello/get
method: get
Invoke result endpoints
sls deploy -s production -r ap-northeast-2
...
api keys:
None
endpoints:
GET - https://udgrtfgbnjn6.execute-api.ap-northeast-2.amazonaws.com/production/hello/get
functions:
hello: helloworld-production-hello
...
기존 Gateway Endpoint 설정
provider:
name: aws
runtime: nodejs12.x
region: ap-northeast-2
apiGateway:
restApiId: xxxxxxxxxx
restApiRootResourceId: xxxxxxxxxx
node install package
npm i http
modules/http_module.js
const http = require('http');
module.exports = {
getUserByHttp: () => new Promise((resolve, reject) => {
const options = {
host: 'jsonplaceholder.typicode.com',
port: 80,
path: '/users',
method: 'GET',
headers: {
'Content-Type': 'applications/json'
}
}
const request = http.request(options, (response)=>{
let body = '';
response.on('data', (data)=>{
body += data;
});
response.on('end', ()=>{
resolve(JSON.parse(body));
});
response.on('error', (err)=>{
console.error('http-error:', err);
});
});
request.end();
})
}
handler.js
'use strict';
require('dotenv').config();
const httpModule = require("./modules/http_module");
module.exports.hello = async event => {
const httpResult = await httpModule.getUserByHttp();
console.log('##### HTTP RESULT #####');
if(httpResult){
httpResult.forEach(user => {
console.log("id = %d, nmae = %s, email = %s", user.id, user.name, user.email);
});
}
};
환경변수
serverless.yaml
provider:
name: aws
runtime: nodejs12.x
region: ap-northeast-2
environment:
key1:
value1
key2:
value2
그외 기본 설정
serverless.yaml
provider:
name: aws
runtime: nodejs12.x
memorySize: 128
timeout: 5
function:
hello:
handler: handler.hello
description: hellolambda.
memorySize: 512
timeout: 10
serverless plugins
list
sls plugin list
sls plugin search -q sns
prune
sls plugin install -n serverless-prune-plugin
serverless.yaml 자동 추가
plugins:
- serverless-prune-plugin
serverless.yaml 설정
custom:
prune:
automatic: true
number: 5
Multi Endpoint api
install lambda-api
npm
handler.js
'use strict';
module.exports.router = async event => {
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'Go Serverless v1.0! Your function executed successfully!',
input: event,
},
null,
2
),
};
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};
serverless.yaml
service: restapi
provider:
name: aws
runtime: nodejs12.x
functions:
router:
handler: handler.router
반응형
'aws' 카테고리의 다른 글
AWS IAM 사용자 및 그룹에 MFA 강제 (0) | 2024.02.05 |
---|---|
AWS ISO 27001:2013, 27017:2015, 27018:2014, 9001:2015 (0) | 2019.07.22 |
AWS SES(Amazone Simple Email Service) (0) | 2019.04.12 |
AWS SNS(Amazon Simple Notification Service) SMS (0) | 2019.04.02 |