Last active
September 30, 2019 14:58
-
-
Save lyda/4bb5abf587997c84f4622b18c0541d15 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Documentation that helped with this: | |
# https://github.com/luebken/serverless-the-manual-way | |
# Status: | |
# https://eu-west-1.console.aws.amazon.com/lambda/home?region=eu-west-1 | |
# 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-ephemera 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Version": "2012-10-17", | |
"Statement": { | |
"Effect": "Allow", | |
"Principal": { | |
"Service": "lambda.amazonaws.com" | |
}, | |
"Action": "sts:AssumeRole" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment