Dead simple AWS SQS to Twitter serverless speaker ๐.
Publish message ๐ to SQS (FIFO), Lambda function will handle publishing it to Twitter. Automagically. ๐
To be honest, my plan is to automate the tweet of certain information using this queue in multiple side projects.
As usual, I'm sharing this with you folks as I was not able to find equivalent in Github / Open source projects. I hope it could be useful to someone else.
Update variables in
Makefile
andtf/variables.tf
according to your environment.
- type
make
to get started (help)
- AWS Account
- Twitter API Keys
- Terraform
make all
(all-in-one command)
make artifacts
(create S3 bucket)make package
(create the zip artifact)make tf-init
(to prepare terraform deployment)make tf-validate
(optional) validate the IaC + FMTmake tf-plan
(optional) Dry-Runmake tf-apply
Deploy the infrastructure as code
nb: Terraform is using remote state on S3.
How to get Twitter API Keys: https://developer.twitter.com/en/docs/basics/apps/guides/the-app-management-dashboard
After you've deployed the Terraform you will need to set Twitter API Keys manually to AWS Secret Manager using AWS console.
Set the following credentials in AWS Secret Manager:
- Consumer Key
- Consumer Key Secret
- Access Token
- Access Token Secret
aws sqs send-message --queue-url https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue --message-body "Information about the largest city in Any Region." --delay-seconds 10 --message-attributes file://send-message.json
Doc: https://docs.aws.amazon.com/cli/latest/reference/sqs/send-message.html
import boto3
# Create SQS client
sqs = boto3.client('sqs')
queue_url = 'SQS_QUEUE_URL'
# Send message to SQS queue
response = sqs.send_message(
QueueUrl=queue_url,
DelaySeconds=10,
MessageAttributes={
'Title': {
'DataType': 'String',
'StringValue': 'The Whistler'
},
'Author': {
'DataType': 'String',
'StringValue': 'John Grisham'
},
'WeeksOn': {
'DataType': 'Number',
'StringValue': '6'
}
},
MessageBody=(
'Information about current NY Times fiction bestseller for '
'week of 12/11/2016.'
)
)
print(response['MessageId'])
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update the tests as appropriate. (just kidding, there is no test)
- AWS SQS
- AWS Lambda function
- AWS Secrets Manager
- Tweepy Python Library