You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS SSM自动化运行手册使用executeScript执行附加Python脚本时出现'module has no attribute main'错误的解决求助

AWS SSM自动化运行手册使用executeScript执行附加Python脚本时出现'module has no attribute main'错误的解决求助

我已经把SSM相关的官方文档翻来覆去啃了好几遍,可还是搞不懂怎么把Python脚本抽离出来(不管是存在S3还是作为运行手册附件),而非硬编码内嵌到我的运行手册里。

我超喜欢SSM运行手册的功能——它能根据参数和分支路径在部署过程中灵活执行逻辑,但现在我真的迫切想要把脚本部分抽象出来。内嵌脚本的执行我已经完全搞定了,可每次尝试执行附加的脚本都会触发这个错误:

Failure message
Step fails when it is Poll action status for completion. Traceback (most recent call last): AttributeError: module 'test' has no attribute 'main' Handler main is not found in provided script. Please refer to Automation Service Troubleshooting Guide for more diagnosis details.

以下是我正在尝试的运行手册代码:

description: A simple SSM runbook that calls a templated script to print and output a message.
schemaVersion: '0.3'
parameters:
  Message:
    type: String
    description: The message to print and output.
    default: "Hello from the runbook!"
mainSteps:
  - name: ExecuteTemplateScript
    action: aws:executeScript
    isEnd: true
    inputs:
      Runtime: python3.10
      Handler: test.main  # [file].[function] format
      InputPayload:
        Message: '{{ Message }}'
      Script: ''
      Attachment: test.py  # Name of the attached file
    outputs:
      - Name: OutputMessage
        Selector: $.Payload.OutputMessage
        Type: String
files:
  test.py:
    checksums:
      sha256: 590708757b79b9438bf299ee496a121c98cf865899db8fea5d788d0cb616d1f5

我已经试过好几种不同的Handler写法:

  • handler: test.py.main
  • handler: test
  • handler: test.main
  • handler: main

我的测试脚本test.py代码如下:

#!/usr/bin/env python3
"""Simple templated script for SSM that prints and outputs a message."""

import json

def process_message(payload: dict) -> dict:
    """Process the input message and return it."""
    message = payload.get('Message', 'No message provided')
    print(f"Message received: {message}")  # Printed to SSM logs
    return {'OutputMessage': message}

def main(events, context):
    """Main function for SSM execution."""
    # SSM passes InputPayload as 'events'
    payload = events
    result = process_message(payload)
    return result  # SSM captures this as output

if __name__ == "__main__":
    # For local testing, simulate SSM input
    import sys
    if not sys.stdin.isatty():
        payload = json.load(sys.stdin)
    else:
        payload = {'Message': 'Hello, world!'}
    result = process_message(payload)
    print(json.dumps(result))

我已经仔细研读了AWS官方的SSM相关文档,包括自动化脚本执行、运行手册编写、附件使用等多方面内容,而且脚本已经正确附加到运行手册中,SHA256校验和也完全匹配。实在走投无路了,来求助各位大佬,麻烦帮我看看问题出在哪!

备注:内容来源于stack exchange,提问作者Micah Longmire

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 11:04:31