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.


# See: https://developer.twitter.com/en/apps/16043135

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 \

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)

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 \
  && 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