gpt4 book ai didi

amazon-web-services - 两个 aws cloudformation 堆栈可以共享一个 AWS::Serverless::Function 吗?

转载 作者:行者123 更新时间:2023-12-03 07:36:24 24 4
gpt4 key购买 nike

这是我的 SAM 模板,它有一个名为 CompanyParameter 的参数,因此当我部署到堆栈时,我将传递一个公司名称,它将为每个公司创建一个队列,但我想要不同公司的所有堆栈点到相同的 lambda 函数。

现在,当我创建第二个堆栈时,它将失败并显示消息:

batch-sqs-queue-process already exists in stack.....

这可能吗?我想这样做的原因是,如果更改了 lambda 函数的某些代码,我只需要为所有公司更新一次。这是正确的方向吗?

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Processing batch system messages on an SQS queue with Lambda
Parameters:
CompanyParameter:
Type: String
Description: Company name
AllowedPattern: "[A-Za-z0-9]+"
ConstraintDescription: Company name must only contain uppercase and lowercase letters and numbers
Resources:
BatchSQSQueueFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./index.js
Description: process the batch data messages
FunctionName: batch-sqs-queue-process
Handler: index.handler
Runtime: nodejs8.10
Events:
BatchSQSEvent:
Type: SQS
Properties:
Queue: !GetAtt BatchSqsQueue.Arn
BatchSize: 10

BatchSqsQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: !Sub batch-sqs-queue-${CompanyParameter}

最佳答案

当前的实现无法按照您想要的方式工作,您部署的每个 CloudFormation 模板都将创建自己的 SQS 队列和自己的 Lambda 函数。如果 Lambda 源代码发生更改,您必须为所有公司重新部署堆栈。另一种方法是创建 AWS Lambda Layer并将所有共享代码放入层中,然后使用每个相应的 SQS 配置 Lambda 以使用该层。这将解决代码重用问题,但有一个警告。复制自documentation :

You choose a specific version of a layer to use. If you want to use a different version later, update your function's configuration.

换句话说,代码在 Lambda 资源之间重复使用,但您仍然必须使用 Lambda 层的新版本号重新部署所有函数。


另一种选择可能是实现 CloudFormation Macro 。看看Count例如,但想象一下用您的公司数组而不是数字来为其提供信息。免责声明,我尚未尝试将宏与 AWS SAM 一起使用。


旁注,您面临的问题 batch-sqs-queue-process 已存在于堆栈中.... 是因为您已将 FunctionName 指定为您的 SAM 模板。 Lambda 函数名称在所有堆栈中必须是唯一的,请阅读有关 CloudFormation 的更多信息 Name Type在用户指南中。通过删除函数名称,CloudFormation 将生成一个唯一的名称(通常基于堆栈名称、资源名称和一些随机字符)。

关于amazon-web-services - 两个 aws cloudformation 堆栈可以共享一个 AWS::Serverless::Function 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55815702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com