├── .gitignore ├── .terraform-version ├── LICENSE ├── README.md ├── infrastructure ├── init.tf ├── main.tf └── variables.tf ├── resources.png └── src ├── lambda_function.py └── packages.zip /.gitignore: -------------------------------------------------------------------------------- 1 | /src/python/ 2 | /infrastructure/tmp/ 3 | -------------------------------------------------------------------------------- /.terraform-version: -------------------------------------------------------------------------------- 1 | 1.4.6 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Anton Bagaiev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP daily 2 | 3 | PHP daily is a daily function, piece of news, library or just a tip. 4 | 5 | To post daily updates it uses AWS Lambda on a Python which is triggered by the CloudWatch event rule: 6 | 7 | ![Resources scheme](./resources.png) 8 | 9 | ### Folders structure 10 | 11 | - [src](./src): lambda function code and packages 12 | - [infrastructure](./infrastructure): terraform code to deploy function and its infrastructure 13 | 14 | ### Q&A 15 | 16 | **How much does it costs?** Near 2 cents per 1 month. 17 | 18 | **Why python?** Because AWS doesn't have native support of PHP and it's just fun. 19 | 20 | **Why event rule is triggered every 23 hours?** Not to be stuck in one timezone. 21 | 22 | ## Also 23 | 24 | Feel free to add comments, issues, pull requests or just follow this twitter: 25 | [https://twitter.com/phpdaily_](https://twitter.com/phpdaily_) 26 | -------------------------------------------------------------------------------- /infrastructure/init.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | aws = { 4 | source = "hashicorp/aws" 5 | version = "~> 4.0" 6 | } 7 | } 8 | } 9 | 10 | provider "aws" { 11 | region = "eu-central-1" 12 | profile = "dontgiveafish" 13 | } 14 | 15 | terraform { 16 | backend "remote" { 17 | organization = "dontgiveafish" 18 | 19 | workspaces { 20 | name = "phpdaily_" 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /infrastructure/main.tf: -------------------------------------------------------------------------------- 1 | # Data 2 | 3 | data "aws_iam_policy_document" "lambda_role_template" { 4 | statement { 5 | actions = [ 6 | "sts:AssumeRole" 7 | ] 8 | 9 | principals { 10 | type = "Service" 11 | identifiers = [ 12 | "lambda.amazonaws.com" 13 | ] 14 | } 15 | } 16 | } 17 | 18 | data "aws_iam_policy_document" "lambda_policy_template" { 19 | statement { 20 | actions = [ 21 | "logs:CreateLogGroup", 22 | ] 23 | resources = [ 24 | "arn:aws:logs:*:*", 25 | ] 26 | } 27 | 28 | statement { 29 | actions = [ 30 | "logs:CreateLogStream", 31 | "logs:PutLogEvents", 32 | ] 33 | resources = [ 34 | "arn:aws:logs:*:*:*:*:*", 35 | ] 36 | } 37 | 38 | statement { 39 | actions = [ 40 | "ec2:DescribeNetworkInterfaces", 41 | "ec2:CreateNetworkInterface", 42 | "ec2:DeleteNetworkInterface", 43 | "ec2:DescribeInstances", 44 | "ec2:AttachNetworkInterface", 45 | ] 46 | resources = [ 47 | "*", 48 | ] 49 | } 50 | 51 | } 52 | 53 | data "archive_file" "lambda_zip" { 54 | type = "zip" 55 | source_file = "../src/lambda_function.py" 56 | output_path = "tmp/lambda.zip" 57 | } 58 | 59 | # IAM 60 | 61 | resource "aws_iam_role" "lambda_role" { 62 | name = format("%s-role", var.lambda_name) 63 | assume_role_policy = data.aws_iam_policy_document.lambda_role_template.json 64 | } 65 | 66 | resource "aws_iam_policy" "lambda_policy" { 67 | name = format("%s-policy", var.lambda_name) 68 | path = "/" 69 | description = format("%s lambda policy", var.lambda_name) 70 | policy = data.aws_iam_policy_document.lambda_policy_template.json 71 | } 72 | 73 | resource "aws_iam_role_policy_attachment" "lambda_policy_attach" { 74 | role = aws_iam_role.lambda_role.name 75 | policy_arn = aws_iam_policy.lambda_policy.arn 76 | } 77 | 78 | # Lambda layers and function 79 | 80 | resource "aws_lambda_layer_version" "packages_layer" { 81 | filename = "../src/packages.zip" 82 | layer_name = format("layer-packages-%s", var.lambda_name) 83 | source_code_hash = filebase64sha256("../src/packages.zip") 84 | 85 | compatible_runtimes = [ 86 | "python3.8" 87 | ] 88 | } 89 | 90 | resource "aws_lambda_function" "lambda" { 91 | filename = data.archive_file.lambda_zip.output_path 92 | function_name = var.lambda_name 93 | role = aws_iam_role.lambda_role.arn 94 | handler = var.lambda_handler 95 | source_code_hash = data.archive_file.lambda_zip.output_base64sha256 96 | 97 | timeout = var.lambda_timeout 98 | runtime = "python3.8" 99 | 100 | environment { 101 | variables = { 102 | TWITTER_CONSUMER_KEY = var.twitter_consumer_key 103 | TWITTER_CONSUMER_SECRET = var.twitter_consumer_secret 104 | TWITTER_ACCESS_TOKEN = var.twitter_access_token 105 | TWITTER_ACCESS_SECRET = var.twitter_access_secret 106 | } 107 | } 108 | 109 | layers = [ 110 | aws_lambda_layer_version.packages_layer.arn 111 | ] 112 | } 113 | 114 | # Cloudwatch schedule 115 | 116 | resource "aws_cloudwatch_event_rule" "lambda_event_rule" { 117 | name = format("%s-event-rule", var.lambda_handler) 118 | description = format("%s event rule", var.lambda_name) 119 | schedule_expression = var.lambda_schedule 120 | } 121 | 122 | resource "aws_cloudwatch_event_target" "lambda_event_target" { 123 | rule = aws_cloudwatch_event_rule.lambda_event_rule.name 124 | target_id = "lambda" 125 | arn = aws_lambda_function.lambda.arn 126 | } 127 | 128 | resource "aws_lambda_permission" "lambda_cloudwatch_permission" { 129 | statement_id = "AllowExecutionFromCloudWatch" 130 | action = "lambda:InvokeFunction" 131 | function_name = aws_lambda_function.lambda.function_name 132 | principal = "events.amazonaws.com" 133 | source_arn = aws_cloudwatch_event_rule.lambda_event_rule.arn 134 | } 135 | -------------------------------------------------------------------------------- /infrastructure/variables.tf: -------------------------------------------------------------------------------- 1 | variable "lambda_name" { 2 | description = "PHP daily lambda resource name" 3 | type = string 4 | default = "lambda-phpdaily" 5 | } 6 | 7 | variable "lambda_handler" { 8 | description = "PHP daily lambda handler" 9 | type = string 10 | default = "lambda_function.lambda_handler" 11 | } 12 | 13 | variable "lambda_timeout" { 14 | description = "PHP daily lambda timeout" 15 | type = number 16 | default = 60 17 | } 18 | 19 | variable "lambda_schedule" { 20 | description = "PHP daily lambda event rule schedule" 21 | type = string 22 | default = "rate(23 hours)" 23 | } 24 | 25 | variable "twitter_consumer_key" { 26 | description = "API key that a service provider to identify the consumer" 27 | type = string 28 | } 29 | 30 | variable "twitter_consumer_secret" { 31 | description = "Secret that is used to request access to a user's resources from a service provider" 32 | type = string 33 | } 34 | 35 | variable "twitter_access_token" { 36 | description = "Token that defines the access privileges of the consumer over a particular user's resources" 37 | type = string 38 | } 39 | 40 | variable "twitter_access_secret" { 41 | description = "Secret that is used with access token to access user's resources" 42 | type = string 43 | } 44 | -------------------------------------------------------------------------------- /resources.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abagayev/phpdaily_/ce88393fa31da40c8ba812777869a13a56e1b49d/resources.png -------------------------------------------------------------------------------- /src/lambda_function.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import random 3 | import scrapy 4 | import tweepy 5 | import os 6 | 7 | 8 | def describe_function(): 9 | parse_url = 'https://www.php.net/manual/en/indexes.functions.php' 10 | manual_url = 'https://www.php.net/manual/en' 11 | 12 | # download index and init selector 13 | 14 | data = requests.get(parse_url) 15 | selector = scrapy.Selector(text=data.content) 16 | 17 | # grab a collection of functions 18 | 19 | functions = [] 20 | 21 | for li in selector.css('.index-for-refentry li li'): 22 | function = li.css('li ::text').get() 23 | if any(x in function for x in ('::', '\\')): 24 | continue 25 | 26 | description = li.css('li::text').get().lstrip(' - ') 27 | href = li.css("a::attr(href)").get() 28 | 29 | functions.append({ 30 | 'function': function, 31 | 'description': description, 32 | 'href': href, 33 | }) 34 | 35 | # make a tweet 36 | 37 | choice = random.choice(functions) 38 | tweet = '{}: {} {}/{}'.format( 39 | choice.get('function'), choice.get('description'), manual_url, choice.get('href')) 40 | 41 | return tweet 42 | 43 | 44 | def describe_package(): 45 | parse_url = 'https://packagist.org/explore/popular?page={}' 46 | manual_url = 'https://packagist.org' 47 | 48 | # download index and init selector 49 | 50 | page = random.randint(1, 100) 51 | data = requests.get(parse_url.format(page)) 52 | selector = scrapy.Selector(text=data.content) 53 | 54 | # grab a collection of packages 55 | 56 | packages = [] 57 | 58 | for li in selector.css('ul.packages li'): 59 | package = li.css('a::text').get() 60 | description = li.css('h4 ~ p ::text').get() 61 | href = li.css("a::attr(href)").get().lstrip('/') 62 | 63 | packages.append({ 64 | 'package': package, 65 | 'description': description, 66 | 'href': href, 67 | }) 68 | 69 | # make a tweet 70 | 71 | choice = random.choice(packages) 72 | tweet = '{}: {} {}/{}'.format( 73 | choice.get('package'), choice.get('description'), manual_url, choice.get('href')) 74 | 75 | return tweet 76 | 77 | 78 | def post_tweet(tweet): 79 | api = tweepy.Client(consumer_key=os.environ['TWITTER_CONSUMER_KEY'], 80 | consumer_secret=os.environ['TWITTER_CONSUMER_SECRET'], 81 | access_token=os.environ['TWITTER_ACCESS_TOKEN'], 82 | access_token_secret=os.environ['TWITTER_ACCESS_SECRET']) 83 | 84 | api.create_tweet(text=tweet) 85 | 86 | 87 | def lambda_handler(event, context): 88 | fortune = random.randint(1, 100) 89 | if fortune < 70: 90 | tweet = describe_function() 91 | else: 92 | tweet = describe_package() 93 | 94 | post_tweet(tweet) 95 | 96 | 97 | if __name__ == "__main__": 98 | lambda_handler(None, None) -------------------------------------------------------------------------------- /src/packages.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abagayev/phpdaily_/ce88393fa31da40c8ba812777869a13a56e1b49d/src/packages.zip --------------------------------------------------------------------------------