要将AWS Lambda与Typescript和webpack的NewRelic集成,可以按照以下步骤操作:
npm install aws-sdk newrelic
npm install --save-dev typescript webpack webpack-cli ts-loader
tsconfig.json
文件,并将以下内容添加到该文件中:{
"compilerOptions": {
"module": "commonjs",
"target": "es2017",
"moduleResolution": "node",
"esModuleInterop": true,
"outDir": "./dist",
"sourceMap": true,
"declaration": true,
"strict": true
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}
webpack.config.js
文件,并将以下内容添加到该文件中:const path = require('path');
module.exports = {
entry: './src/index.ts',
target: 'node',
mode: 'production',
module: {
rules: [{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
}]
},
resolve: {
extensions: ['.tsx', '.ts', '.js']
},
output: {
filename: 'index.js',
path: path.resolve(__dirname, 'dist')
}
};
src
目录下创建一个index.ts
文件,并将以下内容添加到该文件中:import newrelic from 'newrelic';
export const handler = async (event: any, context: any) => {
// Your Lambda function code here
};
newrelic.js
文件,并将以下内容添加到该文件中:'use strict';
if (process.env.NEW_RELIC_LICENSE_KEY) {
require('newrelic');
}
package.json
文件中添加以下scripts:"scripts": {
"build": "webpack",
"start": "node -r newrelic dist/index.js"
}
使用npm run build
命令来生成打包后的代码。
将生成的index.js
和newrelic.js
文件上传到AWS Lambda中。
现在,你的AWS Lambda函数已经与Typescript、webpack和NewRelic集成了。你可以执行npm start
命令来启动Lambda函数,并且可以在NewRelic中查看相关的监控和日志信息。