AWS SAM 调用状态机

问题描述

我正在使用 AWS Step Functions 构建数据管道。我使用 VS Code 和 AWS 工具包在本地运行和调试它,然后再将它部署到我的 QA 和 PROD 环境。状态机有两个任务,第一个是在新文件上传到我的 S3 时发送 SNS 通知,第二个是将此文件从文本格式转换为镶木地板格式。此事件将由 S3 put 事件触发。因此,工作流程是有人将此文件上传到 S3 存储桶,由 cloudtrail 捕获它,然后事件桥触发状态机。

我的问题是,当我使用 SAM 通过我的事件调用命令时,它调用的是 Lambda 函数而不是我的状态机。

我构建了下面的 template.yaml

AWstemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  notification_lambda

  Sample SAM Template for notification_lambda

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3

Resources:

  SmrtrNewFileNotification:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      FunctionName: NewFile_LF
      CodeUri: new_file_notification/
      Handler: lambda_function.lambda_handler
      Runtime: python3.8
      Layers:
        - !Ref PackageLayer
        - !Ref YAMLLayer     

  TenPackageLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      CompatibleRuntimes: 
        - python3.6
        - python3.7
        - python3.8  
      ContentUri: ../../../../path/path/package.zip   
      Description: sb-package
      LayerName: packagelayer
      LicenseInfo: MIT   
      
  YAMLLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      CompatibleRuntimes: 
        - python3.6
        - python3.7
        - python3.8  
      ContentUri: ../../../../path/yaml/yaml.zip   
      Description: sb-yaml
      LayerName: yamllayer
      LicenseInfo: MIT  

  SmrtrStatesExecutionRole:
    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:
        - PolicyName: StatesExecutionPolicy
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action:
                  - "lambda:InvokeFunction"
                Resource: "*"      

  SmrtrDataPipelinestateMachine:
    Type: AWS::Serverless::StateMachine
    Properties:
      Name: "DataPipeline"
      Role: !GetAtt [ SmrtrStatesExecutionRole,Arn ]
      Type: STANDARD        
      DeFinitionUri: ./path/smrtr_datapipeline.asl.yaml
      Events:
        SmrtrS3PutEvent:
          - Type: AWS::Serverless::EventBridgeRule      
            Properties: 
              Pattern: {
                        "source": [
                          "aws.s3"
                        ],"detail-type": [
                          "AWS API Call via CloudTrail"
                        ],"detail": {
                          "eventSource": [
                            "s3.amazonaws.com"
                          ],"eventName": [
                            "copyObject","CompleteMultipartUpload","PutObject"
                          ],"requestParameters": {
                            "bucketName": [
                              "sb-bucket"
                            ],"key": [
                              "staging/file_dataset.txt.gz"
                            ]
                          }
                        }
                      }   

然后我运行这两个命令:

sam build -t ./path/template.yaml
sam local invoke --event ./path/event.json --profile sb-user --debug

然而,这个 invoke 命令正在调用我的 Lambda 函数并没有触发状态机。是否可以使用 SAM 来实现,是否有其他方法可以在部署之前在本地构建它?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)