This directory contains two example apps. Both respond to the Slash Command
/hello-bolt-python-lambda
and both respond to app at-mentions.
The "Lazy Lambda Listener" example is the simpler application and it leverages AWS Lambda and AWS Lambda Function URL to execute the Bolt app logic in Lambda and expose the application HTTP routes to the internet via Lambda URL. The "OAuth Lambda Listener" example additionally includes OAuth flow handling routes and uses AWS S3 to store workspace installation credentials and OAuth flow state variables, enabling your app to be installed by anyone.
Instructions on how to set up and deploy each example are provided below.
- You need an AWS account and your AWS credentials set up on your machine.
- Make sure you have an AWS IAM Role defined with the needed permissions for your Lambda function powering your Slack app:
- Head to the AWS IAM section of AWS Console
- Click Roles from the menu
- Click the Create Role button
- Under "Select type of trusted entity", choose "AWS service"
- Under "Choose a use case", select "Common use cases: Lambda"
- Click "Next: Permissions"
- Under "Attach permission policies", enter "lambda" in the Filter input
- Check the "AWSLambdaBasicExecutionRole", "AWSLambdaExecute" and "AWSLambdaRole" policies
- Click "Next: tags"
- Click "Next: review"
- Enter
bolt_python_lambda_invocation
as the Role name. You can change this if you want, but then make sure to update the role name inlazy_aws_lambda_config.yaml
- Optionally enter a description for the role, such as "Bolt Python basic role"
- Ensure you have created an app on api.slack.com/apps as per the Getting Started Guide. Ensure you have installed it to a workspace.
- Ensure you have exported your Slack Bot Token and Slack Signing Secret for your
apps as the environment variables
SLACK_BOT_TOKEN
andSLACK_SIGNING_SECRET
, respectively, as per the Getting Started Guide. - You may want to create a dedicated virtual environment for this example app, as per the "Setting up your project" section of the Getting Started Guide.
- Let's deploy the Lambda! Run
./deploy_lazy.sh
. By default it deploys to the us-east-1 region in AWS - you can change this at the top oflazy_aws_lambda_config.yaml
if you wish. - Load up AWS Lambda inside the AWS Console - make sure you are in the correct
region that you deployed your app to. You should see a
bolt_py_function
Lambda there. - While your Lambda exists, it is not accessible to the internet, so Slack cannot send events happening in your Slack workspace to your Lambda. Let's fix that by adding an AWS Lambda Function URL to your Lambda so that your Lambda can accept HTTP requests:
- Click on your
bolt_py_function
Lambda - In the Function Overview click "Configuration"
- On the left side, click "Function URL"
- Click "Create function URL"
- Choose auth type "NONE"
- Click "Save"
- Congrats! Your Slack app is now accessible to the public. On the right side of
your
bolt_py_function
Function Overview you should see your Lambda Function URL. - Copy this URL to your clipboard.
- We will now inform Slack that this example app can accept Slash Commands.
- Back on api.slack.com/apps, select your app and choose Slash Commands from the left menu.
- Click Create New Command
- By default, the
lazy_aws_lambda.py
function has logic for a/hello-bolt-python-lambda
command. Enter/hello-bolt-python-lambda
as the Command. - Under Request URL, paste in the previously-copied Lambda Function URL.
- Click Save
- Test it out! Back in your Slack workspace, try typing
/hello-bolt-python-lambda hello
. - If you have issues, here are some debugging options:
- Check the Monitor tab under your Lambda. Did the Lambda get invoked? Did it respond with an error? Investigate the graphs to see how your Lambda is behaving.
- From this same Monitor tab, you can also click "View Logs in CloudWatch" to see the execution logs for your Lambda. This can be helpful to see what errors are being raised.
You need an AWS account and your AWS credentials set up on your machine.
Once you’ve done that you should have access to AWS Console, which is what we’ll use for the rest of this tutorial.
- Start by creating two S3 buckets:
- One to store installation credentials for each Slack workspace that installs your app.
- One to store state variables during the OAuth flow.
- Head over to Amazon S3 in the AWS Console
- Give your bucket a name, region, and set access controls. If you’re doing this for the first time, it’s easiest to keep the defaults and edit them later as necessary. We'll be using the names:
- slack-installations-s3
- slack-state-store-s3
- After your buckets are created, in each bucket’s page head over to “Properties” and save the Amazon Resource Name (ARN). It should look something like
arn:aws:s3:::slack-installations-s3
.
Now let's create a policy that will allow the holder of the policy to take actions in your S3 buckets.
- Head over to Identity and Access Management (IAM) in the AWS Console via Search Bar
- Head to Access Management > Policies and select “Create Policy”
- Click the JSON tab and copy this in:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:Get*",
"s3:Put*",
"s3:Delete*",
"s3-object-lambda:*"
],
"Resource": [
"<your-first-bucket-arn>/*", // don't forget the `/*`
"<your-second-bucket-arn>/*"
]
}
]
}
- Edit “Resource” to include the ARNs of the two buckets you created in the earlier step. These need to exactly match the ARNS you copied earlier and end with a
/*
- Hit "Next:Tags" and "Next:Review"
- Review policy
- Name your policy something memorable enough that you won’t have forgotten it 5 minutes from now when we’ll need to look it up from a list. (e.g. AmazonS3-FullAccess-SlackBuckets)
- Review the summary, and hit "Create Policy". Once the policy is created you should be redirected to the Policies page and see your new policy show up as Customer managed policy.
Let’s create a user role that will use the custom policy we created as well as other policies to let us execute our lambda, write output logs to CloudWatch.
- Head to the Identity and Access Management (IAM) section of AWS Console
- Select Access Management > Roles from the menu
- Click "Create Role"
- Step 1 - Select trusted entity
- Under "Select type of trusted entity", choose "AWS service"
- Under "Choose a use case", select "Common use cases: Lambda"
- Click "Next: Permissions"
- Step 2 - Add permissions
- Add the following policies to the role we’re creating that will allow the user with the role permission to execute Lambda, make changes to their S3 Buckets, log output to CloudWatch
AWSLambdaExecute
AWSLambdaBasicExecutionRole
AWSLambdaRole
<NameOfS3PolicyYouCreatedEarlier>
- Add the following policies to the role we’re creating that will allow the user with the role permission to execute Lambda, make changes to their S3 Buckets, log output to CloudWatch
- Step 3 - Name, review, create
- Enter
bolt_python_s3_storage
as your role name. To use a different name, make sure to update the role name inaws_lambda_oauth_config.yaml
- Optionally enter a description for the role, such as "Bolt Python with S3 access role”
- "Create Role"
- Enter
Ensure you have created an app on api.slack.com/apps as per the Getting Started Guide. You do not need to ensure you have installed it to a workspace, as the OAuth flow will provide your app the ability to be installed by anyone.
- Remember those S3 buckets we made? You will need the names of these buckets again in the next step.
- You need many environment variables exported! Specifically the following from api.slack.com/apps
SLACK_SIGNING_SECRET= # Signing Secret from Basic Information page
SLACK_CLIENT_ID= # Client ID from Basic Information page
SLACK_CLIENT_SECRET # Client Secret from Basic Information page
SLACK_SCOPES= "app_mentions:read,chat:write"
SLACK_INSTALLATION_S3_BUCKET_NAME: # The name of installations bucket
SLACK_STATE_S3_BUCKET_NAME: # The name of the state store bucket
export
- Let's deploy the Lambda! Run
./deploy_oauth.sh
. By default it deploys to the us-east-1 region in AWS - you can customize this inaws_lambda_oauth_config.yaml
. - Load up AWS Lambda inside the AWS Console - make sure you are in the correct region that you deployed your app to. You should see a
bolt_py_oauth_function
Lambda there.
Your Lambda exists, but it is not accessible to the internet, so Slack cannot yet send events happening in your Slack workspace to your Lambda. Let's fix that by adding an AWS Lambda Function URL to your Lambda so that your Lambda can accept HTTP requests
- Click on your
bolt_py_oauth_function
Lambda - In the Function Overview, on the left side, click "Configuration
- On the left side, click "Function URL"
- Click "Create function URL"
- Choose auth type "NONE"
- Click "Save"
Phew, congrats! Your Slack app is now accessible to the public. On the right side of your bolt_py_oauth_function Function Overview you should see a your Lambda Function URL.
- Copy it - this is the URL your Lambda function is accessible at publicly.
- We will now inform Slack that this example app can accept Slash Commands.
- Back on api.slack.com/apps, select your app and choose "Slash Commands" from the left menu.
- Click "Create New Command"
- By default, the
aws_lambda_oauth.py
function has logic for a /hello-bolt-python-lambda command. Enter/hello-bolt-python-lambda
as the Command.
- Under Request URL, paste in the previously-copied Lambda Function URL.
- Click "Save"
- By default, the
- We also need to register the API Endpoint as the OAuth redirect URL:
- Load up the OAuth & Permissions page onapi.slack.com/apps
- Scroll down to "Redirect URLs"
- Copy the URL endpoint in - but remove the path portion. The Redirect URL needs to only partially match where we will send users.
You can now install the app to any workspace!
- Once installed to a Slack workspace, try typing
/hello-bolt-python-lambda
hello. - If you have issues, here are some debugging options:
- View lambda activity: Head to the Monitor tab under your Lambda. Did the Lambda get invoked? Did it respond with an error? Investigate the graphs to see how your Lambda is behaving.
- Check out the logs: From this same Monitor tab, you can also click "View Logs in CloudWatch" to see the execution logs for your Lambda. This can be helpful to see what errors are being raised.