Brain Phrye
code cooking diy fiction reviews personal politics tools

Ephemeral Twitter Code

I’ve set up Vicky Lai’s ephemeral to make my tweets, well, ephemeral. But if you follow her README you’ll see the initial config is all manual. I’d rather have that be scripted so I can more easily replicate it in the future.

So I wrote a script to create the AWS lambda instance. The prerequisites for this are covered in Vicky’s README but you’ll also need the aws cli, a go install and I find it’s also useful to use named profiles.

Oh, and note that the MAX_TWEET_AGE var is handled by ParseDuration so make sure you write that correctly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
# See: https://developer.twitter.com/en/apps/16043135
TWITTER_CONSUMER_KEY=<secret>
TWITTER_CONSUMER_SECRET=<secret>
TWITTER_ACCESS_TOKEN=<secret>
TWITTER_ACCESS_TOKEN_SECRET=<secret>
MAX_TWEET_AGE=120h
WHITELIST=123:456:789
go get -u github.com/ChimeraCoder/anaconda
go get -u github.com/aws/aws-lambda-go/lambda
AWS_ACCOUNT_NUMBER=$(aws --profile me sts get-caller-identity|jq -r .Account)
if ! aws --profile me iam get-role --role-name twitter-ephemeral \
> /dev/null 2>&1; then
echo "Creating twitter-ephemeral role."
aws --profile me iam create-role \
--role-name twitter-ephemeral \
--assume-role-policy-document file://twitter-ephemeral.json
aws --profile me iam attach-role-policy \
--role-name twitter-ephemeral \
--policy-arn \
arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
fi
SCHED_RULE=$(aws --profile me events describe-rule \
--name twitter-ephemera 2> /dev/null | jq -r .Arn)
if [[ -z "$SCHED_RULE" ]]; then
aws --profile me events put-rule --schedule-expression "cron(5 2,14 * * ? *)" --name twitter-ephemeral
SCHED_RULE=$(aws --profile me events describe-rule \
--name twitter-ephemeral 2> /dev/null | jq -r .Arn)
fi
GOOS=linux go build \
&& zip ephemeral.zip ephemeral \
&& rm ephemeral \
&& aws --profile me lambda create-function \
--runtime go1.x \
--timeout 90 \
--role=arn:aws:iam::$AWS_ACCOUNT_NUMBER:role/twitter-ephemeral \
--handler=ephemeral \
--function-name ephemeral \
--zip-file fileb://ephemeral.zip \
--environment="Variables={TWITTER_CONSUMER_KEY=$TWITTER_CONSUMER_KEY,TWITTER_CONSUMER_SECRET=$TWITTER_CONSUMER_SECRET,TWITTER_ACCESS_TOKEN=$TWITTER_ACCESS_TOKEN,TWITTER_ACCESS_TOKEN_SECRET=$TWITTER_ACCESS_TOKEN_SECRET,MAX_TWEET_AGE=$MAX_TWEET_AGE,WHITELIST=$WHITELIST}" \
&& rm ephemeral.zip
aws --profile me lambda add-permission \
--function-name ephemeral \
--statement-id scheduled-twitter-ephemeral \
--action 'lambda:InvokeFunction' \
--principal events.amazonaws.com \
--source-arn $SCHED_RULE