├── .gitignore ├── src ├── .gitignore ├── lambda_handler.py └── sc2quotes.txt ├── .env.example ├── README.md ├── variables.tf └── main.tf /.gitignore: -------------------------------------------------------------------------------- 1 | .terraform 2 | .env 3 | lambda.zip -------------------------------------------------------------------------------- /src/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | !lambda_handler.py 4 | !sc2quotes.txt -------------------------------------------------------------------------------- /.env.example: -------------------------------------------------------------------------------- 1 | # Twitter API keys 2 | export TF_VAR_CONSUMER_KEY= 3 | export TF_VAR_CONSUMER_SECRET= 4 | export TF_VAR_ACCESS_KEY= 5 | export TF_VAR_ACCESS_SECRET= -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | sc2_ebooks 2 | 3 | copy `.env.example` to `.env` and fill in the required values 4 | 5 | pack the dependencies into the src folder: `pip install -t src tweepy` 6 | 7 | `terraform init` 8 | 9 | `terraform apply` -------------------------------------------------------------------------------- /variables.tf: -------------------------------------------------------------------------------- 1 | # Twitter API keys 2 | variable "CONSUMER_KEY" { 3 | type = string 4 | } 5 | 6 | variable "CONSUMER_SECRET" { 7 | type = string 8 | } 9 | 10 | variable "ACCESS_KEY" { 11 | type = string 12 | } 13 | 14 | variable "ACCESS_SECRET" { 15 | type = string 16 | } 17 | 18 | # path to build zip file to 19 | variable "lambda_zip" { 20 | default = "lambda.zip" 21 | } -------------------------------------------------------------------------------- /src/lambda_handler.py: -------------------------------------------------------------------------------- 1 | import os, random, tweepy 2 | 3 | def lambda_handler(event, context): 4 | auth = tweepy.OAuthHandler(os.getenv('CONSUMER_KEY'), os.getenv('CONSUMER_SECRET')) 5 | auth.set_access_token(os.getenv('ACCESS_KEY'), os.getenv('ACCESS_SECRET')) 6 | api = tweepy.API(auth) 7 | 8 | quotes=list(open('sc2quotes.txt', encoding="utf-8-sig")) 9 | quote=random.choice(quotes) 10 | api.update_status(quote) -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | backend "s3" { 3 | bucket = "sc2quotes-tfstate" 4 | key = "terraform" 5 | region = "eu-west-2" 6 | } 7 | } 8 | 9 | provider "aws" { 10 | region = "eu-west-2" 11 | } 12 | 13 | data "archive_file" "lambda_zip" { 14 | type = "zip" 15 | source_dir = "./src" 16 | output_path = var.lambda_zip 17 | } 18 | 19 | resource "aws_lambda_function" "lambda" { 20 | filename = var.lambda_zip 21 | function_name = "sc2quotes_lambda" 22 | role = aws_iam_role.role.arn 23 | handler = "lambda_handler.lambda_handler" 24 | runtime = "python3.7" 25 | source_code_hash = filebase64sha256(var.lambda_zip) 26 | environment { 27 | variables = { 28 | CONSUMER_KEY = var.CONSUMER_KEY 29 | CONSUMER_SECRET = var.CONSUMER_SECRET 30 | ACCESS_KEY = var.ACCESS_KEY 31 | ACCESS_SECRET = var.ACCESS_SECRET 32 | } 33 | } 34 | } 35 | 36 | # Schedule 37 | resource "aws_cloudwatch_event_rule" "event_rule" { 38 | name = "sc2quotes_cron" 39 | description = "posting the quotes on the times" 40 | schedule_expression = "rate(2 hours)" 41 | } 42 | 43 | resource "aws_cloudwatch_event_target" "event_target" { 44 | rule = aws_cloudwatch_event_rule.event_rule.name 45 | arn = aws_lambda_function.lambda.arn 46 | } 47 | 48 | resource "aws_lambda_permission" "event_permission" { 49 | statement_id = "sc2quotes_permission" 50 | action = "lambda:InvokeFunction" 51 | function_name = aws_lambda_function.lambda.function_name 52 | principal = "events.amazonaws.com" 53 | source_arn = aws_cloudwatch_event_rule.event_rule.arn 54 | } 55 | 56 | resource "aws_iam_role" "role" { 57 | name = "sc2quotes_lambda_role" 58 | 59 | assume_role_policy = <