AWS Lambda to hit qiita api to update like and stock number.
- Node.js v22.11.0
- Serverless with AWS Lambda
自分用メモなので折りたたんでおく
- Node.jsのインストール
# 現在のLTS: Jobのv22.11.0を指定
nvm ls-remote
nvm install v22.11.0
nvm alias default 22 # default変更
- ライブラリのインストール
npm init -y
npm install --save-dev typescript
npm install --save-dev @types/node
npm install @types/aws-lambda
npm install axios
npm install dotenv
- tsconfigの設定
npx tsc --init
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs", // node.jsの場合はcommonjsで良さそう
"moduleResolution": "node",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true, // ファイル名の大文字小文字を厳密にチェック
"strict": true,
"skipLibCheck": true, // ライブラリの型チェックをスキップ
"noEmitOnError": true, // エラーがある場合はコンパイルしない
"noImplicitAny": true, // 暗黙的なany型の使用を許可しない
},
"include": ["qiita-auto-update/handler.ts"],
"compileOnSave": true
}
[!NOTE]
package.json
のtypeも同じくcommonjsにあわせる必要がある。
- VSCodeでビルドタスクを.vscode/tasks.jsonの作成(Ctrl + Shift + Bしておくとtsファイル保存時に自動でビルドされる)
- ACCESS_TOKENを.envファイルから読み込むようにする
- .envファイルの作成
- serverless-dotenv-pluginのインストール
- set up
serverless
. See Getting Started - clone this repository
- prepare QIITA_ACCESS_TOKEN from qiita settings page
- create
.env
file in the root directory and writeQIITA_ACCESS_TOKEN
andTARGET_URI
cat .env
QIITA_ACCESS_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TARGET_URI=https://qiita.com/your_account/items/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
- deploy to aws lambda
cd qiita-auto-update
sls deploy