1 Answer
- Newest
- Most votes
- Most comments
1
To execute a script from S3 in an SSM Runbook, you need to use the aws:executeScript action with the appropriate configuration. Here's how you can modify your runbook to execute a script stored in S3:
-
First, upload your Python script (test.py) to an S3 bucket.
-
Modify your SSM Runbook YAML as follows:
description: A simple SSM runbook that calls a script from S3 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: ExecuteS3Script action: aws:executeScript isEnd: true inputs: Runtime: python3.10 Handler: main InputPayload: Message: '{{ Message }}' Script: | import boto3 import json s3 = boto3.client('s3') def main(events, context): # Download the script from S3 bucket = 'your-s3-bucket-name' key = 'path/to/test.py' response = s3.get_object(Bucket=bucket, Key=key) script_content = response['Body'].read().decode('utf-8') # Execute the downloaded script exec(script_content, globals()) # Call the process_message function from the downloaded script return process_message(events) The key changes in this configuration are: 1. The Handler is set to 'main', which refers to the main function in the inline script. 2. The Script section now contains an inline Python script that downloads your actual script from S3 and executes it. 3. Remove the 'files' section from your runbook as it's no longer needed. Make sure to replace 'your-s3-bucket-name' and 'path/to/test.py' with the actual S3 bucket name and the path to your script in S3. This approach allows you to keep your main script logic in S3 while using a small inline script in the runbook to fetch and execute it. The inline script downloads your Python file from S3, executes it in the global namespace, and then calls the process_message function from your original script. Remember to ensure that the IAM role associated with your SSM execution has permissions to read from the S3 bucket where your script is stored. This method should resolve the "Handler main is not found" error you were encountering, as the handler 'main' now refers to the function in the inline script, which in turn executes your S3-stored script. **Sources** [Using scripts in runbooks - AWS Systems Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/automation-document-script-considerations.html) [Example 2: Scripted runbook - AWS Systems Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/automation-authoring-runbooks-scripted-example.html)
Relevant content
asked 4 years ago

This is a "way around" the problem. Docs say that you should be able to execute a script directly by including it as an attachment:
https://docs.aws.amazon.com/systems-manager/latest/userguide/automation-action-executeScript.html
"If you're providing a script using an attachment, you must also define a files section in the top-level elements of your runbook. For more information, see Schema version 0.3.
To invoke a file for Python, use the filename.method_name format in Handler."
I did extrapolate on this answer and come up with "a solve", but I still want to be able to just execute a script by attaching like the docs say I should:
https://publish.obsidian.md/thought-realm/Areas/How+To+Do+Things/How+to+Run+a+Python+Script+from+S3+Using+AWS+SSM+Automation+Runbooks
If anyone has a runbook example of a script that executes based on an attachement, I would love to see it.