aws:executeScript – Run a script
Runs the Python or PowerShell script provided using the specified runtime and handler.
Each aws:executeScript action can run up to a maximum duration of 600
seconds (10 minutes). You can limit the timeout by specifying the
timeoutSeconds parameter for an aws:executeScript
step.
Use return statements in your function to add outputs to your output payload. For
examples of defining outputs for your aws:executeScript action, see Example 2: Scripted runbook. You can also send
the output from aws:executeScript actions in your runbooks to the Amazon CloudWatch Logs
log group you specify. For more information, see Logging Automation action output with CloudWatch Logs.
If you want to send output from aws:executeScript actions to CloudWatch Logs, or if
the scripts you specify for aws:executeScript actions call AWS API
operations, an AWS Identity and Access Management (IAM) service role (or assume role) is always required to run
the runbook.
The aws:executeScript action does not support automatic throttling
retry. If your script makes AWS API calls that might be throttled, you must
implement your own retry logic in your script code.
The aws:executeScript action contains the following preinstalled
PowerShell Core modules:
-
Microsoft.PowerShell.Host
-
Microsoft.PowerShell.Management
-
Microsoft.PowerShell.Security
-
Microsoft.PowerShell.Utility
-
PackageManagement
-
PowerShellGet
To use PowerShell Core modules that aren't preinstalled, your script must install the
module with the -Force flag, as shown in the following command. The
AWSPowerShell.NetCore module isn't supported. Replace
ModuleName with the module you want to install.
Install-ModuleModuleName-Force
To use PowerShell Core cmdlets in your script, we recommend using the
AWS.Tools modules, as shown in the following commands. Replace each
example resource placeholder with your own
information.
-
Amazon S3 cmdlets.
Install-Module AWS.Tools.S3 -Force Get-S3Bucket -BucketNameamzn-s3-demo-bucket -
Amazon EC2 cmdlets.
Install-Module AWS.Tools.EC2 -Force Get-EC2InstanceStatus -InstanceIdinstance-id -
Common, or service independent AWS Tools for Windows PowerShell cmdlets.
Install-Module AWS.Tools.Common -Force Get-AWSRegion
If your script initializes new objects in addition to using PowerShell Core cmdlets, you must also import the module as shown in the following command.
Install-Module AWS.Tools.EC2 -Force Import-Module AWS.Tools.EC2 $tag = New-Object Amazon.EC2.Model.Tag $tag.Key = "Tag" $tag.Value = "TagValue" New-EC2Tag -Resourcei-02573cafcfEXAMPLE-Tag $tag
For examples of installing and importing AWS.Tools modules, and using
PowerShell Core cmdlets in runbooks, see Visual design experience for Automation runbooks.
Input
Provide the information required to run your script. Replace each
example resource placeholder with your own
information.
The attachment for a Python script can be a .py file or a .zip file that contains the script. PowerShell scripts must be stored in .zip files.
- Runtime
-
The runtime language to be used for running the provided script.
aws:executeScriptsupports the runtimes in the following table.Runtime name Runtime value for runtimeinputPython 3.12 python3.12Python 3.11 python3.11Python 3.10 python3.10PowerShell 7.4 ( dotnet8)PowerShell 7.4Type: String
Required: Yes
NoteFor python runtimes, the environment provides 512MB of memory and 512MB of disk space. For PowerShell runtimes, the environment provides 1024MB of memory and 512MB of disk space.
- Handler
-
The name of your function. You must make sure the function defined in the handler has two parameters,
eventsandcontext. The PowerShell runtime does not support this parameter.Type: String
Required: Yes (Python) | Not supported (PowerShell)
- InputPayload
-
A JSON or YAML object that will be passed to the first parameter of the handler. This can be used to pass input data to the script.
Type: String
Required: No
- Script
-
An embedded script that you want to run during the automation.
Type: String
Required: No (Python) | Yes (PowerShell)
- Attachment
-
The name of a standalone script file or .zip file that can be invoked by the action. Specify the same value as the
Nameof the document attachment file you specify in theAttachmentsrequest parameter. For more information, see Attachments in the AWS Systems Manager API Reference. If you're providing a script using an attachment, you must also define afilessection 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_nameformat inHandler.NoteThe attachment for a Python script can be a .py file or a .zip file that contains the script. PowerShell scripts must be stored in .zip files.
When including Python libraries in your attachment, we recommend adding an empty
__init__.pyfile in each module directory. This lets you import the modules from the library in your attachment within your script content. For example:from library import moduleType: String
Required: No
- CloudConnector
-
The ID of a Systems Manager Cloud Connector for Microsoft Azure or Google Cloud, such as
a1b2c3d4-5678-90ab-cdef-111122223333. Specify the connector ID, not its ARN. At run time, Automation uses the runbook's assume role (or the credentials of the identity that initiated runbook execution) to assume the federation role specified in the Cloud Connector. The federation role must trust that identity. Automation then uses the federation role to obtain short-lived credentials for the cloud environment specified in the connector. If the credential exchange is successful, environment variables in the script's runtime environment are populated with the credentials and other cloud-specific information. For more information, see Permissions for automations that use a Cloud Connector.Automation sets only the variables for the connector's cloud. For an Azure connector, Automation sets the following variables:
-
AZURE_ACCESS_TOKEN: An access token for Azure Resource Manager. -
AZURE_TENANT_ID: The connector's tenant ID. -
AZURE_CLIENT_ID: The connector's application ID. -
AZURE_SUBSCRIPTION_IDS: The connector's subscription IDs, separated by commas. Set only when the connector specifies subscriptions.
The Azure token works only with Azure Resource Manager (
https://management.azure.com). It doesn't work with Azure data-plane APIs, such as Blob Storage, Key Vault secrets, or Microsoft Graph, so use it with the management libraries (azure.mgmt.*).For a Google Cloud connector, Automation sets the following variables:
-
GCP_ACCESS_TOKEN: An access token for the connector's service account, with thehttps://www.googleapis.com/auth/cloud-platformscope. -
GCP_SERVICE_ACCOUNT_EMAIL: The email address of the connector's service account. -
GCP_WORKLOAD_IDENTITY_POOL_PROVIDER: The connector's workload identity pool provider, in the formprojects/.PROJECT_NUMBER/locations/global/workloadIdentityPools/POOL/providers/PROVIDER -
GCP_ORGANIZATION_ID: The connector's organization ID. Set only when the connector specifies an organization. -
GCP_PROJECT_IDS: The connector's project IDs, separated by commas. Set only when the connector specifies projects.
The tokens are short-lived and can't be refreshed. Don't write them to the script's output or logs. For examples that use these variables, see aws:executeScript Cloud Connector examples.
Type: String
Required: No
-
- Package
-
The name of an Azure or Google Cloud SDK package to add to the script's runtime. The script imports the package's libraries directly, so you don't install them. Without
CloudConnector, the script gets the libraries but no cloud credentials. Use a package for the same cloud asCloudConnector. Automation doesn't check that they match. For the available packages, the libraries in each, and the runtimes that each supports, see Azure and Google Cloud SDK packages. For examples, see Use the Azure and Google Cloud SDKs in a script.Packageis supported only with the automation supported python runtime and does not support PowerShell.Type: String
Required: No
Output
- Payload
-
The JSON representation of the object returned by your function. Up to 100KB is returned. If you output a list, a maximum of 100 items is returned.
Using attachments with aws:executeScript
Attachments provide a powerful way to package and reuse complex scripts, multiple
modules, and external dependencies with your aws:executeScript actions.
Use attachments when you need to:
-
Package multiple Python modules or PowerShell scripts together.
-
Reuse the same script logic across multiple runbooks.
-
Include external libraries or dependencies with your scripts.
-
Keep your runbook definition clean by separating complex script logic.
-
Share script packages across teams or automation workflows.
Attachment structure and packaging
You can attach either single files or zip packages containing multiple files. The structure depends on your use case:
Single file attachments
For simple scripts, you can attach a single .py file
(Python) or a .zip file containing a single PowerShell
script.
Multi-module packages
For complex automation that requires multiple modules, create a zip package with the following recommended structure:
my-automation-package.zip ├── main.py # Entry point script ├── utils/ │ ├── __init__.py # Required for Python module imports │ ├── helper_functions.py # Utility functions │ └── aws_operations.py # AWS-specific operations ├── config/ │ ├── __init__.py │ └── settings.py # Configuration settings └── requirements.txt # Optional: document dependencies
For Python packages, you must include an empty
__init__.py file in each directory that contains
Python modules. This lets you import modules using standard Python
import syntax like from utils import helper_functions.
PowerShell package structure
PowerShell attachments must be packaged in zip files with the following structure:
my-powershell-package.zip ├── Main.ps1 # Entry point script ├── Modules/ │ ├── HelperFunctions.ps1 # Utility functions │ └── AWSOperations.ps1 # AWS-specific operations └── Config/ └── Settings.ps1 # Configuration settings
Creating runbooks with attachments
Follow these steps to create runbooks that use attachments:
-
Upload your attachment to Amazon S3
Upload your script file or zip package to an S3 bucket that your automation role can access. Note the S3 URI for use in the next step.
aws s3 cp my-automation-package.zip s3://my-automation-bucket/scripts/ -
Calculate the attachment checksum
Calculate the SHA-256 checksum of your attachment file for security verification:
# Linux/macOS shasum -a 256 my-automation-package.zip # Windows PowerShell Get-FileHash -Algorithm SHA256 my-automation-package.zip -
Define the files section in your runbook
Add a
filessection at the top level of your runbook to reference your attachment:files: my-automation-package.zip: checksums: sha256: "your-calculated-checksum-here" -
Reference the attachment in your executeScript step
Use the
Attachmentparameter to reference your uploaded file:- name: runMyScript action: aws:executeScript inputs: Runtime: python3.11 Handler: main.process_data Attachment: my-automation-package.zip InputPayload: inputData: "{{InputParameter}}"
aws:executeScript attachment examples
The following examples demonstrate different ways to use attachments with the
aws:executeScript action.
Example 1: Single file attachment
This example shows how to use a single Python file as an attachment to process EC2 instance data.
Attachment file: process_instance.py
Create a Python file with the following content:
import boto3 import json def process_instance_data(events, context): """Process EC2 instance data and return formatted results.""" try: instance_id = events.get('instanceId') if not instance_id: raise ValueError("instanceId is required") ec2 = boto3.client('ec2') # Get instance details response = ec2.describe_instances(InstanceIds=[instance_id]) instance = response['Reservations'][0]['Instances'][0] # Format the response result = { 'instanceId': instance_id, 'instanceType': instance['InstanceType'], 'state': instance['State']['Name'], 'availabilityZone': instance['Placement']['AvailabilityZone'], 'tags': {tag['Key']: tag['Value'] for tag in instance.get('Tags', [])} } print(f"Successfully processed instance {instance_id}") return result except Exception as e: print(f"Error processing instance: {str(e)}") raise
Complete runbook
Here's the complete runbook that uses the single file attachment:
description: Process EC2 instance data using single file attachment schemaVersion: '0.3' assumeRole: '{{AutomationAssumeRole}}' parameters: AutomationAssumeRole: type: String description: (Required) IAM role for automation execution InstanceId: type: String description: (Required) EC2 instance ID to process files: process_instance.py: checksums: sha256: "abc123def456..." mainSteps: - name: processInstance action: aws:executeScript inputs: Runtime: python3.11 Handler: process_instance.process_instance_data Attachment: process_instance.py InputPayload: instanceId: '{{InstanceId}}' outputs: - Type: StringMap Name: InstanceData Selector: $.Payload outputs: - processInstance.InstanceData
Example 2: Multi-module package
This example demonstrates using a zip package containing multiple Python modules for complex S3 bucket operations.
Package structure
Create a zip package with the following structure:
s3-operations.zip ├── main.py ├── utils/ │ ├── __init__.py │ ├── s3_helper.py │ └── validation.py └── config/ ├── __init__.py └── settings.py
main.py (entry point)
The main script that orchestrates the operations:
from utils.s3_helper import S3Operations from utils.validation import validate_bucket_name from config.settings import get_default_settings def cleanup_s3_bucket(events, context): """Clean up S3 bucket based on specified criteria.""" try: bucket_name = events.get('bucketName') max_age_days = events.get('maxAgeDays', 30) # Validate inputs if not validate_bucket_name(bucket_name): raise ValueError(f"Invalid bucket name: {bucket_name}") # Initialize S3 operations s3_ops = S3Operations() settings = get_default_settings() # Perform cleanup deleted_objects = s3_ops.delete_old_objects( bucket_name, max_age_days, settings['dry_run'] ) result = { 'bucketName': bucket_name, 'deletedCount': len(deleted_objects), 'deletedObjects': deleted_objects[:10], # Return first 10 for brevity 'dryRun': settings['dry_run'] } print(f"Cleanup completed for bucket {bucket_name}") return result except Exception as e: print(f"Error during S3 cleanup: {str(e)}") raise
aws:executeScript Cloud Connector examples
The following examples use the environment variables that Automation sets when
you specify CloudConnector. Each example is a complete runbook. For
more examples, see Use the Azure and Google Cloud SDKs in a script.
Example – Azure
This example lists the VMs in each of the connector's Azure
subscriptions. The script builds an Azure credential from
AZURE_ACCESS_TOKEN and gets the subscriptions from
AZURE_SUBSCRIPTION_IDS. Automation doesn't set
AZURE_SUBSCRIPTION_IDS for a connector that covers the whole
tenant.
description: Lists the VMs in each of the connector's Azure subscriptions. schemaVersion: '0.3' assumeRole: '{{ AutomationAssumeRole }}' parameters: AutomationAssumeRole: type: AWS::IAM::Role::Arn description: The role that Automation assumes to run this runbook. CloudConnector: type: AWS::SSM::CloudConnector::Id description: The Cloud Connector for the Azure subscriptions. mainSteps: - name: listVms action: aws:executeScript inputs: Runtime: python3.11 Handler: list_vms CloudConnector: '{{ CloudConnector }}' Package: azure_202610 Script: |- import os import time from types import SimpleNamespace from azure.core.credentials import AccessToken from azure.mgmt.compute import ComputeManagementClient def list_vms(events, context): # The token can't be refreshed, so get_token returns the same token every time. token = AccessToken(os.environ["AZURE_ACCESS_TOKEN"], int(time.time()) + 600) credential = SimpleNamespace(get_token=lambda *scopes, **kwargs: token) vms = {} for subscription_id in os.environ["AZURE_SUBSCRIPTION_IDS"].split(","): with ComputeManagementClient(credential, subscription_id) as client: vms[subscription_id] = [vm.name for vm in client.virtual_machines.list_all()] return vms outputs: - Name: Vms Selector: $.Payload Type: StringMap
Example – Google Cloud
This example lists the VM instances in each of the connector's
Google Cloud projects. The script builds a credential from
GCP_ACCESS_TOKEN and gets the projects from
GCP_PROJECT_IDS. Automation sets
GCP_PROJECT_IDS only when the connector specifies
projects.
description: Lists the VM instances in each of the connector's Google Cloud projects. schemaVersion: '0.3' assumeRole: '{{ AutomationAssumeRole }}' parameters: AutomationAssumeRole: type: AWS::IAM::Role::Arn description: The role that Automation assumes to run this runbook. CloudConnector: type: AWS::SSM::CloudConnector::Id description: The Cloud Connector for the Google Cloud projects. mainSteps: - name: listInstances action: aws:executeScript inputs: Runtime: python3.11 Handler: list_instances CloudConnector: '{{ CloudConnector }}' Package: gcp_202610 Script: |- import os from google.cloud import compute_v1 from google.oauth2.credentials import Credentials def list_instances(events, context): # The token can't be refreshed. Credentials without an expiry time don't try to refresh it. credentials = Credentials(token=os.environ["GCP_ACCESS_TOKEN"]) instances = {} with compute_v1.InstancesClient(credentials=credentials) as client: for project in os.environ["GCP_PROJECT_IDS"].split(","): names = [] for _, scoped_list in client.aggregated_list(project=project): names.extend(instance.name for instance in scoped_list.instances) instances[project] = names return instances outputs: - Name: Instances Selector: $.Payload Type: StringMap
Troubleshooting aws:executeScript attachments
Use the following guidance to resolve common issues with
aws:executeScript attachments:
Module import errors
If you receive import errors when using multi-module packages:
-
Make sure you have included an empty
__init__.pyfile in each directory containing Python modules. -
Verify that your import statements match the actual file and directory structure in your zip package.
-
Use relative imports (e.g.,
from .utils import helper) or absolute imports (e.g.,from utils import helper) consistently.
Attachment not found errors
If your automation fails to find the attachment:
-
Verify that the
Attachmentparameter value exactly matches the key in yourfilessection. -
Check that your S3 bucket path and file name are correct in the
filessection. -
Make sure your automation role has
s3:GetObjectpermission for the attachment S3 location. -
Verify that the checksum in your runbook matches the actual file checksum.
Handler function errors
If you receive handler-related errors:
-
For Python: Use the format
filename.function_namein theHandlerparameter (e.g.,main.process_data). -
Make sure your handler function accepts exactly two parameters:
eventsandcontext. -
For PowerShell: Do not specify a
Handlerparameter; the script runs directly.
Script execution failures
If your script fails during execution:
-
Check the automation execution history for detailed error messages and stack traces.
-
Use
print()statements (Python) orWrite-Information(PowerShell) to add debugging output. -
Verify that all required AWS permissions are granted to your automation role.
-
Test your script logic locally before packaging it as an attachment.
Exit codes and error handling
To properly handle errors and return exit codes:
-
In Python: Use
raise Exception("error message")to indicate script failure. -
In PowerShell: Use
throw "error message"orWrite-Errorto indicate failure. -
Return structured data from your functions to provide detailed success/failure information.
-
Use try-catch blocks to handle exceptions gracefully and provide meaningful error messages.