Custom actions - AWS Chatbot

Custom actions

Custom actions are preconfigured buttons you add to custom and default notifications. These actions allow you to automate commonly used DevOps processes and incident response tasks. When you create a custom action, you configure your action button to run either a CLI command or a Lambda function. Action targets can be paramaterized by using the parameters available in your notification metadata. You can use custom actions to retrieve telemetry information, run an automation runbook, and notify team members. When an issue arises, you can take action directly from your notifications. Custom actions are available in AWS Chatbot configurations for Microsoft Teams and Slack. Custom actions can't be created using AWS CDK at this time.

No additional permissions are needed to configure or run custom actions. When your channel members choose the custom action button, the action target is invoked using the configured IAM permissions in your channel configuration.

Creating a custom action

You can create custom actions using Lambda functions in your account or by entering AWS CLI commands. AWS CLI command Shortcuts function similarly to Command aliases.

Lambda action
To create a Lambda action
  1. Choose the vertical ellipsis button on the bottom of a notification in your chat channel.

  2. In Manage actions, choose Create.

  3. Enter a custom action name. This name is a unique identifier for your custom action.

  4. Enter a name for your custom action button. This name is shown on a button on your notification. This name should be 20 characters or less and can incorporate emojis.

  5. For Custom action type, select Lambda action.

  6. Choose Next.

  7. Select an AWS account.

  8. Select a Region.

    Note

    The available Lambda functions are populated from this account and Region.

  9. Choose Load Lambdas.

  10. In Define Lambda Function, select a Lambda function.

  11. Enter your payload.

    Note

    We expose predefined variables from your custom notifications as useable variables.

  12. (Optional) Add more variables by choosing + Add more variables.

  13. Choose Next.

  14. (Optional) Add additional display criteria:

    1. Select a variable.

    2. Select a condition.

    3. Choose Add.

  15. Choose Save.

AWS CLI command action
To create an AWS CLI command action
  1. Choose the vertical ellipsis button on the bottom of a notification in your chat channel.

  2. In Manage actions, choose Create.

  3. Enter a custom action name. This name is a unique identifier for your custom action.

  4. Enter a name for your custom action button. This name is shown on a button on your notification. This name should be 20 characters or less and can incorporate emojis.

  5. For Custom action type, select Command action.

  6. Enter a command.

    Note

    We expose predefined variables from your custom notifications as useable variables.

  7. (Optional) Add more variables by choosing + Add more variables.

  8. Choose Next.

  9. (Optional) Add additional display criteria:

    1. Select a variable.

    2. Select a condition.

    3. Choose Add.

  10. Choose Save.

Use cases

Custom notifications metadata

You can use custom notifications to specify metadata for your custom actions. Custom actions display this information as variables that you define in the payload. Before you run a custom action, you're shown a complete summary of that action and its payload. In the following example, a custom notification for allow listing an IP address contains an IP address as metadata:

{ "version": "1.0", "source": "custom", "content": { "title": "IP Address Allowlist Request", "description": "We have received a request to allows list an IP address from 'sample@sample.com'." }, "metadata": { "additionalContext": { "IPAddress": "192.168.0.1" } } }

If you create a custom action based on this notification, {"message": "I'ved allow listed IP address: $IPAddress"} is shown as an available notification variable that you can use in your payload. For example, the following Lambda action payload displays the message and the IP address variable.

{"message": "I'ved allow listed IP address: $IPAddress"}

Recent Amazon CloudWatch Logs errors

The following example shows how you can use custom actions to display recent errors from an Amazon CloudWatch Logs group in your channel from an Amazon CloudWatch Logs notification.

The following Lambda function returns a list of the most common Amazon CloudWatch Logs errors:

import boto3 from collections import Counter import json import datetime def extract_message(value): if value.startswith('{'): try: structured_message = json.loads(value) return structured_message.get('message', value) except Exception: pass return value def take_ellipsis(value, length): if len(value) <= length: return value else: return value[:length - 1] + '…' def lambda_handler(event, context): log_group_name = event['logGroup'] lookback_minutes = int(event.get('minutes', 60)) filter = event.get('filter', 'ERROR') limit = int(event.get('limit', 10)) logs_client = boto3.client('logs') now = datetime.datetime.now() offset = now - datetime.timedelta(minutes=lookback_minutes) print(offset) start_time = int(offset.timestamp() * 1000) end_time = int(now.timestamp() * 1000) response = logs_client.filter_log_events( logGroupName=log_group_name, startTime=start_time, endTime=end_time, filterPattern=filter ) messages = [extract_message(event['message']) for event in response['events']] message_counts = Counter(messages) top_messages = message_counts.most_common(limit) if top_messages: formatted_messages = [ f'{index + 1}. `{take_ellipsis(message[0], 100)}` ({message[1]} occurrences)' for index, error in enumerate(top_messages) ] message_summary = '\n'.join(formatted_messages) return f'*Most common errors in `{log_group_name}` within the last hour*\n\n' + message_summary else: return f'Found no errors matching filter within the last {lookback_minutes} minutes in {log_group_name}'

You can create a Lambda action that invokes this Lambda function and view errors for specific log groups by choosing this function while creating your action and entering the following as the payload:

{ "logGroup": "$LogGroup" }