|
| 1 | +# Twitter Bot |
| 2 | + |
| 3 | +automatically tweet on a schedule |
| 4 | + |
| 5 | +## Create Twitter Application |
| 6 | + |
| 7 | +More info here <https://developer.twitter.com/en/docs/basics/apps/overview>. |
| 8 | + |
| 9 | +Note the Consumer API keys and Access token & access token secret. |
| 10 | + |
| 11 | +```sh |
| 12 | +CONSUMER_KEY=... |
| 13 | +CONSUMER_SECRET=... |
| 14 | +ACCESS_TOKEN=... |
| 15 | +ACCESS_TOKEN_SECRET=... |
| 16 | +``` |
| 17 | + |
| 18 | +## Create SSM Parameters |
| 19 | + |
| 20 | +Use the keys from the previous step: |
| 21 | + |
| 22 | +```sh |
| 23 | +aws ssm put-parameter --cli-input-json '{"Type": "SecureString", "KeyId": "alias/aws/ssm", "Name": "/TwitterBot/consumer_key", "Value": "'"$CONSUMER_KEY"'"}' |
| 24 | + |
| 25 | +aws ssm put-parameter --cli-input-json '{"Type": "SecureString", "KeyId": "alias/aws/ssm", "Name": "/TwitterBot/consumer_secret", "Value": "'"$CONSUMER_SECRET"'"}' |
| 26 | + |
| 27 | +aws ssm put-parameter --cli-input-json '{"Type": "SecureString", "KeyId": "alias/aws/ssm", "Name": "/TwitterBot/access_token", "Value": "'"$ACCESS_TOKEN"'"}' |
| 28 | + |
| 29 | +aws ssm put-parameter --cli-input-json '{"Type": "SecureString", "KeyId": "alias/aws/ssm", "Name": "/TwitterBot/access_token_secret", "Value": "'"$ACCESS_TOKEN_SECRET"'"}' |
| 30 | +``` |
| 31 | + |
| 32 | +## Create an S3 bucket and upload the data file |
| 33 | + |
| 34 | +```sh |
| 35 | +aws s3 mb s3://123456789012-twitterbot |
| 36 | +aws s3 cp data.txt s3://123456789012-twitterbot |
| 37 | +``` |
| 38 | + |
| 39 | +## Create IAM execution role for Lambda |
| 40 | + |
| 41 | +Grant access to your S3 bucket: |
| 42 | + |
| 43 | +```json |
| 44 | +{ |
| 45 | + "Version": "2012-10-17", |
| 46 | + "Statement": [{ |
| 47 | + "Effect": "Allow", |
| 48 | + "Action": [ |
| 49 | + "s3:GetObject" |
| 50 | + ], |
| 51 | + "Resource": "arn:aws:s3:::123456789012-twitterbot/*" |
| 52 | + }] |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +## Create deployment package |
| 57 | + |
| 58 | +Create a new virtual environment using `pipenv` and install the required libraries: |
| 59 | + |
| 60 | +```sh |
| 61 | +pipenv --python 3.7 |
| 62 | +pipenv shell |
| 63 | +pipenv install tweepy |
| 64 | +mkdir package |
| 65 | +pipenv lock -r > requirements.txt |
| 66 | +pip install -r requirements.txt --no-deps -t package |
| 67 | +cd package |
| 68 | +zip -r9 ../package.zip . |
| 69 | +cd .. |
| 70 | +zip -g package.zip lambda_handler.py |
| 71 | +``` |
| 72 | + |
| 73 | +<!-- |
| 74 | +## Update Lambda funcion |
| 75 | +
|
| 76 | +```sh |
| 77 | +aws lambda update-function-code --function-name TwitterBot --zip-file fileb://package.zip |
| 78 | +``` --> |
| 79 | + |
| 80 | +## Create Lambda function |
| 81 | + |
| 82 | +```sh |
| 83 | +aws lambda create-function \ |
| 84 | +--function-name TwitterBot \ |
| 85 | +--zip-file fileb://package.zip \ |
| 86 | +--role arn:aws:iam::123456789012:role/LambdaTwitterBotRole \ |
| 87 | +--handler lambda_function.lambda_handler \ |
| 88 | +--runtime python3.7 \ |
| 89 | +--environment Variables={BUCKET_NAME=123456789012-twitterbot} |
| 90 | +``` |
| 91 | + |
| 92 | +## Create CloudWatch Scheduled Rule |
| 93 | + |
| 94 | +```sh |
| 95 | +aws events put-rule \ |
| 96 | +--name TwitterBot \ |
| 97 | +--schedule-expression 'rate(1 hour)' |
| 98 | + |
| 99 | +aws lambda add-permission \ |
| 100 | +--function-name TwitterBot \ |
| 101 | +--statement-id TwitterBot \ |
| 102 | +--action 'lambda:InvokeFunction' \ |
| 103 | +--principal events.amazonaws.com \ |
| 104 | +--source-arn arn:aws:events:us-east-1:123456789012:rule/TwitterBot |
| 105 | + |
| 106 | +aws events put-targets --rule TwitterBot --targets file://targets.json |
| 107 | +``` |
0 commit comments