我在其构造函数中有一个带有一些代码的ac#aws lambda类。每次启动Lambda方法本身时都会调用它(带有SNS消息),但是,我看不到构造函数被调用(添加了从cloudwatch中观察到的日志调用)。仅在首次启动时(在aws堆栈创建/更新之后)调用构造函数。
这是预期的行为吗?aws是否以某种方式缓存我的lambda实例?
public class MyLambda
{
public MyLambda()
{
Console.WriteLine("Hello from ctor");
}
// This is the method assigned in CloudFormation
public bool Execute(SNSEvent snsEvent)
{
Console.WriteLine("Lambda called");
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
这是cloudwatch日志中的结果;首次启动Lambda:
Hello from ctor
Lambda called
Run Code Online (Sandbox Code Playgroud)
和第二次启动Lambda
Lambda called
Run Code Online (Sandbox Code Playgroud) 我想使用AWS lambda函数将视频上传到S3。该视频在我的本地计算机上不可用。我有“下载URL”。我不想将其下载到本地计算机上并上传到S3中。我正在寻找一种使用lambda函数将视频文件直接放入S3的解决方案。如果使用缓冲区或流式传输,则会消耗大量内存。有更好的解决方案吗?
非常感谢您的帮助。
我正在使用Terraform构建API +相应的lambda函数.
我有一些其他的基础设施,我想它很好地设置(也许我错了?):
test和prod)两个VPC上的所有资源都是相同的; 例如,有一个test-private-subnet和一个prod-private-subnet具有完全相同的规格,相同的DB,等等.
现在,我正在研究将为API提供动力的API和lambdas.
我不觉得我需要一个test&prodAPI网关和test&prodlambdas:
stage_variables具有不同ips 的API 来实现API 的testvs prod环境但是当我尝试使用vpc_config块设置lambda时(因为我需要将它与允许在DB上进行的安全组关联),我收到以下错误:
Error applying plan:
1 error(s) occurred:
* module.lambdas.aws_lambda_function.api-lambda-users: 1 error(s) occurred:
* aws_lambda_function.api-lambda-users: Error creating Lambda function: InvalidParameterValueException: Security Groups are required to be in the same VPC.
status code: 400, request id: xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx
Run Code Online (Sandbox Code Playgroud)
我的lambda配置看起来像这样:
resource "aws_lambda_function" …Run Code Online (Sandbox Code Playgroud) 我正在开发需要安排任务的应用程序,因此我使用AWS Lambda,但是,安排的时间是动态的,因为它取决于用户请求,所以无法使用AWS控制台进行安排,因此我使用AWS Javascript SDK对其进行调度。这是流程:
以下是我编写的Node.js代码
schedule_aws_lambda: function(booking_id, cronTimeIST, callback){
var event = new AWS.CloudWatchEvents({
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
region: 'eu-west-1'
});
var lambda = new AWS.Lambda({
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
region: 'eu-west-1'
});
var year = cronTimeIST.utc().year();
var month = cronTimeIST.utc().month() + 1;
var date = cronTimeIST.utc().date();
var hour = cronTimeIST.utc().hour();
var minute = cronTimeIST.utc().minute();
var cronExpression = "cron(" + minute + " "+ hour + " " + date + " " + month …Run Code Online (Sandbox Code Playgroud) javascript amazon-web-services amazon-cloudwatch aws-sdk aws-lambda
我在node6中有一个lambda函数,它有5个env变量,全部用aws kms加密.我有以下方法,它采用加密密钥并返回解密密钥.
function decryptKMS(encryptedKey) {
console.log('inside decryptkms');
const kms = new AWS.KMS();
kms.decrypt({ CiphertextBlob: new Buffer(encryptedKey, 'base64') }, (err, data) => {
if (err) {
console.log('Decrypt error:', err);
return callback(err);
}
var result = data.Plaintext.toString('ascii');
return result;
});
}
Run Code Online (Sandbox Code Playgroud)
在我的处理程序中,我这样做是为了获取我的解密密钥.
decryptedkey1 = decryptKMS(encryptedkey1);
decryptedkey2 = decryptKMS(encryptedkey2);
decryptedkey3 = decryptKMS(encryptedkey3);
decryptedkey4 = decryptKMS(encryptedkey4);
decryptedkey5 = decryptKMS(encryptedkey5);
Run Code Online (Sandbox Code Playgroud)
但是,由于节点是异步的,因此在解密密钥之前,该功能会移至下一步.无论如何我可以使用节点承诺来组合所有密钥,或者有没有办法从kms一次解密多个密钥?
我试图从Lambda向另一个API发出请求。我发现使用NodeJS http和https模块可以执行GET请求,但是其他任何模块(例如POST)都无法使用;巧合的是,POST是我要尝试调用的服务所需的唯一方法。
这是Lambda执行GET并收到200响应的工作示例:
const https = require('https')
function handler(event, context, callback) {
const options = {
hostname: 'encrypted.google.com'
}
https
.get(options, (res) => {
console.log('statusCode:', res.statusCode);
res.on('end', callback.bind(null, null))
})
.on('error', callback);
}
exports.handler = handlerRun Code Online (Sandbox Code Playgroud)
这样就证明他的要求是允许的。但是,如果脚本尝试使用.request()https(或https)lib / module的方法发出相同的请求,则该请求将永远不会完成,并且Lambda会超时。
const https = require('https')
function handler(event, context, callback) {
const options = {
hostname: 'encrypted.google.com',
method: 'GET'
}
https
.request(options, (res) => {
console.log('statusCode:', res.statusCode);
res.on('end', callback.bind(null, null))
})
.on('error', callback);
}
exports.handler = handlerRun Code Online (Sandbox Code Playgroud)
我不知道我在做什么错。调用https.request()将以静默方式失败-不会引发错误-并且日志中未报告任何内容。
我正在尝试为使用AWS Lambda的应用程序编写无服务器后端,并且遇到标题中的错误。使用API Gateway代理集成进行测试时会发生错误,但是在Lambda控制台中进行测试时,该功能可以正常工作。
这是错误:
{
"errorMessage":"string indices must be integers",
"errorType":"TypeError",
"stackTrace":[
[
"/var/task/auth_login.py",
17,
"lambda_handler",
"response = get_user(payload)"
],
[
"/var/task/shifty_utils/__init__.py",
22,
"get_user",
"table = dynamo.Table(user['company'] + '_users')"
]
]
}
Run Code Online (Sandbox Code Playgroud)
这是发生情况的上下文:
def lambda_handler(event, context):
payload = event['body']
response = get_user(payload)
def get_user(user):
try:
table = dynamo.Table(user['company'] + '_users')
response = table.get_item(
Key={
'userId': user['userId'],
'position': user['position']
}
)
except ClientError as e:
print(e.response['Error']['Message'])
return {'message': e.response['Error']['Message']}
else:
return response
Run Code Online (Sandbox Code Playgroud)
基本上,代理集成似乎是以JSON格式的字符串而不是dict的形式读取事件对象,但是如果我为此调整代码,就会发生以下情况:
{
"errorMessage":"the JSON object must be str, bytes …Run Code Online (Sandbox Code Playgroud) 我创建了一个Lambda函数,我希望每隔5分钟调用一次,或者每天调用一次,或者其他什么.我如何用对流层进行设置?
我无法在任何地方找到一个例子.
我的Lambda在请求正文(event.body)中从我的用户接收图像的二进制数据。
我尝试将其正确上传到S3,但是下载时,图像已损坏/无法打开。
我还需要将上传图像的URl返回给用户。
请帮忙!
module.exports.uploadImage = (event, context, callback) => {
var buf = new Buffer(new Buffer(event.body).toString('base64').replace(/^data:image\/\w+;base64,/, ""),'base64');
var data = {
Key: Date.now()+"",
Body: buf,
ContentEncoding: 'base64',
ContentType: 'image/png',
ACL: 'public-read'
};
s3Bucket.putObject(data, function(err, data){
if (err) {
console.log(err);
console.log('Error uploading data: ', data);
} else {
console.log('succesfully uploaded the image!');
}
callback(null,data);
});
};
Run Code Online (Sandbox Code Playgroud) 我试图通过AWS API Gateway的自定义授权程序来授权API调用,
这基本上是一个自定义的lambda函数,它接受以下格式的以下标题 -
{
"authorizationToken": "0c34ba00bde34200b383abe22bcfef96",
"methodArn": "arn:aws:execute-api:ap-southeast-1:855399270504:z6t3cv0z4m/null/GET/",
"type": "TOKEN"
}
Run Code Online (Sandbox Code Playgroud)
并期望以下格式的回复 -
{
"principalId": "xxxxxxx", // the principal user identification associated with the token send by the client
"policyDocument": { // example policy shown below, but this value is any valid policy
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"execute-api:Invoke"
],
"Resource": [
"arn:aws:execute-api:us-east-1:xxxxxxxxxxxx:xxxxxxxx:/test/*/mydemoresource/*"
]
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
我能够使用authorizationToken执行内部逻辑并验证函数是否应该在"允许"或"拒绝"策略中响应,
但是当我尝试从控制台测试授权器时,我收到解析错误,
以下是我的请求日志 -
Execution log for request test-request
Thu Jun 29 11:48:10 UTC 2017 …Run Code Online (Sandbox Code Playgroud) java amazon-web-services amazon-iam aws-lambda aws-api-gateway
aws-lambda ×10
node.js ×4
amazon-s3 ×2
aws-sdk ×2
amazon-iam ×1
amazon-vpc ×1
asynchronous ×1
aws-kms ×1
c# ×1
file-upload ×1
java ×1
javascript ×1
python ×1
terraform ×1
troposphere ×1