Skip to content

Commit 008758e

Browse files
authored
Updating lazy lambda OAuth example (slackapi#444)
* Updating lazy lambda OAuth example. * Removing references to no longer used environment variable.
1 parent df43450 commit 008758e

6 files changed

Lines changed: 107 additions & 13 deletions

File tree

examples/aws_lambda/.env.oauth_sample

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ export SLACK_CLIENT_SECRET=
44
export SLACK_SCOPES=app_mentions:read,channels:history,im:history,chat:write
55
export SLACK_INSTALLATION_S3_BUCKET_NAME=
66
export SLACK_STATE_S3_BUCKET_NAME=
7-
export SLACK_LAMBDA_PATH=/default/bolt_py_function

examples/aws_lambda/README.md

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
# Lazy Lambda Listener Example Bolt App
1+
# AWS Lambda Bolt Python Examples
2+
3+
This directory contains two example apps. Both respond to the Slash Command
4+
`/hello-bolt-python-lambda` and both respond to app at-mentions.
5+
6+
The "Lazy Lambda Listener" example is the simpler application and it leverages
7+
AWS Lambda and AWS API Gateway to execute the Bolt app logic in Lambda and
8+
expose the application HTTP routes to the internet via API Gateway. The "OAuth
9+
Lambda Listener" example additionally includes OAuth flow handling routes and uses
10+
AWS S3 to store workspace installation credentials and OAuth flow state
11+
variables, enabling your app to be installed by anyone.
12+
13+
Instructions on how to set up and deploy each example are provided below.
14+
15+
## Lazy Lambda Listener Example Bolt App
216

317
1. You need an AWS account and your AWS credentials set up on your machine.
418
2. Make sure you have an AWS IAM Role defined with the needed permissions for
@@ -30,7 +44,7 @@
3044
Started Guide](https://slack.dev/bolt-python/tutorial/getting-started).
3145
6. Let's deploy the Lambda! Run `./deploy_lazy.sh`. By default it deploys to the
3246
us-east-1 region in AWS - you can change this at the top of `lazy_aws_lambda_config.yaml` if you wish.
33-
7. Load up AWS Lambda inside the AWS Console - make sure you are in the correct
47+
7. Load up AWS Lambda inside the AWS Console - make sure you are in the correct
3448
region that you deployed your app to. You should see a `bolt_py_function`
3549
Lambda there.
3650
8. While your Lambda exists, it is not accessible to the internet, so Slack
@@ -68,3 +82,90 @@
6882
- From this same Monitor tab, you can also click "View Logs in CloudWatch" to
6983
see the execution logs for your Lambda. This can be helpful to see what
7084
errors are being raised.
85+
86+
## OAuth Lambda Listener Example Bolt App
87+
88+
1. You need an AWS account and your AWS credentials set up on your machine.
89+
2. Make sure you have an AWS IAM Role defined with the needed permissions for
90+
your Lambda function powering your Slack app:
91+
- Head to the AWS IAM section of AWS Console
92+
- Click Roles from the menu
93+
- Click the Create Role button
94+
- Under "Select type of trusted entity", choose "AWS service"
95+
- Under "Choose a use case", select "Common use cases: Lambda"
96+
- Click "Next: Permissions"
97+
- Under "Attach permission policies", enter "lambda" in the Filter input
98+
- Check the "AWSLambdaBasicExecutionRole" and "AWSLambdaExecute" policies
99+
- Under "Attach permission policies", enter "s3" in the Filter input
100+
- Check the "AWSS3FullAccess" policy
101+
- Click "Next: tags"
102+
- Click "Next: review"
103+
- Enter `bolt_python_s3_storage` as the Role name. You can change this
104+
if you want, but then make sure to update the role name in
105+
`aws_lambda_oauth_config.yaml`
106+
- Optionally enter a description for the role, such as "Bolt Python with S3
107+
access role"
108+
3. Ensure you have created an app on api.slack.com/apps as per the [Getting
109+
Started Guide](https://slack.dev/bolt-python/tutorial/getting-started).
110+
You do not need to ensure you have installed it to a workspace, as the OAuth
111+
flow will provide your app the ability to be installed by anyone.
112+
4. You will need to create two S3 buckets: one to store installation credentials
113+
(when a new Slack workspace installs your app) and one to store state
114+
variables during the OAuth flow. You will need the names of these buckets in
115+
the next step.
116+
5. You need many environment variables exported! Specifically the following from
117+
api.slack.com/apps:
118+
- `SLACK_SIGNING_SECRET`: Signing Secret from Basic Information page
119+
- `SLACK_CLIENT_ID`: Client ID from Basic Information page
120+
- `SLACK_CLIENT_SECRET`: Client Secret from Basic Information page
121+
- `SLACK_SCOPES="app_mentions:read,chat:write"`: Which scopes this application
122+
needs
123+
- `SLACK_INSTALLATION_S3_BUCKET_NAME`: The name of one of the S3 buckets you
124+
created
125+
- `SLACK_STATE_S3_BUCKET_NAME`: The name of the other S3 bucket you created
126+
6. Let's deploy the Lambda! Run `./deploy_oauth.sh`. By default it deploys to the
127+
us-east-1 region in AWS - you can change this at the top of `aws_lambda_oauth_config.yaml` if you wish.
128+
7. Load up AWS Lambda inside the AWS Console - make sure you are in the correct
129+
region that you deployed your app to. You should see a `bolt_py_oauth_function`
130+
Lambda there.
131+
8. While your Lambda exists, it is not accessible to the internet, so Slack
132+
cannot send events happening in your Slack workspace to your Lambda. Let's
133+
fix that by adding an AWS API Gateway in front of your Lambda so that your
134+
Lambda can accept HTTP requests:
135+
- Click on your `bolt_py_oauth_function` Lambda
136+
- In the Function Overview, on the left side, click "+ Add Trigger"
137+
- Select API Gateway from the trigger list
138+
- Make sure "Create an API" is selected in the dropdown, and choose "HTTP API"
139+
as the API Type
140+
- Under Security, select "Open"
141+
- Click "Add"
142+
9. Congrats! Your Slack app is now accessible to the public. On the left side of
143+
your `bolt_py_oauth_function` Function Overview you should see a purple API Gateway
144+
icon. Click it.
145+
10. Click Details to expand the details section.
146+
11. Copy the API Endpoint - this is the URL your Lambda function is accessible
147+
at publicly.
148+
12. We will now inform Slack that this example app can accept Slash Commands.
149+
- Back on api.slack.com/apps, select your app and choose Slash Commands from the left menu.
150+
- Click Create New Command
151+
- By default, the `aws_lambda_oauth.py` function has logic for a
152+
`/hello-bolt-python-lambda` command. Enter `/hello-bolt-python-lambda` as
153+
the Command.
154+
- Under Request URL, paste in the previously-copied API Endpoint from API
155+
Gateway.
156+
- Click Save
157+
13. We also need to register the API Endpoint as the OAuth redirect URL:
158+
- Load up the "OAuth & Permissions" page on api.slack.com/apps
159+
- Scroll down to Redirect URLs
160+
- Copy the API endpoint in - but remove the path portion. The Redirect URL
161+
needs to only _partially_ match where we will send users.
162+
14. You can now install the app to any workspace!
163+
15. Test it out! Once installed to a Slack workspace, try typing
164+
`/hello-bolt-python-lambda hello`.
165+
16. If you have issues, here are some debugging options:
166+
- Check the Monitor tab under your Lambda. Did the Lambda get invoked? Did it
167+
respond with an error? Investigate the graphs to see how your Lambda is
168+
behaving.
169+
- From this same Monitor tab, you can also click "View Logs in CloudWatch" to
170+
see the execution logs for your Lambda. This can be helpful to see what
171+
errors are being raised.

examples/aws_lambda/aws_lambda.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import logging
2-
import sys
32

4-
sys.path.insert(1, "vendor")
53
from slack_bolt import App
64
from slack_bolt.adapter.aws_lambda import SlackRequestHandler
75

examples/aws_lambda/aws_lambda_oauth.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import logging
2-
import sys
3-
4-
sys.path.insert(1, "vendor")
52

63
from slack_bolt import App
74
from slack_bolt.adapter.aws_lambda import SlackRequestHandler

examples/aws_lambda/aws_lambda_oauth_config.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
region: us-east-1
22

3-
function_name: bolt_py_function
3+
function_name: bolt_py_oauth_function
44
handler: aws_lambda_oauth.handler
55
description: My first lambda function
66
runtime: python3.8
@@ -31,7 +31,6 @@ environment_variables:
3131
SLACK_SCOPES: ${SLACK_SCOPES}
3232
SLACK_INSTALLATION_S3_BUCKET_NAME: ${SLACK_INSTALLATION_S3_BUCKET_NAME}
3333
SLACK_STATE_S3_BUCKET_NAME: ${SLACK_STATE_S3_BUCKET_NAME}
34-
SLACK_LAMBDA_PATH: ${SLACK_LAMBDA_PATH}
3534

3635

3736
# If `tags` is uncommented then tags will be set at creation or update
@@ -43,4 +42,4 @@ environment_variables:
4342

4443
# Build options
4544
build:
46-
source_directories: vendor # a comma delimited list of directories in your project root that contains source to package.
45+
source_directories: slack_bolt # a comma delimited list of directories in your project root that contains source to package.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
2-
rm -rf vendor && mkdir -p vendor/slack_bolt && cp -pr ../../slack_bolt/* vendor/slack_bolt/
2+
rm -rf slack_bolt && mkdir slack_bolt && cp -pr ../../slack_bolt/* slack_bolt/
33
pip install python-lambda -U
44
lambda deploy \
55
--config-file aws_lambda_oauth_config.yaml \
6-
--requirements requirements_oauth.txt
6+
--requirements requirements_oauth.txt

0 commit comments

Comments
 (0)