简体   繁体   English

AWS Lambda 发送 HTTP 请求

[英]AWS Lambda sending HTTP request

This is likely a question with an easy answer, but i can't seem to figure it out.这可能是一个很容易回答的问题,但我似乎无法弄清楚。

Background: I have a python Lambda function to pick up changes in a DB, then using HTTP post the changes in json to a URL.背景:我有一个 python Lambda 函数来获取数据库中的更改,然后使用 HTTP 将 json 中的更改发布到 URL。 I'm using urllib2 sort of like this:我使用 urllib2 有点像这样:

# this runs inside a loop, in reality my error handling is much better
request = urllib2.Request(url)
request.add_header('Content-type', 'application/json')
try:
    response = urllib2.urlopen(request, json_message)
except:
    response = "Failed!"

It seems from the logs either the call to send the messages is skipped entirely, or times-out while waiting for a response.从日志看来,要么完全跳过发送消息的调用,要么在等待响应时超时。

Is there a permission setting I'm missing, the outbound rules in AWS appear to be right.是否有我遗漏的权限设置,AWS 中的出站规则似乎是正确的。 [Edit] - The VPC applied to this lambda does have internet access, and the security groups applied appear to allow internet access. [编辑] - 应用于此 lambda 的 VPC 确实可以访问 Internet,并且应用的安全组似乎允许访问 Internet。 [/Edit] [/编辑]

I've tested the code locally (connected to the same data source) and it works flawlessly.我已经在本地测试了代码(连接到相同的数据源)并且它完美地工作。

It appears the other questions related to posting from a lambda is related to node.js, and usually because the url is wrong.与从 lambda 发布相关的其他问题似乎与 node.js 相关,通常是因为 url 错误。 In this case, I'm using a requestb.in url, that i know is working as it works when running locally.在这种情况下,我使用的是 requestb.in url,我知道它在本地运行时可以正常工作。

Edit:编辑:

I've setup my NAT gateway, and it should work, I've even gone as far as going to a different AWS account, re-creating the conditions, and it works fine.我已经设置了我的 NAT 网关,它应该可以工作,我什至去另一个 AWS 账户,重新创建条件,它工作正常。 I can't see any Security Groups that would be blocking access anywhere.我看不到任何会阻止任何地方访问的安全组。 It's continuing to time-out.它继续超时。

Edit: Turns out i was just an idiot when i setup my default route to the NAT Gateway, out of habit i wrote 0.0.0.0/24 instead of 0.0.0.0/0编辑:事实证明,当我设置到 NAT 网关的默认路由时,我只是个白痴,出于习惯,我写了 0.0.0.0/24 而不是 0.0.0.0/0

If you've deployed your Lambda function inside your VPC, it does not obtain a public IP address, even if it's deployed into a subnet with a route to an Internet Gateway.如果您已在 VPC 内部署 Lambda 函数,则它不会获取公共 IP 地址,即使它部署到具有到 Internet 网关的路由的子网中也是如此。 It only obtains a private IP address, and thus can not communicate to the public Internet by itself.它只获取私有IP地址,因此无法自行与公共Internet通信。

To communicate to the public Internet, Lambda functions deployed inside your VPC need to be done so in a private subnet which has a route to either a NAT Gateway or a self-managed NAT instance .要与公共 Internet 通信,需要在私有子网中完成部署在您的 VPC 内的 Lambda 函数,该子网具有到NAT 网关或自我管理的NAT 实例路由

I have also faced the same issue.我也遇到过同样的问题。 I overcame it by using boto3 to invoke a lambda from another lambda.我通过使用 boto3 从另一个 lambda 调用一个 lambda 来克服它。

import boto3

client = boto3.client('lambda')

response = client.invoke(
    FunctionName='string',
    InvocationType='Event'|'RequestResponse'|'DryRun',
    LogType='None'|'Tail',
    ClientContext='string',
    Payload=b'bytes'|file,
    Qualifier='string'
)

But make sure that you set the IAM policy for lambda role (in the Source AWS account) to invoke that another lambda.但请确保为 lambda 角色(在源 AWS 账户中)设置 IAM 策略以调用另一个 lambda。

Adding to the above, boto3 uses HTTP at the backend.除此之外,boto3 在后端使用 HTTP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM