Using CLI commands with AWS Chatbot - Common use cases - AWS Chatbot

Using CLI commands with AWS Chatbot - Common use cases

Common use cases for using AWS Chatbot in Slack involve running CLI commands. This topic also includes an example use case for invoking a Lambda function in Slack, written in Python 3.8.

For more information about running CLI commands in Slack see Running AWS CLI commands from Slack channels.

For a tutorial that walks you through how to invoke Lambda functions from AWS Chatbot, see the Tutorial: Using AWS Chatbot to run an AWS Lambda function remotely.

Restart an Amazon EC2 instance

The following example shows how CLI commands can be used to restart your specified Amazon EC2 instance from your Slack channel. The parameters you include here are your instance id, min, and max size. For more information about restarting Amazon EC2 instances, see the reboot-instances command in the AWS CLI Command Reference.

@aws ec2 reboot-instances --instance-ids i-1234567890abcdef5

Change Auto Scaling limits

The following example shows how CLI commands can be used to change your Auto Scaling limits directly from your Slack channel. The parameters you include are the name of your Auto Scaling group and the minimum and maximum sizes. For more information about changing autoscaling limits, see update-autoscaling-group command in the AWS CLI Command Reference.

@aws autoscaling update-auto-scaling-group --auto-scaling-group-name my-asg --min-size 2 --max-size 10

Run an Automation runbook

The following example shows how CLI commands can be used to run an Automation runbook. In this command you specify your document name and your parameters. For more information, see the start-automation-execution command in the AWS CLI Command Reference.

@aws ssm start-automation-execution --document-name "AWS-UpdateLinuxAmi" --parameters "AutomationAssumeRole=arn:aws:iam::123456789012:role/SSMAutomationRole,SourceAmiId=ami-EXAMPLE,IamInstanceProfileName=EC2InstanceRole"

Use a Lambda function to approve an AWS CodePipeline action

The code example in this section demonstrates how you can use a Lambda function to perform activities, spefically how you can manually approve a pipeline action. This function enables you to approve or reject a pipeline action from Slack by entering the status and a summary. The function gets the required token using the get_pipeline_status method. It then uses the token value when applying the approval decision by using the put_approval_result method. For more information about these methods, see the CodePipeline section of the Boto3 docs 1.14.10 documentation. The Lambda code uses Python 3.8.

import boto3 def lambda_handler(event, context): client = boto3.client('codepipeline') getToken = client.get_pipeline_state( name = 'mypipeline1' ) myToken=getToken['stageStates']['actionStates']['latestExecution']['token'] response = client.put_approval_result( pipelineName='mypipeline1', stageName='beta', actionName='Approval', result={ 'summary': ['summary'], 'status': ['status'] }, token=myToken )

The following AWS Chatbot command invokes the Lambda function. For more information about CodePipeline and pipeline actions, see the AWS CodePipeline User Guide.

@aws invoke mypipeline1-beta-Approval --payload {"summary": "the design looks good, ready to release",“status”: "Approved"}