Execute AWS CLI Command
Overview
The Execute AWS CLI Command action allows users to run any AWS CLI command directly from their bot workflow. This feature supports a wide range of AWS operations, providing flexibility in managing AWS resources efficiently. Users can either enter an AWS CLI command manually or use an AI prompt to generate the appropriate command based on specific requirements.
Prerequisites
Linux Agent
- Agent Configuration:
Ensure the Linux Agent is configured. Refer to the Agent Installation Guide. - The AWS CLI must be installed and configured on the agent, with credentials that have the appropriate permissions for the intended AWS operations.
- Refer to the AWS CLI installation guide.
How to Use This Action?
To use the Execute AWS CLI Command action, follow these steps:
-
In your bot workflow, navigate to the Linux Actions and select Library.
-
Search for or locate the Execute AWS CLI Command action and drag it into your workflow.
-
Select the Linux integration that is connected to your AWS CLI-configured agent.
-
Under the Parameters section, specify the required parameter values according to your requirements. For more details, refer to the Parameter Details section.
-
In the
aws_command
field, you can either:-
Manually enter the AWS CLI command you want to execute.
-
Use AI to generate the command by clicking on the "Generate with AI" button. For example, enter a prompt like "describe all instances in the us-east-1 region."
-
-
Save or update the bot, then click on the Run button to execute the bot or the Run button inside the action node. Wait for the execution to complete. After execution, view the results in the execution details.
Parameter Details
Parameter | Required | Description |
---|---|---|
aws_command | Yes | The AWS CLI command to execute. |
script_mode | Yes | Specifies whether the AWS CLI command is executed as a script (combination of multiple commands) or as individual commands. Set to True for script execution, or False for individual command execution. Defaults to False. NOTE: Check format limitations in Limitations. |
use_aws_cli_config | Conditional | Indicates whether to use the AWS CLI configuration directly (defaults to True). This method is familiar and quick to set up. Ensure the AWS CLI is installed and configured using aws configure . For advanced options, use aws_config_profile . |
run_as_user | Yes | Specifies the user under which the command runs (defaults to "ubuntu."). Update the username if using a different cloud provider or user. Ensure AWS CLI is configured for this user. |
aws_config_profile | Conditional | The profile from which to load AWS credentials. To use this instead of use_aws_cli_config , add the necessary credentials to the agent configuration file, including AWS_ACCESS_KEY_ID , AWS_SECRET_ACCESS_KEY , and AWS_DEFAULT_REGION . For more information on the agent configuration file, refer to the Agent Installation Guide. |
execution_timeout | Yes | The maximum time (in seconds) allowed for command execution, Defaults to 300 seconds. |
Using Handlebar
You can use the Handlebar capabilities to reference values dynamically from the output of a previous node. This enables the creation of flexible and reusable commands within your workflow.
Referencing Particular Values
For example, to stop one or more EC2 instances, you can utilize the following AWS CLI command:
aws ec2 stop-instances --region us-east-1 --instance-ids <instance-ids>
If the previous node returns a list of instance IDs under the instance_id key, you can reference those values as follows:
aws ec2 stop-instances --region us-east-1 --instance-ids {{$.nodes.list_ec2_instances[0].instance_id}} {{$.nodes.list_ec2_instances[1].instance_id}}
Note: To reference values from previous nodes, type $$
to view and select the desired data dynamically.
Explanation
In this example, the instance_id
values are dynamically retrieved from the list of EC2 instances returned by the list_ec2_instances
node. The values are referenced using Handlebar syntax, with indices 0
and 1
corresponding to the first and second instance IDs in the list, respectively.
The generated command will dynamically substitute the instance-id
values, resulting in:
aws ec2 stop-instances --region us-east-1 --instance-ids i-1234567890abcdef0 i-0987654321abcdef0
Limitations
- Outputs: Each action executes commands with the default output format set to JSON.
-
Script mode: Not recommended if you intend to use data from the current CLI node as input for the next node in the workflow.
-
Non-script mode: The output may not always be in JSON format. This depends on AWS CLI behavior, as certain commands do not produce JSON outputs.
-
Example Use Case
Scenario: Stopping All Running EC2 Instances Tagged with Environment=dev
As a cloud administrator, you need to efficiently manage EC2 instances by stopping all instances tagged with Environment=dev
in the us-east-1
region. This command helps streamline resource management and optimize costs by stopping instances no longer needed.
Command to Stop All Running EC2 Instances with a Specific Tag:
Use the following command to locate and stop all running EC2 instances with the Environment=dev
tag in the specified region:
aws ec2 describe-instances \ --region us-east-1 \ --filters "Name=tag:Environment,Values=dev" "Name=instance-state-name,Values=running" \ --query "Reservations[].Instances[].InstanceId" \ --output text | xargs -I {} aws ec2 stop-instances --region us-east-1 --instance-ids {}