-
Create a workspace on your local, and prepare python lib if not included in AWS sdk
pip install requests -t your_workspace_pathFor mac user, you may need to add a
setup.cfgfile to fix error. -
Copy script of
lambda_function.py, do remember to change username/email settings. -
zipcontents (including dependency lib, e.g.requestshere), pay attention to the root should be current path. -
Sign up AWS on your region (it's free for first 1-million lambda call per month, perhaps enough for you). Vist lambda in products or here's a fast link.
-
Create function as following:
- Name: Any as you wish
- Description: (optional)
- Runtime: Python 2.7
- Code entry type: Upload a .ZIP file
- Handler:
lambda_function.lambda_handler, orYOUR_FILE_NAME.YOUR_FUNCTION_NAME - Role: if you don't have one, select Basic execution rule to create a new one
- Memory: could be default, for Python, >32M is recommended
- Timeout: considering your http timeout, could be 30s.
- VPC: (optional)
Click Create Function after you finished.
-
Perform a test:
- Click Test button, and select Scheduled Event from Sample event template dropdown. You may modify the json object if you want.
- Click Save and Test, check output of script
-
Add trigger from EventSource:
- Click Event Sources in navigation menu
- Click Add Event Soures, and select CloudWatch Event - Schedule in dropdown list
- Set Schedule expression to 1day or other feqeuency, do Submit to finish trigger.
Created
June 12, 2016 12:00
-
-
Save willings/dbd1593eb665e8f9f411d7dee054955d to your computer and use it in GitHub Desktop.
StackOverflow login handler for AWS lambda
This file contains hidden or 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
| # -*- coding: utf-8 -*- | |
| """Example of Performing HTTP Request using lambda | |
| This module gives a simple example of using AWS lambda to login into stackoverflow.com periodically. | |
| Attributes: | |
| USER_NAME(str): found by clicking your gravatar on toolbar of StackOverflow.com. | |
| USER_ID(int): numeric part of the url. | |
| USER_EMAIL(str): your email. | |
| USER_PASSORD(str): your password, oauth is not supported yet. | |
| """ | |
| from __future__ import print_function | |
| from datetime import datetime as dt | |
| import requests | |
| import re | |
| USER_NAME = 'newbie' | |
| USER_ID = 8000000 | |
| USER_EMAIL = '[email protected]' | |
| USER_PASSWD = 'password' | |
| URL_BASE = 'https://stackoverflow.com' | |
| def http_get_fkey(session): | |
| """Get `fkey` form value from html | |
| This value is used by login posting and cookies (sometimes) | |
| :param session: shared session, all http request should be handled in same session | |
| :return: `fkey` value if success | |
| """ | |
| url_login = URL_BASE + '/users/login?ssrc=head&returnurl=http%3a%2f%2fstackoverflow.com' | |
| request = session.get(url_login) | |
| m = re.search('\<input\stype=\"hidden\"\sname=\"fkey\"\svalue=\"(\w+)\"\>', request.text) | |
| return m.group(1) | |
| def http_post_login(session, fkey, email, password): | |
| """Post Email/Password/fkey to finish a login | |
| :param session: shared session, all http request should be handled in same session | |
| :param fkey: value retrieved from http_get_fkey | |
| :param email: USER_EMAIL | |
| :param password: USER_PASSWD | |
| :return: None | |
| """ | |
| post_form = { | |
| 'ssrc': 'head', | |
| 'fkey': fkey, | |
| 'email': email, | |
| 'password': password | |
| } | |
| url_login = URL_BASE + '/users/login?ssrc=head&returnurl=http%3a%2f%2fstackoverflow.com' | |
| session.post(url_login, data=post_form, allow_redirects=True) | |
| def http_get_validate(session): | |
| """Validate login result by GET profile edit page | |
| :param session: shared session, all http request should be handled in same session | |
| :return: True if login successfully | |
| """ | |
| url_edit = URL_BASE + '/users/edit/' + str(USER_ID) | |
| request = session.get(url_edit) | |
| for i in range(0, 3): | |
| if request.status_code == 200: | |
| return True, 'Login validated successfully.' | |
| return False, 'Login error, status code: {}'.format(request.status_code) | |
| def login_chain(email, password): | |
| session = requests.Session() | |
| fkey = http_get_fkey(session) | |
| http_post_login(session, fkey, email, password) | |
| return http_get_validate(session) | |
| def time_label(): | |
| return dt.now().strftime("%Y%m%d %H:%M:%S") | |
| def lambda_handler(event, context): | |
| """Login handler called by AWS lambda | |
| :param event: | |
| :param context: | |
| :return: | |
| """ | |
| print("[{}] Handle incomming event: {}".format(time_label(), event['time'])) | |
| status, message = login_chain(USER_EMAIL, USER_PASSWD) | |
| print("[{}] {}".format(time_label(), message)) | |
| if not status: | |
| raise message |
This file contains hidden or 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
| [install] | |
| prefix= |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment