node.js - 启用 aws-xray serverless.yml

标签 node.js typescript amazon-web-services aws-xray

我尝试通过以下方式为所有 lambda 函数启用 aws-xray:

无服务器.yml

provider:
  tracing:
    lambda: true
    apiGateway: true

  name: aws
  runtime: nodejs8.10

  stage: ${opt:stage, 'dev'}
  region: ${opt:region, 'ca-central-1'}

服务.ts

import * as AWS from 'aws-sdk'
import * as AWSXRay from 'aws-xray-sdk'
const XAWS = AWSXRay.captureAWS(AWS)
const docClient: DocumentClient = new XAWS.DynamoDB.DocumentClient()
const s3 = new XAWS.S3({signatureVersion: 'v4'})

sls部署之后我收到以下错误:

An error occurred: <some_lambda funcion> - The provided execution role does not have permissions to call PutTraceSegments on XRAY (Service: AWSLambdaInternal; Status Code: 400; Error Code: InvalidParameterValueException; Request ID: 364243f8-8847-48ef-87ad-75da2537e7f7).

我不确定问题是什么。我还尝试过部署:

  iamRoleStatements:
    - Effect: Allow
      Action:
        - "xray:PutTraceSegments"
        - "xray:PutTelemetryRecords"
      Resource:
        - "*"

还是同样的问题。

我将非常感谢任何帮助,因为我不知道为什么这是一个问题,特别是因为我有另一个项目以完全相同的方式启用了跟踪,没有问题!

package.json:

{
  "name": "mini-twitter",
  "version": "1.0.0",
  "description": "Serverless Mini-Twitter app",
  "dependencies": {
    "aws-xray-sdk": "^2.2.0",
    "source-map-support": "^0.5.11",
  },
  "devDependencies": {
    "@types/aws-lambda": "^8.10.17",
    "@types/node": "^10.14.4",
    "aws-sdk": "^2.433.0",
    "serverless-iam-roles-per-function": "^1.0.4",
    "serverless-webpack": "^5.2.0",
    "ts-loader": "^5.3.3",
    "typescript": "^3.4.1",
  }
}

最佳答案

您需要安装插件:

点赞 Gareth McCumskey 的评论(谢谢!)

只需使用:

serverless plugin install --name serverless-plugin-tracing

或者手动执行:

npm install --save-dev serverless-plugin-tracing

并在您的 serverless.yml 上启用它:

plugins:
  - serverless-plugin-tracing

现在您的文件如下所示:

provider:
  name: aws
  stage: test
  tracing: true # enable tracing
  iamRoleStatements:
    - Effect: "Allow" # xray permissions (required)
      Action:
        - "xray:PutTraceSegments"
        - "xray:PutTelemetryRecords"
      Resource:
        - "*"

plugins:
  - serverless-plugin-tracing

更多信息: https://serverless.com/plugins/serverless-plugin-tracing/

关于node.js - 启用 aws-xray serverless.yml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57618014/

相关文章:

amazon-web-services - AWS SQS 批量大小和消息

amazon-web-services - EC2 实例克隆后 Tomcat 版本更新

javascript - 如何使用 NodeJS 将 2 个 XLSX 文件合并为一个

node.js - 使用 NodeJS fs.watch 重命名操作获取以前的文件名

typescript - 类型错误 : Cannot find module 'firebase-functions' or its corresponding type declarations when deploying in Vercel

css - 模态 Angular 2 造型 div 体内

node.js - c9的Architect中是如何处理依赖关系的?

javascript - 玩家沿对 Angular 线移动 - 太快了 || Node JS

typescript - 无法从 Vue.js 中的方法访问数据(使用 Ionic)

amazon-web-services - AWS SAM 函数 AutoPublishAlias "Invalid function version"