标签: aws-serverless

在 AWS Cloudwatch 中查找 lambda 函数错误的日志?

我承认我还没有真正开始部署 AWS Lambda 函数,但本文是这样说的:

AWS CloudWatch 上的日志非常糟糕。我花了很长时间才找到简单应用程序的失败功能,想象一下大规模应用程序的恐怖。

在 AWS Cloudwatch 上过滤日志有什么特别困难的地方吗?例如,如果我们从 Servless 的节点模板部署 stock hello lambda,我想在日志中找到调用(错误或其他)是相当容易的?

如果有人有任何简单的最佳实践技巧,我们可以遵循这些技巧来使日志语句易于找到,那就太棒了!

amazon-web-services node.js amazon-cloudwatch serverless-framework aws-serverless

5
推荐指数
1
解决办法
1921
查看次数

为 Amazon ApiGateway 中的 URI 指定的 HTTP 终端节点无效

我正在尝试使用 cloudformation 模板为 API 网关创建一个具有 GET 方法的资源 /user/devices 但它给了我一个以下错误

发生错误:ApiGatewayRootMethod - 为 URI 指定的 HTTP 端点无效(服务:AmazonApiGateway;状态代码:400;错误代码:BadRequestException;请求 ID:xxxxxxxxxx)

下面是我的cloudformation模板,

AWSTemplateFormatVersion: 2018-11-13
Description: test user

resources:
  Resources:

    UserDeviceApiGateway:
      Type: "AWS::ApiGateway::RestApi"
      Properties:
        Name: "test-user-info"
        Description: "Fetch the user"

    UserResource:
      Type: 'AWS::ApiGateway::Resource'
      Properties:
        ParentId:
          Fn::GetAtt: ["UserDeviceApiGateway","RootResourceId"]
        RestApiId:
          Ref: "UserDeviceApiGateway"
        PathPart: 'user'

    Resource:
      Type: 'AWS::ApiGateway::Resource'
      Properties:
        ParentId:
          Ref: "UserResource"
        RestApiId:
          Ref: "UserDeviceApiGateway"
        PathPart: 'devices'

    ApiGatewayRootMethod:
      Type: "AWS::ApiGateway::Method"
      Properties:
        AuthorizationType: "NONE"
        HttpMethod: "GET"
        Integration:
          IntegrationHttpMethod: "GET"
          Type: "HTTP"
          Uri: Sub
            - "arn:aws:apigateway:arn:aws:lambda:eu-west-1:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-west-1:xxxxxxxx:function:user-device-lambda/invocations"
        ResourceId:
          Fn::GetAtt: ["UserDeviceApiGateway","RootResourceId"]
        RestApiId: …
Run Code Online (Sandbox Code Playgroud)

amazon-web-services node.js aws-api-gateway serverless-framework aws-serverless

5
推荐指数
1
解决办法
1万
查看次数

Lambda 无法访问 KMS 密钥

当我运行我的 lambda 代码时,我收到以下错误:

The ciphertext refers to a customer master key that does not exist, does not exist in this region, or you are not allowed to access.

我主要遵循使用 aws-sam-cli 创建堆栈,模板的相关部分在代码下方。

相关代码是:

const ssm = new AWS.SSM();
const param = {
    Name: "param1",
    WithDecryption: true
};
const secret = await ssm.getParameter(param).promise();
Run Code Online (Sandbox Code Playgroud)

template.yaml 文件的相关部分是:

KeyAlias:
    Type: AWS::KMS::Alias
    Properties:
      AliasName: 'param1Key'
      TargetKeyId: !Ref Key
Key:
    Type: AWS::KMS::Key
    Properties:
      KeyPolicy:
        Id: default
        Statement:
        - Effect: Allow
          Principal:
            AWS: !Sub arn:aws:iam::${AWS::AccountId}:root
          Action: …
Run Code Online (Sandbox Code Playgroud)

amazon-web-services aws-lambda aws-sam-cli aws-serverless serverless-application-model

5
推荐指数
1
解决办法
1万
查看次数

按计划在每个 DynamoDb 条目上运行 lambda?

有没有办法在每个 DynamoDb 表记录上运行 Lambda?

我有一个 Dynamo 表,其中包含姓名、姓氏、电子邮件和一个以姓名、姓氏、电子邮件作为参数的 Lambda 。我正在尝试配置环境,以便每天 Lambda 自动运行它在 Dynamo 中找到的每个值;无法在一个 Lambda 中完成所有记录,因为它不会扩展(添加更多用户后将超时)。

我目前设置了一个 CloudWatch 规则,可以按计划触发 lambda,但我必须手动将参数添加到 Dynamo 的触发器中 - 它不是自动的,也不是动态的/未连接到 dynamo。

——

另一种选择是在每次更新 DynamoDb 记录时运行 lambda...我可以每周更新所有记录,然后在更新它们时将触发 Lambda,但我不知道这是否可能。

对这些方法之一的更多见解将不胜感激!

amazon-web-services amazon-dynamodb amazon-cloudwatch aws-lambda aws-serverless

5
推荐指数
1
解决办法
2156
查看次数

AWS 无服务器功能未响应图像

我正在尝试让 AWS API Gateway 使用图像进行响应。我的无服务器 Lambda 代码如下

const express = require('express');
const serverless = require('serverless-http');
const bodyParser = require('body-parser');
const request = require('request');
const fetch = require('node-fetch')
var Jimp = require('jimp');
const app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))

app.get('/image/:id', async(req, res) => {
    const id = req.params.id;

    var imgUrl = 'https://developer.salesforce.com/forums/profilephoto/729F00000005O41/T';
    let options = {};

    const image = await Jimp.read(imgUrl);
    image.getBuffer(Jimp.MIME_JPEG, (err, buffer) => {
        res.set('content-type', 'image/jpeg');
        res.send(buffer.toString('base64'));
    });
});
// wrap express app instance with serverless http …
Run Code Online (Sandbox Code Playgroud)

amazon-web-services node.js serverless-framework aws-serverless

5
推荐指数
2
解决办法
2478
查看次数

通过 serverless 设置 API 网关的请求验证器

我想通过无服务器设置 API 网关的请求验证器。我为请求验证器尝试了两种不同的设置。但是,这两种方法都失败了。我已经总结了我所做的,所以如果有什么问题,请告诉我。

  1. 我在 swagger(OAS3.0) 中编写了 API 规范。因此我尝试使用OAS扩展来实现Request Validator的设置。在我sls deploy使用下面的swagger.yaml和之后serverless.yml,没有将 中描述的验证模式x-amazon-apigateway-request-validators添加到请求验证器选项中。 https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-swagger-extensions-request-validator.html

在此处输入图片说明

swagger.yaml 在下面:

openapi: 3.0.0

info:
  description: xxx
  version: '0.1'
  title: xxx API
x-amazon-apigateway-request-validators:
  body-only:
    validateRequestBody: true,
    validateRequestParameters: false
  except-body:
    validateRequestBody: false,
    validateRequestParameters: true
  all:
    validateRequestBody: true,
    validateRequestParameters: true
tags:
  - name: auth
    description: xxx
paths:
  /login:
    post:
      tags:
        - auth
      summary: xxx
      description: ''
      x-amazon-apigateway-request-validator: all
      responses:
        '200':
          description: success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AuthResponse'
        '400':
          description: fail
          content:
            application/json …
Run Code Online (Sandbox Code Playgroud)

swagger aws-api-gateway serverless-framework serverless aws-serverless

5
推荐指数
1
解决办法
1130
查看次数

如何将角色传递给 cli 命令“aws cloudformation deploy”或“sam deploy”?

我正在使用 SAM 模板和 CLI 创建一个 cloudformation 堆栈。我已经使用一个帐户成功完成了此操作,该帐户从直接附加到该帐户的策略中获取了所有必需的权限。向此帐户授予所有这些权限是很差的安全实践,因此我创建了一个附加了相同策略的角色,并希望将其用于部署。但是,即使我通过--role-arn参数传递我的角色,该命令仍然会向帐户查找权限。

以下是我尝试使用的命令:

aws cloudformation deploy --template-file TemplatePackaged.yaml --stack-name TestStack --capabilities CAPABILITY_IAM --region us-east-1 --role-arn arn:aws:iam::666488004797:role/LambdaApplicationCreateRole
Run Code Online (Sandbox Code Playgroud)

或者

sam deploy --template-file TemplatePackaged.yaml --stack-name TestStack --capabilities CAPABILITY_IAM --region us-east-1 --role-arn arn:aws:iam::666488004797:role/LambdaApplicationCreateRole
Run Code Online (Sandbox Code Playgroud)

除非登录 cli 的用户具有所需的权限,否则我使用任一命令都会收到错误:

调用DescribeStacks操作时发生错误(AccessDenied):用户:arn:aws:iam :: 666488004797:user/DummyUser1无权在资源:arn:aws:cloudformation:us-east-1上执行:cloudformation:DescribeStacks: 666488004797:堆栈/梅丽莎/*

如何让deploy命令使用--role-arn参数中传递的角色来获取它需要的权限?

aws-cloudformation amazon-iam aws-cli aws-serverless

5
推荐指数
1
解决办法
2470
查看次数

在 serverless.yaml 上出现以下错误:无法读取块映射条目;多行键可能不是隐式键

我正在尝试将 lambda 函数与serverless.yml文件一起部署到 AWS,但它抛出以下错误

以下是YAML文件中定义的函数

functions:
 s3-thumbnail-generator:
 handler:handler.s3_thumbnail_generator  
   events:
     - s3:
       bucket: ${self:custom.bucket}
       event: s3.ObjectCreated:*
       rules:
       - suffix: .png

plugins:
  - serverless-python-requirements  
Run Code Online (Sandbox Code Playgroud)

我得到的错误:

无法读取块映射条目;多行键可能不是 serverless.yml" 中第 45 行第 10 列的隐式键:

我需要了解如何在 YAML 文件中解决此问题才能将函数部署到 AWS?

yaml aws-lambda aws-serverless

5
推荐指数
1
解决办法
1万
查看次数

MinimizeSizeCompression 不适用于无服务器

我正在使用新的无服务器压缩选项,最小压缩大小 = 1KB。在有效负载 > 1KB 上一切正常,但有效负载 <1KB 也会被压缩。

无论我选择什么值,minimumCompressionSize 都保持为 0(见下图)

这是我的 serverless.yml:

  name: aws
  runtime: nodejs8.10
  region: eu-west-1
  apiGateway:
    minimumCompressionSize: 1024
...
Run Code Online (Sandbox Code Playgroud)

你遇到过这个问题吗?如何在不手动更新 aws 上的 api-gateway 配置的情况下解决该问题?

先感谢您。

我遵循了本指南:https://medium.com/@OneMuppet_/gzip-deflate-content-from-aws-api-gateway-using-serverless-36e208da4270

并尝试了官方文档中的内容: https://serverless.com/framework/docs/providers/aws/events/apigateway#compression

在此输入图像描述

serverless-framework serverless-plugins aws-serverless

5
推荐指数
1
解决办法
1408
查看次数

无服务器离线抛出“配置错误”或“无法读取未定义的属性‘选项’”

我正在尝试使用 NodeJS、AWS Lambda、API Gateway、RDS 和 PostgreSQL 部署无服务器 REST API。

到目前为止,我已经成功设置了 PostgreSQL RDS,在开始编写函数来处理对数据库的请求之前,我认为最好先在本地测试一个小函数以检查请求是否得到正确处理。

所以在项目的根目录下,我安装了 serverless-offline:

npm install serverless-offline

它在安装类型时发出了几个警告:

npm WARN 已弃用 @hapi/pez@4.1.2:此版本已弃用,不再支持或维护

(如果这些信息无关紧要,我很抱歉,我是新手,不知道什么是重要的,什么不重要。)

然后我配置了我的serverless.yml

service: serverless-node-postgres-rds-rest-api

app: serverless-app

frameworkVersion: '2'

provider:
  name: aws
  runtime: nodejs12.x
  lambdaHashingVersion: 20201221

plugins:
  - serverless-offline

configValidationMode: error

functions:
  hello:
    handler: handler.hello
    events:
      - httpApi:
          path: hello
          method: get
Run Code Online (Sandbox Code Playgroud)

这是handler.js

'use strict';

module.exports.hello = async (event) => {
  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: 'Go Serverless v1.0! Your function executed …
Run Code Online (Sandbox Code Playgroud)

node.js aws-serverless serverless-offline

5
推荐指数
1
解决办法
856
查看次数