Posted as an attachment to Slack on AWS Lambda (Python)

I was making AWS Lambda that fetches a file from Amazon S3 and posts it to Slack as an attachment, but I struggled because AWS Lambda's Python environment did not include the requests module.

Premise / Environment

AWS Lambda Python environment

What I wanted to do

  1. Get the file from Amazon S3
  2. Post the file as an attachment with Slack's files.upload API (use multipart / form-data for content type)

What I couldn't do and why

Uploading files in Python Or As you can see in, it is standard to use requests. However, AWS Lambda's Python environment did not include the requests module.


--Use Lambda Layer with requests -** Use urllib.request instead of requests <-Adopt **


Even if you use urllib.request, you need to create multipart form data, so I referred to the following URL. The ʻencode_multipart_formdata` method described in this article converts the data part and the file part into the multipart form data format.

Using this, I did the following.


##Point 1:Define parameters excluding file parameter
data = {
   'token': <your token>,
   'channels': <your channels>,
   'initial_comment': key + 'Post:eyes:',
   'filename': key,
   'filetype': 'webp'

url = ""
content = open('/tmp/' + key, 'rb').read()

##Point 2:define the file parameter
file = {'file': {'filename': key, 'content': content}}

##Point 3:Convert data and file into one multipart form data
content_type, body = encode_multipart_formdata(data, file)

header = {'Content-Type': content_type}
request = urllib.request.Request(url, body, header)
response = urllib.request.urlopen(request)

The point is

  1. Define parameters in the data variable excluding the file parameter of the Slack files.upload API.
  2. Define the file parameter of the Slack files.upload API with the file variable
  3. Convert to multipart form data with ʻencode_multipart_formdata` method

3 points. You can now post files to Slack with AWS Lambda without using requests.


Which is easier to make a Lambda Layer ...?

Recommended Posts

Posted as an attachment to Slack on AWS Lambda (Python)
Support for Python 2.7 runtime on AWS Lambda (as of 2020.1)
I want to AWS Lambda with Python on Mac!
Run Python on Schedule on AWS Lambda
Try running a Schedule to start and stop an instance on AWS Lambda (Python)
Post images of Papillon regularly on Python + AWS Lambda + Slack
Building an environment to execute python programs on AWS EC2
[Python] Allow pip3 packages to be imported on AWS Lambda
Things to note when running Python on EC2 from AWS Lambda
[For Python] Quickly create an upload file to AWS Lambda Layer
[Python] Run Headless Chrome on AWS Lambda
Connect to s3 with AWS Lambda Python
Periodically run a python program on AWS Lambda
[2020 version] How to install Python3 on AWS EC2
Sample to send slack notification with python lambda
[AWS / Lambda] How to load Python external library
Summary of studying Python to use AWS Lambda
Feel free to turn Python using the library into an AWS Lambda function
Summary of points I was addicted to running Selenium on AWS Lambda (python)
Create a setting in terraform to send a message from AWS Lambda Python3.8 to Slack
Check types_map when using mimetypes on AWS Lambda (Python)
How to set layer on Lambda using AWS SAM
I tried to get an AMI using AWS Lambda
Deploy Python3 function with Serverless Framework on AWS Lambda
Amazon SNS → AWS Lambda → Slack → AWS Chatbot to execute AWS commands
A little trick to know when writing a Twilio application using Python on AWS Lambda
Post from Python to Slack
Post to vim → Python → Slack
[Python] Scraping in AWS Lambda
[AWS] Try adding Python library to Layer with SAM + Lambda (Python)
Post to slack with Python 3
Update python on Mac to 3.7-> 3.8
An introduction to Python Programming
Let's use AWS Lambda to create a mechanism to notify slack when the value monitored by CloudWatch is exceeded on Python
How to use Python lambda
Site monitoring and alert notification with AWS Lambda + Python + Slack
Post to Slack in Python
[Python] Convert CSV file uploaded to S3 to JSON file with AWS Lambda
How to make AWS Lambda Layers when running selenium × chrome on AWS Lambda
A game to go on an adventure in python interactive mode
xgboost (python) on EC2 Spot instance environment prepared by AWS Lambda
Best practice for logging in JSON format on AWS Lambda / Python
[Python / AWS Lambda layers] I want to reuse only module in AWS Lambda Layers
Slack --APIGateway --Lambda (Python) --How to make a RedShift interactive app
I tried to summarize everyone's remarks on slack with wordcloud (Python)
Prepare an environment to use OpenCV and Pillow with AWS Lambda
The road to installing Python and Flask on an offline PC
Building an environment to run ChainerMN on a GPU instance on AWS
[Introduction to Udemy Python 3 + Application] 58. Lambda
Python 3.6 on Windows ... and to Xamarin.
Summary if using AWS Lambda (Python)
Numba to speed up as Python
Introduction to Python Hands On Part 1
[Lambda] [Python] Post to Twitter from Lambda!
Write AWS Lambda function in Python
Notify HipChat with AWS Lambda (Python)
Steps to install python3 on mac
An introduction to Python for non-engineers
An alternative to `pause` in Python
[AWS SAM] Introduction to Python version
[Python Tutorial] An Easy Introduction to Python