我意识到它很新,但我没有看到任何语言的示例如何为使用 AWS CDK 创建的 lambda 指定角色。
我试图这样做
const cdk = require('@aws-cdk/cdk');
const lambda = require('@aws-cdk/aws-lambda');
const iam = require('@aws-cdk/aws-iam');
const path = require('path');
class MyStack extends cdk.Stack {
constructor (parent, id, props) {
super(parent, id, props);
//
// Create a lambda...
const fn = new lambda.Function(this, 'MyFunction-cdktest', {
runtime: lambda.Runtime.NodeJS810,
handler: 'index.handler',
code: lambda.Code.directory( path.join( __dirname, 'lambda')),
role: iam.RoleName('lambda_basic_execution')
});
}
}
class MyApp extends cdk.App {
constructor (argv) {
super(argv);
new MyStack(this, 'hello-cdk');
}
}
console.log(new MyApp(process.argv).run());
Run Code Online (Sandbox Code Playgroud)
为了尝试为函数指定现有的 IAM …
使用无服务器,我们可以process.env通过创建这样的配置文件条目来添加变量:
environment:
STRIPE_SECRET_KEY: ${self:custom.secrets.stripeSecretKey} # Stripe secret API key
Run Code Online (Sandbox Code Playgroud)
我们可以像这样在 lambda 函数中访问它:
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
Run Code Online (Sandbox Code Playgroud)
我们如何使用 AWS Amplify 做到这一点?
amazon-web-services node.js aws-lambda aws-sdk-js aws-amplify
我正在尝试在 AWS 中使用 Secrets Manager 一个 Lambda 函数。管理器用于将数据库凭据存储到 Snowflake(用户名、密码)的机密。
我设法在 Secrets Manager 中设置了一个秘密,其中包含几个键/值对(例如一个用于用户名,另一个用于密码)。
现在我试图在我的 Python 函数代码中引用这些值。AWS 文档友好地提供了以下代码段:
import boto3
import base64
from botocore.exceptions import ClientError
def get_secret():
secret_name = "MY/SECRET/NAME"
region_name = "us-west-2"
# Create a Secrets Manager client
session = boto3.session.Session()
client = session.client(
service_name='secretsmanager',
region_name=region_name
)
# In this sample we only handle the specific exceptions for the 'GetSecretValue' API.
# See https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
# We rethrow the exception by default.
try:
get_secret_value_response = client.get_secret_value(
SecretId=secret_name
)
except ClientError …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用图像裁剪器依赖项作为 aws lambda 上传函数。它在我的本地机器()上完美运行node v14.17.3,但在将其作为 lambda fn 触发时失败并出现以下错误。
"errorType":
"Error"
"errorMessage":
"\nSomething went wrong installing the \"sharp\" module\n\nCannot find module '../build/Release/sharp-linux-x64.node'\nRequire stack:\n- /var/task/node_modules/sharp/lib/sharp.js\n- /var/task/node_modules/sharp/lib/constructor.js\n- /var/task/node_modules/sharp/lib/index.js\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js\n\nPossible solutions:\n- Install with the --verbose flag and look for errors: \"npm install --ignore-scripts=false --verbose sharp\"\n- Install for the current runtime: \"npm install --platform=linux --arch=x64 sharp\"\n- Consult the installation documentation: https://sharp.pixelplumbing.com/install"
"stack":
"Error: "
"Something went wrong installing the \"sharp\" module"
""
"Cannot find module '../build/Release/sharp-linux-x64.node'"
"Require stack:"
"- /var/task/node_modules/sharp/lib/sharp.js"
"- …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 JavaScript 模块执行以下代码。我知道 NodeJS 默认是 CommonJS。我的代码在本地工作,但是当我想将其作为 lambda 中的模块运行时,我遇到了以下问题:
错误:
{
"errorType": "ReferenceError",
"errorMessage": "exports is not defined in ES module scope\nThis file is being treated as an ES module because it has a '.js' file extension and '/var/task/package.json' contains \"type\": \"module\". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.",
"trace": [
"ReferenceError: exports is not defined in ES module scope",
"This file is being treated as an ES module because it has a …Run Code Online (Sandbox Code Playgroud) 所以我有一个在 Lambda 中运行的无服务器 Express 应用程序。一个请求(响应大小约为 800KB)不断返回LAMBDA_RUNTIME Failed to post handler success response. Http response code: 413.错误。我认为这可能是由于某些内部逻辑超时,并添加了日志,并且所有获取和处理最多需要 6 秒,但 lamdba 仍然返回此错误。
这些是响应标头
x-amz-cf-pop: YTO50-C3
x-amzn-errortype: InternalServerErrorException
x-amzn-requestid: f291230-342-4324-324-cb7df188944c
x-cache: Error from cloudfront
Run Code Online (Sandbox Code Playgroud)
响应大小绝对不会太大,我返回带有正确数据的响应,日志中没有抛出任何错误。知道为什么会发生这种情况吗?另外关于如何调试这个问题有什么建议吗?当然,一切都可以在本地运行,但是有没有办法让我调试实际的 lambda?我添加的日志表明整个过程已完成,但不知何故返回了错误。
更新了我的 serverless.yml 配置
service: my-service
variablesResolutionMode: 20210326
useDotenv: true
custom:
serverless-offline:
useChildProcesses: true
webpack:
webpackConfig: ./webpack.config.js
packager: "yarn"
includeModules: true
prune:
automatic: true
includeLayers: true
number: 3
customDomain:
domainName: "abc.com"
basePath: "val"
stage: ${someval}
createRoute53Record: true
plugins:
- serverless-domain-manager
- serverless-webpack
- serverless-prune-plugin
- serverless-webpack-prisma …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过 CDK 部署在打字稿中实现的 lambda。
我能够成功部署 lambda,但是当我测试它时,出现以下错误:
{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'index'\nRequire stack:\n- /var/runtime/index.mjs",
"trace": [
"Runtime.ImportModuleError: Error: Cannot find module 'index'",
"Require stack:",
"- /var/runtime/index.mjs",
" at _loadUserApp (file:///var/runtime/index.mjs:996:17)",
" at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1031:21)",
" at async start (file:///var/runtime/index.mjs:1194:23)",
" at async file:///var/runtime/index.mjs:1200:1"
]
}
Run Code Online (Sandbox Code Playgroud)
lambda 在 CDK 中声明如下:
const postImages = new lambda.Function(scope, utils.prefixed('post_images'), {
runtime: lambda.Runtime.NODEJS_18_X,
handler: 'index.lambdaHandler',
code: lambda.Code.fromAsset('./sd_service/post_images')
})
Run Code Online (Sandbox Code Playgroud)
的内容./sd_service/post_images看起来像这样:
import { Context, APIGatewayEvent, APIGatewayProxyResult } from 'aws-lambda';
export …Run Code Online (Sandbox Code Playgroud) 我花了整整一周的时间尝试这个,所以这是一个冰雹玛丽.
我试图将Tesseract OCR打包到运行在Python上的AWS Lambda(我也使用PILLOW进行图像预处理,因此选择了Python).
我理解如何使用virtualenv将Python软件包部署到AWS,但是我似乎无法找到将实际的Tesseract OCR部署到环境中的方法(例如/ env /)
pip install py-tesseract在Python包装成/ ENV /的成功部署的结果,但是这依赖于一个单独的(本地)安装正方体的pip install tesseract-ocr在我错误输出之前做的事情让我只有一定距离,我假设是由于缺少leptonica依赖.但是,我不知道如何将leptonica打包成/ env /(如果可能的话)Run Code Online (Sandbox Code Playgroud)tesseract_ocr.cpp:264:10: fatal error: 'leptonica/allheaders.h' file not found #include "leptonica/allheaders.h"
Run Code Online (Sandbox Code Playgroud)Processing dependencies for python-tesseract==0.9.1 Searching for python-tesseract==0.9.1 Reading https://pypi.python.org/simple/python-tesseract/ Couldn't find index page for 'python-tesseract' (maybe misspelled?) Scanning index of all packages (this may take a while) Reading https://pypi.python.org/simple/ No local packages or download links found for python-tesseract==0.9.1
任何指针都将非常感激.
我正在测试aws lambda,使用4.3版本的nodejs.我能够在控制台测试中成功完成处理函数中的所有语句,包括连接到我们的vpc中的mongodb主机.但是,该功能总是超时.我发现了几个讨论使用回调,设置上下文属性和IAM角色权限的帖子和资源,但无论我做什么,它总是会超时.当前代码:
'use strict';
var Mongoose = require('mongoose');
var Device = require('./device_model');
var Alarm = require('./alarm_model');
var Event = require('./event_model');
var mongoConnection = process.env.MONGO_URL;
var connection = Mongoose.connect(mongoConnection);
Mongoose.connection.once('open', function() {
console.log("Connecting to mongo at: " + mongoConnection);
console.log("Mongoose connection in lambda opened");
});
Mongoose.connection.on('error', function(){
console.error("Error creating mongoose connection in lambda, exiting!");
process.exit(1);
});
exports.check_alarms = function(event, context, callback) {
context.callbackWaitsForEmtpyEventLoop = false;
console.log("The incoming event: " + JSON.stringify(event));
var device = null;
Device.findByUUID(event.uuid, function(error, result){
if(!error){
device = …Run Code Online (Sandbox Code Playgroud) 何时释放lambda中的连接和清理资源.在普通的Node JS应用程序中,我们使用钩子
process.on('exit', (code) => {
console.log(`About to exit with code: ${code}`);
});
Run Code Online (Sandbox Code Playgroud)
但是,这不适用于AWS Lambda.导致睡眠模式下的Mysql连接.我们没有足够的资源用于此类活动连接.AWS文档均未指定实现此目的的方法.
如何接收AWS Lambda容器的停止事件?
aws-lambda ×10
node.js ×5
aws-cdk ×2
python ×2
aws-amplify ×1
aws-sdk-js ×1
containers ×1
express ×1
graphql ×1
node-modules ×1
serverless ×1
tesseract ×1
virtualenv ×1