|
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 |
2 | 16 |
|
3 | 17 | 1. You need an AWS account and your AWS credentials set up on your machine. |
4 | 18 | 2. Make sure you have an AWS IAM Role defined with the needed permissions for |
|
30 | 44 | Started Guide](https://slack.dev/bolt-python/tutorial/getting-started). |
31 | 45 | 6. Let's deploy the Lambda! Run `./deploy_lazy.sh`. By default it deploys to the |
32 | 46 | 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 |
34 | 48 | region that you deployed your app to. You should see a `bolt_py_function` |
35 | 49 | Lambda there. |
36 | 50 | 8. While your Lambda exists, it is not accessible to the internet, so Slack |
|
68 | 82 | - From this same Monitor tab, you can also click "View Logs in CloudWatch" to |
69 | 83 | see the execution logs for your Lambda. This can be helpful to see what |
70 | 84 | 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. |
0 commit comments