gpt4 book ai didi

aws-lambda - 如何将 Step 函数的 Arn 传递给 CloudFormation 堆栈中的另一个函数?

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

我在 CloudFormation 堆栈中创建了一个步骤函数,部署后我需要从堆栈中的另一个函数调用该函数。我不知道该怎么做,而且我遇到了循环依赖。

基本上,我尝试将环境变量传递给作为步骤函数的 ARN 的函数。

这是 CloudFormation 代码:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
reports stack

Globals:
Function:
Timeout: 120
MemorySize: 2048
Runtime: python3.9
Environment:
Variables:
STEP_FUNCTION: !GetAtt Research.Arn ### =====> how do i do that ?

Parameters:

ProjectName:
Description: Name of the Project
Type: String
Default: Project

Resources:

MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
TracingEnabled: true
Cors:
AllowMethods: "'DELETE,GET,HEAD,OPTIONS,POST,PUT'"
AllowHeaders: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
AllowOrigin: "'*'"


List:
DependsOn: VideoResearch
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/
Handler: get.get
Events:
List:
Type: Api
Properties:
RestApiId: !Ref MyApi
Path: /reports
Method: GET
Policies:
(...)


################ STEP FUNCTION ################

Research:
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/
Handler: runResearch.research
Policies:
(...)


StepFunctionToHandleResearchRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- !Sub states.${AWS::Region}.amazonaws.com
Action: "sts:AssumeRole"
Path: "/"
Policies:
(...)


#
StepFunctionToHandleVideosResearch:
Type: "AWS::StepFunctions::StateMachine"
Properties:
DefinitionString: !Sub |
{
"StartAt": "Research",
"States": {
"Research": {
"Type": "Task",
"Resource": "${VideoResearch.Arn}",
"End": true
}
}
}
RoleArn: !GetAtt [ StepFunctionToHandleResearchRole, Arn ]


Outputs:

(...)

# I also tried to export the arn of the function

在我的函数代码中,我有:

stepFunctionARN = os.environ['STEP_FUNCTION']

最佳答案

@Anthony B. 在评论中发布了答案:问题与我将依赖项添加为全局变量这一事实有关,它创建了一个循环依赖项(API=> 本地函数 <= API 查找函数的 ARN => 不不起作用)。

如果您删除全局变量并添加局部变量,并使用“DependsOn:Research”,则一切正常。

API=>created => 创建第一个函数,获取 Arn => 创建其他函数并提供 ARN

关于aws-lambda - 如何将 Step 函数的 Arn 传递给 CloudFormation 堆栈中的另一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74360290/

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