【问题标题】:How to pass the Arn of a Step function to another function in a CloudFormation stack?如何将 Step 函数的 Arn 传递给 CloudFormation 堆栈中的另一个函数?
【发布时间】: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


【解决方案1】:

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

如果您删除全局变量并添加一个局部变量,并带有“DependsOn:Research”,那么一切正常。

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

【讨论】:

    猜你喜欢
    • 2019-03-15
    • 2021-12-30
    • 2021-03-27
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 2019-07-02
    • 2015-02-21
    • 2011-10-31
    相关资源
    最近更新 更多