amazon-web-services - 如何在“serverless.yml”中引用 lambda 版本?
问题描述
我正在使用serverless
将 lambda 部署到 AWS。我有一个案例,它lambda
在部分中定义,functions
但配置并发配置是resources
因为我需要使用Condition
. 我遇到的问题是我如何引用已发布的 lambda 版本resources
?
functions:
getTransactionsHandler:
...
resources:
Conditions:
CommonPCNotZero: !Not [!Equals [0, '${self:custom.commonPC}']]
Resources:
!If
- CommonPCNotZero
- getTransactionsHandler:
Type: AWS::Lambda::Alias
Properties:
FunctionName: !Ref GetTransactionsHandlerLambdaFunction
FunctionVersion: HOW CAN I GET THE VERSION?
ProvisionedConcurrencyConfig:
ProvisionedConcurrentExecutions: '${self:custom.commonPC}'
- !Ref AWS::NoValue
解决方案
在 serverless.yml 文件中放置一个占位符作为参考。
[无服务器脚本插件] LambdaFunctionARN:THIS_IS_SET_IN_SCRIPT
可编写脚本的插件使用钩子
custom:
scriptHooks:
before:aws:package:finalize:saveServiceState: scripts/completeLambdaAssociation.js
脚本 completeLambdaAssociation.js 通过搜索 AWS::Lambda::Version 资源来更新 LambdaFunctionARN。
// serverless injected by serverless-scriptable-plugin
// noinspection
JSUnresolvedVariable const sls = serverless;
// noinspection JSUnresolvedVariable
const resources = sls.service.provider.compiledCloudFormationTemplate.Resources;
const resourceType = 'AWS::Lambda::Version'; const prefix = 'RewriteUriLambdaVersion';
const resourceNames = Object.keys(resources).filter(name => resources[name].Type === resourceType && name.startsWith(prefix))
if (resourceNames.length !== 1) {
throw Error(`Must have exactly 1 resource of type ${resourceType} and prefix ${prefix}, found
${resourceNames}`);
}
const distConfig = resources['CFDistribution']['Properties']['DistributionConfig']; distConfig['DefaultCacheBehavior']['LambdaFunctionAssociations'][0]['LambdaFunctionARN'] = { Ref: resourceNames[0]};
console.log(`[${__filename}] Updated LambdaFunctionARN on first LambdaFunctionAssociation on DefaultCacheBehavior`);
推荐阅读
- php - WordPress:get_post_status 需要很长时间
- sql - 我的关于 SQL Pivot 函数的脚本有什么错误?
- vba - 使用子文件夹名称自动分类电子邮件
- google-sheets-api - 使用 gspread 对谷歌电子表格的读取请求出现错误 500
- rust - 是 Vec 中的数据吗
总是密密麻麻? - javascript - 尝试替换构造函数变量时出错
- java - 从kafka主题读取时如何验证无效的分区名称
- php - Twig - 带有 3 个操作数的“for”语法,这是什么意思
- android - 有什么方法可以检测 Android TV Leanback 中键盘的可见性
- ios - WKWebView canGoForward 总是返回 false swift 5