Streamline Slack File Uploads to Amazon S3 with AWS Lambda

Listen to this Post

Featured Image
Integrating external applications with AWS can significantly enhance workflow automation. One common use case is saving files from Slack to Amazon S3 for long-term storage. This article explores how to achieve this using the Slack API and AWS Lambda in a serverless architecture.

How It Works

  1. Slack API Setup: Configure a Slack app with necessary permissions to access files.
  2. AWS Lambda Function: Write a Lambda function to process Slack file events and upload them to S3.
  3. Event Trigger: Use Slack webhooks or AWS EventBridge to trigger the Lambda function when a file is shared.
  4. S3 Bucket Configuration: Set up an S3 bucket with proper IAM permissions for Lambda to write files.

You Should Know: Essential Commands & Code

1. Slack API Setup

  • Create a Slack App:
    curl -X POST https://slack.com/api/apps.manifest.create \
    -H "Authorization: Bearer xoxb-your-token" \
    -H "Content-Type: application/json" \
    -d '{
    "manifest": {
    "display_information": {
    "name": "Slack-to-S3"
    },
    "features": {
    "bot_user": {
    "display_name": "S3 Uploader"
    }
    },
    "oauth_config": {
    "scopes": {
    "bot": ["files:read"]
    }
    }
    }
    }'
    

2. AWS Lambda Function (Python)

import boto3
import requests
import os

s3 = boto3.client('s3')

def lambda_handler(event, context):
file_url = event['file']['url_private_download']
file_name = event['file']['name']

Download file from Slack
headers = {'Authorization': f'Bearer {os.environ["SLACK_TOKEN"]}'}
response = requests.get(file_url, headers=headers)

Upload to S3
s3.put_object(
Bucket='your-s3-bucket',
Key=f'slack-files/{file_name}',
Body=response.content
)
return {'statusCode': 200, 'body': 'File uploaded to S3'}

3. AWS CLI Commands

  • Create an S3 bucket:
    aws s3 mb s3://slack-files-backup
    
  • Set Lambda execution role:
    aws iam create-role --role-name lambda-s3-role --assume-role-policy-document file://trust-policy.json
    
  • Attach S3 access policy:
    aws iam attach-role-policy --role-name lambda-s3-role --policy-arn arn:aws:iam::aws:policy/AmazonS3FullAccess
    

4. Deploy Lambda via AWS SAM

AWSTemplateFormatVersion: '2010-09-09'
Resources:
SlackToS3Function:
Type: AWS::Serverless::Function
Properties:
CodeUri: slack-to-s3/
Handler: app.lambda_handler
Runtime: python3.9
Environment:
Variables:
SLACK_TOKEN: !Ref SlackToken
Policies:
- S3FullAccessPolicy:
BucketName: slack-files-backup

What Undercode Say

Automating Slack file backups to AWS S3 using Lambda is a powerful serverless solution. Key takeaways:
– Use Slack API’s `files:read` scope for file access.
– Secure AWS credentials with IAM roles.
– Optimize Lambda with error handling and logging.
– Monitor using AWS CloudWatch:

aws logs tail /aws/lambda/SlackToS3Function --follow

For advanced users, consider adding file encryption in S3:

aws s3api put-bucket-encryption --bucket slack-files-backup --server-side-encryption-configuration '{"Rules": [{"ApplyServerSideEncryptionByDefault": {"SSEAlgorithm": "AES256"}}]}'

Expected Output:

A fully automated pipeline that saves Slack files to S3, secured with AWS best practices.

Reference: AWS Community

References:

Reported By: Darryl Ruggles – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 Telegram