Tutorial: Using AWS Chatbot to run an AWS Lambda function remotely
In this tutorial you use AWS Chatbot to run a Lambda function remotely and check the status of the Lambda function using Amazon CloudWatch. A Lambda function is a self contained block of organized resuable code that you write. Lambda functions are useful because they are run without provisioning or managing servers. Additionally, they are only invoked when needed based on your specifications. There are steps at the end of this tutorial to delete the resources you created.
Topics
Prerequisites
This tutorial assumes that you have some familiarity with the Lambda, AWS Chatbot, and CloudWatch consoles.
For more information, see the following topics:
-
Getting started with AWS Lambda in the AWS Lambda Developer Guide.
-
Setting up AWS Chatbot in the AWS Chatbot Administrator Guide.
-
Getting Set Up with CloudWatch in the Amazon CloudWatch User Guide.
The AWS Region that you select while setting up these consoles should be the same Region you specify in your Slack channel when your first AWS Command Line Interface (AWS CLI) command in Step 5: Invoke a Lambda function from Slack.
Step 1: Create a Lambda function
In this procedure you create a Lambda function in the console and test it.
To create a Lambda function
-
Sign in to the AWS Management Console and open the Lambda console at console.aws.amazon.com/lambda
. -
Choose Create function.
-
Choose Author From Scratch.
-
In Function Name, enter:
myHelloWorld
-
Choose Create Function.
-
Copy and paste the following example code into
index.js
.export const handler = async (event) => { // TODO implement const response = 'Hello World!' return response; }; };
-
Choose Deploy.
-
Choose Test.
-
In Event Name, enter:
myHelloWorld
-
Choose Save.
-
Choose Test and then verify that the Execution results tab displays Response: "Hello World!"
Step 2: Create an SNS topic
CloudWatch uses Amazon SNS to send notifications. First, you create an SNS topic and subscribe to it using your email. Later in the tutorial you use this SNS topic to configure AWS Chatbot.
To create an SNS topic
-
Open the Amazon SNS console
. -
In the left navigation pane, choose Topics.
-
Choose Create Topic.
-
Create a topic with the following settings:
-
Type – Standard
-
Name –
myHelloWorldNotifications
-
Display name –
myHelloWorld
-
-
Choose Create topic.
-
Choose Create subscription.
-
Create a subscription with the following settings:
-
Protocol –
Email
-
Endpoint – Your email address
-
-
Confirm subscription to the SNS by checking your email and choosing the link.
Step 3: Configure a CloudWatch alarm
A CloudWatch alarm monitors your Lambda function and sends a notification if an error occurs.
To create a CloudWatch alarm
-
Open the CloudWatch console
. -
Choose Alarms.
-
Choose Create alarm.
-
Choose Select metric.
-
Choose Lambda.
-
Choose By Function Name.
-
Choose myHelloWorld errors.
-
Change the following settings:
-
Period – 1 minute
-
Whenever Errors is
Greater
than0
-
Send notifications to –
myHelloWorldNotifications
-
Alarm name –
myHelloWorld-alarm
-
Alarm description –
Lambda myHelloWorld alarm
-
-
Choose Create alarm.
Step 4: Configure a Slack client for AWS Chatbot
You can configure a Slack client using AWS Chatbot to to run different commands in Slack using the AWS CLI. In this tutorial you use AWS CLI to invoke your Lambda function from Slack.
To create a Slack client
-
Open the AWS Chatbot console
. -
Under Configure a Chat client choose Slack, and then choose Configure.
Important
When you choose Configure, you are momentarily navigated away from the AWS Chatbot console.
-
In the upper right corner, choose the dropdown list, and then choose the Slack workspace that you want to use with AWS Chatbot.
Note
There's no limit to the number of workspaces that you can set up for AWS Chatbot, but you must set up each workspace one at a time.
-
Choose Allow.
-
Choose Configure new channel.
-
Under Configuration details, for Name, enter
myHelloWorld
. -
Under Channel type, choose Private.
-
Navigate to Slack and create a private channel by choosing the + button to the right of Channels.
-
Choose Create a channel.
-
Name the channel
myHelloWorld
. -
Choose to make the channel private.
-
Choose Create.
-
When prompted to add people, choose x.
-
Navigate back to the AWS Chatbot console and enter the private channel ID.
-
-
Define the IAM permissions that the chatbot uses for messaging your Slack chat room as shown following:
-
For Role name, enter
myHelloWorldRole
. -
For Policy Templates, select Read-only command permissions and Lambda-invoke command permissions.
-
-
In the SNS topics section, choose the appropriate AWS Region under Region.
-
Under Topics, select the myHelloWorldNotifcations topic.
-
Choose Configure.
Step 5: Invoke a Lambda function from Slack
After you configure a chatbot in AWS Chatbot, you can invoke Lambda functions from Slack
using AWS CLI syntax. To interact with AWS Chatbot in Slack, enter
@aws
followed by an AWS CLI command. For more information, see
Running AWS CLI commands from chat channels using AWS Chatbot in the
AWS Chatbot Administrator Guide.
To invoke a Lambda function
Invite AWS Chatbot to your channel by doing the following in Slack:
-
Enter
@AWS
. Choose Invite to Channel.
Tip
You only have to invite AWS Chatbot to the channel once.
If AWS is not listed as a valid member of the channel, you need to add the AWS Chatbot app to the Slack workspace. For more information, see the Getting started guide for AWS Chatbot.
-
-
Enter the following command in Slack:
@aws lambda invoke --function-name myHelloWorld --region
<your region>
Important
Replace
<your region>
with the same AWS Region you set while using the Lambda, CloudWatch, and AWS Chatbot consoles. You only need to specify the AWS Region in the channel once when you type your first AWS CLI command in Slack.Tip
AWS Chatbot also supports certain simplified AWS CLI syntaxes. For example, the simplified version of the previous command is shown following:
@aws invoke myHelloWorld --region
<your region>
-
Choose Yes.
-
The following output is shown:
ExecutedVersion: $LATEST Payload: \"Hello World\" StatusCode: 200
Troubleshooting
If you try to run your Lambda function in Slack and you encounter errors referring to the following permissions, revisit step 8 of the Step 4: Configure a Slack client for AWS Chatbot procedure and verify that you have the correct permissions assigned to your role:
-
Lambda-invoke command permissions
-
Read-only command permissions
Step 6: Test the CloudWatch alarm
In this step, you update the myHelloWorld function so that it returns an error, which triggers the CloudWatch alarm. By testing the alarm you can confirm that it's configured correctly and that you can view CloudWatch alarms in Slack (in addition to logs).
To test the CloudWatch alarm
-
Open the Lambda console Functions page
. -
Choose myHelloWorld.
-
Copy and paste the following example code into the Lambda function code:
exports.handler = async (event) => { throw new Error('this is an error'); };
-
Choose Deploy and confirm your changes have been deployed by viewing the label next to the Deploy button.
-
Return to your Slack channel and then enter the following command:
@aws invoke myHelloWorld
-
An error appears in your output, and you receive a CloudWatch alarm notification in Slack and an email. It might take a few minutes for you to receive the notifications.
-
To view logs, choose Show logs or Show error logs.
Troubleshooting
If you don't receive a notification in Slack or an email from CloudWatch, navigate to the CloudWatch console and on the left of the screen. Under Alarms, choose In alarm to confirm that your alarm has triggered. Your alarm name should appear on this page if it has been triggered successfully.
Step 7: Clean up resources
You can remove any resources created for this tutorial that you don't want to keep by navigating to the specific service’s console and deleting the resource. Removing unwanted or unused resources is beneficial because it lowers overall costs to you.
To delete the Lambda function
-
Open the Lambda console
. -
Choose myHelloWordFunction.
-
Choose Actions and then choose delete.
To delete the CloudWatch alarm
-
Open the CloudWatch console
. -
In the left navigation pane, choose Insufficient.
-
Choose myHelloWorld-alarm by selecting the check box.
-
Choose Actions and then choose delete.
To delete the AWS Chatbot configuration
-
Open the AWS Chatbot console
. -
Choose Slack.
-
Choose the radio button next to the channel you created and then choose Delete.