【发布时间】:2022-11-09 14:43:21
【问题描述】:
我在 CloudFormation 堆栈中创建了一个步进函数,一旦部署,我需要从堆栈中的另一个函数调用该函数。我不知道该怎么做,而且我得到了循环依赖。
基本上我正在尝试将环境变量传递给作为 step 函数的 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']
【问题讨论】:
-
通过使用全局变量,所有参数都将应用于所有函数,甚至是 Research。您需要从全局变量中删除 env 变量,然后将其放入将触发研究功能的函数中
-
它被添加到所有函数的事实不是问题,我遇到的问题是如何在没有循环依赖的情况下将步进函数 ARN 发送到函数。知道怎么做吗?
-
@AnthonyB。您的解决方案确实有效。我尝试了它并删除了全局变量并将其替换为具有 DependsOn 依赖项的局部变量,就像一个魅力!
标签: aws-lambda amazon-cloudformation aws-step-functions