我想逐行读取位于S3上的文件。我尝试了以下代码,这些代码是我在网上搜索的,但是Lambda函数在退出时没有调用任何readline回调。我究竟做错了什么?
const aws = require('aws-sdk');
const s3 = new aws.S3({ apiVersion: '2006-03-01' });
const readline = require('readline');
exports.handler = async (event, context, callback) => {
const bucket = event.Records[0].s3.bucket.name;
const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
const params = {
Bucket: bucket,
Key: key,
};
const s3ReadStream = s3.getObject(params).createReadStream();
const rl = readline.createInterface({
input: s3ReadStream,
terminal: false
});
rl.on('line', (line) => {
console.log(`Line from file: ${line}`);
});
rl.on('error', () => {
console.log('error');
});
rl.on('close', function () {
console.log('closed');
context.succeed();
});
console.log('done'); …Run Code Online (Sandbox Code Playgroud) 创建Lambda函数时,通过GUI控制台加密环境变量并不难。我只需输入密钥值对,然后打开加密助手并输入我的KMS密钥的ARN。这使我可以加密该值,因此,在将其发送存储之前已对其进行了加密,如上图所示。
我要完成的工作是完全一样的,但是假设您不是在GUI控制台中而是通过CloudFormation模板(正在CLI中进行部署)部署Lamba函数。
这是我在CloudFormation模板中指定相关参数的方式:
"EnvironmentVariable" : {
"Type" : "String",
"Default" : "test",
"Description" : "Environment Variable"
},
"KmsKeyArn" : {
"Type" : "String",
"Description" : "KMS Key ARN if environment variables are encrypted"
},
Run Code Online (Sandbox Code Playgroud)
这是我在Lambda资源(模板的“资源”部分)中引用这些参数的方式:
"Environment" : {
"Variables" : {
"SomeVariable": {
"Ref" : "EnvironmentVariable"
}
}
},
"KmsKeyArn" : { "Ref" : "KmsKeyArn" },
Run Code Online (Sandbox Code Playgroud)
这是我在CLI中部署此模板的方式(更改了所有ARN和其他值以保护隐私,但保持其结构):
AWS cloudformation部署--template-file lambda-template.json --stack-name“ CLI-lambda-stack” --parameter-overrides S3BucketName =“ theBucket” S3FileLocation =“ lambda_function.zip” S3ObjectVersion =“ ZuB0eueEgh2yh5q00.DiykLNudujdsc5” DeadLetter =“ arn:aws:sns:us-west-2:526598937246:CloudFormationTests” EnvironmentVariable =“ testing” …
encryption amazon-web-services aws-cloudformation aws-cli aws-lambda
update-function-code AWS CLI命令更新除处理程序功能文件之外的所有代码文件,lambda_function.py
具体来说,我制作了一个bash脚本
一切正常,但主功能文件lambda_function.py未更新。
奇怪的是,当我从lambda下载zip进行更改,然后上传到同一lambda时,它可以工作(所有文件都已更新)。
仅供参考,这是我的bash脚本,用于从一个lambda下载代码并上传到另一个lambda:
#!/bin/sh
SRC_LAMBDA_FUNCTION_NAME="$1"
DEST_LAMBDA_FUNCTION_NAME="$2"
export PYTHONIOENCODING=utf8
# get lambda function config
LAMBDA_JSON=$(aws lambda get-function --function-name $SRC_LAMBDA_FUNCTION_NAME)
export LAMBDA_JSON
# parse the code zip file download URL (link expires 10 minutes after creation)
ZIP_FILE_URL=$(python -c 'import json,sys,os;obj=json.loads(os.environ["LAMBDA_JSON"]);print(obj["Code"]["Location"])')
# make temp dir
mkdir -p download
# download the code from src lambda
curl -o "download/$SRC_LAMBDA_FUNCTION_NAME.zip" $ZIP_FILE_URL
# upload the code to dest lambda
aws lambda update-function-code --function-name $DEST_LAMBDA_FUNCTION_NAME --zip-file "fileb://download/$SRC_LAMBDA_FUNCTION_NAME.zip"
Run Code Online (Sandbox Code Playgroud) 我见过很多人使用SNS来触发其lambda函数,而不是使用API网关来执行它。有任何具体原因吗?
我个人认为允许API网关执行此操作比使用SNS灵活得多。关于为什么这样做有什么很好的阐述?如果我使用SNS触发该功能,是否可以提高性能或降低成本?
我有一个lambda函数,该函数曾经使用在lambda配置中设置的加密环境变量,但不再需要它们。我尝试在用户界面中删除env变量,该变量不再显示,但仍在日志中看到:
“在环境变量中找到凭据。”
我也尝试使用update-function-code命令,而没有传递不起作用的env变量。
有什么方法可以从我的lambda函数配置中删除加密的env变量?我想确保删除未使用/不需要的东西。
谢谢!
我正在使用AWS Lambda函数(使用nodejs)。
从APP发送到Cognito的任何请求以注册用户。然后,我设置了“ 预注册”触发器来验证用户的客户,并检查我们数据库中是否有可用的用户自定义属性。如果是,则返回错误,否则将新记录插入数据库并将事件返回给Cognito。
TimeoutInfo-5分钟。
它在请求中的某个时间发生,而不是一直发生。RequestId为不同的。(它将在某个时间触发3次,大部分时间两次)
Lambda触发代码如下。
用户/ index.js
const handler = async (event, context) => {
log.info('createUserLambda:start');
// immediately return once the call back is called to avoid
// lambda time out because of any open db connections
context.callbackWaitsForEmptyEventLoop = false;
return await preUserCreate(event);
};
Run Code Online (Sandbox Code Playgroud)
Exports.handler =处理程序; users / users.js
export const preUserCreate = async (event) => {
log.info('preUserCreate:Start');
let userAttributes = event.request.userAttributes;
const currentDate = moment().utc().format('YYYY-MM-DD HH:mm:ss');
try {
let userParams = {
'docStatus': 'VRF'
};
let …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过以下代码将“技能购买”添加到我的Alexa技能中:
const LaunchRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === "LaunchRequest";
},
handle(handlerInput){
console.log("In LaunchRequest");
const locale = handlerInput.requestEnvelope.request.locale;
const ms = handlerInput.serviceClientFactory.getMonetizationServiceClient();
return ms.getInSkillProducts(locale).then(function(result) {
// Code to handle result.inSkillProducts goes here
const totalProducts = result.inSkillProducts.length;
const purchasableProducts = result.inSkillProducts.filter(record => record.purchasable == 'PURCHASABLE');
const entitledProducts = result.inSkillProducts.filter(record => record.entitled == 'ENTITLED');
return handlerInput.responseBuilder
.speak('Found total ' + result.inSkillProducts.length + ' products of which ' + purchasableProducts.length + ' are purchasable and ' + entitledProducts.length + ' are entitled.') …Run Code Online (Sandbox Code Playgroud) 我正在尝试在AWS Lambda(nodejs 8.10)上配置哨兵,但没有将异常发送到Sentry。我感觉这是时间问题:在将数据发送到哨兵之前,lambda已终止。
哪种方法可以在AWS Lambda上集成哨兵?
谢谢!
我有一个带有AWS :: Lambda :: Function资源的CloudFormation模板,并且我试图将本地zip文件作为代码上传,但是没有上传。Lambda函数是在没有代码文件的情况下创建的。
Resources:
mastertestingLambdaDataDigestor:
Properties:
Code:
ZipFile: fileb:///home/dariobenitez/Proyectos/dataflow/templates/lambda_template.zip
FunctionName: mastertesting_Kinesis2DynamoDB_Datapipeline
Handler: handler.kinesis_to_dynamodb
Role: SOMEROLE
Runtime: python3.6
Type: AWS::Lambda::Function
Run Code Online (Sandbox Code Playgroud)
当我尝试使用CLI部署相同功能时,zip文件路径参数有效。任何的想法?
非常感谢!
我一直在使用下面的代码(现在已经添加到其中)将文件发送到S3。我的lambda代码可以正常工作,但是当我转移诸如MP4之类的较大文件时,我觉得我需要异步/等待。
如何将其完全转换为异步/等待?
exports.handler = async (event, context, callback) => {
...
// Copy data to a variable to enable write to S3 Bucket
var result = response.audioContent;
console.log('Result contents ', result);
// Set S3 bucket details and put MP3 file into S3 bucket from tmp
var s3 = new AWS.S3();
await var params = {
Bucket: 'bucketname',
Key: filename + ".txt",
ACL: 'public-read',
Body: result
};
await s3.putObject(params, function (err, result) {
if (err) console.log('TXT file not sent to S3 …Run Code Online (Sandbox Code Playgroud) aws-lambda ×10
node.js ×5
amazon-s3 ×2
alexa ×1
amazon-sns ×1
async-await ×1
aws-cli ×1
aws-sdk ×1
bash ×1
encryption ×1
javascript ×1
lambda ×1
python ×1
sentry ×1