标签: aws-lambda

在Python中解析多部分请求字符串

我有这样的字符串

"--5b34210d81fb44c5a0fdc1a1e5ce42c3\r\nContent-Disposition: form-data; name=\"author\"\r\n\r\nJohn Smith\r\n--5b34210d81fb44c5a0fdc1a1e5ce42c3\r\nContent-Disposition: form-data; name=\"file\"; filename=\"example2.txt\"\r\nContent-Type: text/plain\r\nExpires: 0\r\n\r\nHello World\r\n--5b34210d81fb44c5a0fdc1a1e5ce42c3--\r\n"
Run Code Online (Sandbox Code Playgroud)

我也有其他标题可用的请求标头。

如何使用Python3轻松解析?

我正在通过API网关在AWS Lambda中处理文件上传,请求主体和标头可通过Python dict使用。

关于堆栈溢出还有其他类似的问题,但是大多数都假设使用requests模块或其他模块,并且期望请求详细信息采用特定的对象或格式。

注意:我知道可以将用户上传到S3并触发Lambda,但是在这种情况下,我故意选择不这样做。

python aws-lambda aws-api-gateway

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

调节Lambda时,AWS Lambda + API网关返回502错误

同步调用:如果该函数被同步调用并受到限制,则Lambda返回429错误,并且调用服务负责重试。ThrottledReason错误代码说明您遇到的是功能级别限制(如果指定)还是帐户级别限制(请参阅下面的注释)。每个服务可能都有自己的重试策略。例如,CloudWatch Logs最多重试失败的批次五次,两次重试之间有延迟。有关事件源及其调用类型的列表,请参阅支持的事件源。

参考

我不确定我对上述句子的理解是正确的,如果我错了,请修复我。

  1. 限制lambda时,它会返回429 ErrorAPI网关。
  2. 在此处的API网关中,调用服务将重试该请求。

但是,它没有按预期工作。以下是限制lambda时来自cloudWatch的API网关日志。

API-Gateway-Execution-Logs_3f1frvtwe4/sam-sm-test 2a38a4a9316c49e5a833517c45d31070 (bededbf0-73ae-11e8-87a2-f51933ef104f) Endpoint response body before transformations: {"Reason":"ReservedFunctionConcurrentInvocationLimitExceeded","Type":"User","message":"Rate Exceeded."}
API-Gateway-Execution-Logs_3f1frvtwe4/sam-sm-test 2a38a4a9316c49e5a833517c45d31070 (bededbf0-73ae-11e8-87a2-f51933ef104f) Endpoint response headers: {Connection=keep-alive, x-amzn-RequestId=bedfc624-73ae-11e8-bd28-6345cb3606c4, x-amzn-ErrorType=TooManyRequestsException, Content-Length=104, Date=Tue, 19 Jun 2018 10:51:39 GMT, Content-Type=application/json}
API-Gateway-Execution-Logs_3f1frvtwe4/sam-sm-test 2a38a4a9316c49e5a833517c45d31070 (bededbf0-73ae-11e8-87a2-f51933ef104f) Execution failed due to configuration error: Malformed Lambda proxy response
Run Code Online (Sandbox Code Playgroud)

实际上,Lambda返回{"Reason":"ReservedFunctionConcurrentInvocationLimitExceeded","Type":"User","message":"Rate Exceeded."}的API网关格式错误(代理集成),因此,API网关返回502 Error给调用API的客户端。

我希望重试失败的请求。我该如何处理?

amazon-web-services aws-lambda aws-api-gateway

2
推荐指数
1
解决办法
2208
查看次数

手动签署要在Lambda中使用的AppSync URL会导致错误的签名错误

在Lambda中,我想签署我的AppSync端点,aws-signature-v4以便将其用于突变.

生成的URL似乎没问题,但是当我尝试它时,它会给我以下错误:

{ "errors" : [ { "errorType" : "InvalidSignatureException", "message" : "The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details. etc... } ] }

这是我的lambda函数

import { Context, Callback } from 'aws-lambda';
import { GraphQLClient } from 'graphql-request';

const v4 = require('aws-signature-v4');

export async function handle(event: any, context: Context, callback: Callback) {
  context.callbackWaitsForEmptyEventLoop = false;

  const …
Run Code Online (Sandbox Code Playgroud)

amazon-web-services aws-lambda aws-appsync

2
推荐指数
1
解决办法
1687
查看次数

Cloudformation-'AWS_PROXY'当前仅支持Lambda函数

我想使用cloudformation创建堆栈时出现以下错误:

The following resource(s) failed to create: [ApiSyncLogsPost]. . Rollback requested by user.
CREATE_FAILED   AWS::ApiGateway::Method ApiSyncLogsPost Integrations of type 'AWS_PROXY' currently only supports Lambda function and Firehose stream invocations. (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; Request ID: 8bff48b0-93e4-11e8-a183-5de2b976f282)
Run Code Online (Sandbox Code Playgroud)

我在Google上没有发现与此错误相关的任何信息,当我搜索它时,我只得到了很多AWS Cloudformation文档。

我认为正在发生的是 ApiSyncLogsPost资源定义不正确,但是我在互联网上发现的示例与此类似。

为了解决该错误需要更改什么?

这是cloudformation json:

{
"AWSTemplateFormatVersion": "2010-09-09",
"Metadata": {
    "AWS::CloudFormation::Designer": {
        "a73e3686-2291-49a2-b052-565c5f85a9c9": {
            "size": {
                "width": 60,
                "height": 60
            },
            "position": {
                "x": 270,
                "y": 120
            },
            "z": 2,
            "parent": "d68e59d4-72c0-425a-9b7b-227cbac3252d",
            "embeds": []
        },
        "0665ec07-964b-4c90-86c5-5f7731cece7b": { …
Run Code Online (Sandbox Code Playgroud)

aws-cloudformation aws-lambda aws-api-gateway

2
推荐指数
1
解决办法
864
查看次数

未授权CloudFormation执行:资源上的iam:PassRole

这是我template.yml在Cloud9 中的代码的一部分:

Type: 'AWS::Serverless::Function'
Properties:
  Handler: index.handler
  Runtime: nodejs6.10
  CodeUri: .
  Description: Updates records in the AppConfig table.
  MemorySize: 128
  Timeout: 3
  Role: 'arn:aws:iam::579913947261:role/FnRole'
  Events:
    Api1:
      Type: Api
      Properties:
Run Code Online (Sandbox Code Playgroud)

当我在Cloud9中提交更改时,尝试在CodePipeline Deploy阶段部署失败ExecuteChangeSet。我收到此错误:

未授权CloudFormation执行:资源上的iam:PassRole

有人可以帮忙吗?

amazon-web-services aws-cloudformation amazon-iam aws-lambda

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

在lambda函数中生成用于dynamodb的唯一密钥

DynamoDB没有为您自动生成唯一密钥的选项。

在示例中,我看到人们从字段组合中创建uid,但是有没有办法为不具有任何值组合的数据创建唯一ID,该值可以充当唯一标识符?我的问题专门针对lambda函数。

我看到的一个选择是基于时间戳创建一个uuid,末尾带有一个计数器,将其插入(或检查它是否存在),并在重复的情况下尝试递增一次直到成功。但是,这意味着我可能会在不创建条目的情况下超出lambda函数的执行时间限制。

node.js amazon-dynamodb aws-lambda serverless

2
推荐指数
3
解决办法
4175
查看次数

CloudFormation自定义资源未完成删除

我已经设置了自定义资源,以在删除后立即返回

const aws = require('aws-sdk')
const util = require('util')

exports.handler = (event, context) => {
  console.log('Event>>>')
  console.log(JSON.stringify(event))
  aws.config.update({ region: event.ResourceProperties.Region })

  if (event.RequestType === 'Delete') return ApiMethodCustom.sendResponse(event, context, 'SUCCESS') // HERE!

  ApiMethodCustom.setupIntegration(event, context)
}

  static async sendResponse(event, context, responseStatus, responseData = {}) {
    var responseBody = JSON.stringify({
      Status: responseStatus,
      Reason: "See the details in CloudWatch Log Stream: " + context.logStreamName,
      PhysicalResourceId: context.logStreamName,
      StackId: event.StackId,
      RequestId: event.RequestId,
      LogicalResourceId: event.LogicalResourceId,
      Data: responseData
    });

    console.log("RESPONSE BODY:\n", responseBody);

    var https = require("https");
    var url …
Run Code Online (Sandbox Code Playgroud)

aws-cloudformation aws-lambda

2
推荐指数
1
解决办法
1734
查看次数

软件包Amazon.Lambda.Tools 3.0.1与netcoreapp2.1(.NETCoreApp,Version = v2.1)不兼容。

我已经安装了dotnet工具。

安装--global Amazon.Lambda.Tools --version 3.0.1并尝试在.net核心项目2.1中使用。我在设置项目文件时遇到此错误。

 <ItemGroup>
    <DotNetCliToolReference Include="Amazon.Lambda.Tools" Version="3.0.1" />
 </ItemGroup>  
Run Code Online (Sandbox Code Playgroud)

但我收到此错误。

软件包Amazon.Lambda.Tools 3.0.1与netcoreapp2.1(.NETCoreApp,Version = v2.1)不兼容。软件包Amazon.Lambda.Tools 3.0.1支持:netcoreapp2.1(.NETCoreApp,Version = v2.1)

救命??

amazon-web-services aws-sdk aws-lambda

2
推荐指数
1
解决办法
1515
查看次数

如何从AWS Lambda(Node.js)的处理程序中调用module.exports

这就是AWS中的说法:

函数中的module-name.export值。例如,“ index.handler”在index.js中调用exports.handler。

并正确调用此函数:

exports.handler = (username, password) => {
    ...
}
Run Code Online (Sandbox Code Playgroud)

但是,如果代码是这样的:

module.exports = (username, password) => {
    ...
}
Run Code Online (Sandbox Code Playgroud)

我怎么称呼它?没什么我想喜欢的作品module.exports,module.handler等等。

node.js aws-lambda

2
推荐指数
1
解决办法
5059
查看次数

Lambda + Python +退出代码

我遇到了一个用Python编写的简单AWS Lambda函数的问题。

当我运行Lambda函数时,我的代码正在按预期运行,结果是正确的,但仍然以错误代码(退出代码)结尾:"Process exited before completing request",这导致Lambda运行3次(异步)。

您是否有最佳实践来管理Lambda的退出代码?

#!/usr/bin/python

import boto3
import sys
import tweepy
import datetime

session = boto3

# Init s3 client
s3 = session.resource('s3')

def get_data_and_push(s3_bucket, s3_key, user):
    # Retrieve CSV file
    try:
        dest = s3.Object(s3_bucket, s3_key)
        dest.download_file(tmpfile)
    except Exception, e:
        print 'An error occured while trying to download CSV file'
        print 'This exception has been thrown :'
        print e
        sys.exit(1)

    # Authenticate to Twitter
    try:
        auth = tweepy.OAuthHandler(t_consumer_key, t_consumer_secret)
        auth.set_access_token(t_access_token_key, t_access_token_secret)
        api = tweepy.API(auth) …
Run Code Online (Sandbox Code Playgroud)

python amazon-web-services aws-lambda

2
推荐指数
1
解决办法
2029
查看次数