Skip to content

Commit

Permalink
Initial commit (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
aurimasmick authored May 27, 2021
1 parent 440e8be commit 3d86333
Show file tree
Hide file tree
Showing 15 changed files with 517 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[report]
sort = Cover
[run]
omit = src/slacknewsbot/venv/*
6 changes: 6 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[flake8]
max-line-length = 88
ignore = E501,E402,E127
exclude =
venv
.terraform
32 changes: 32 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: CI

on: [push]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7, 3.8]

steps:
- uses: actions/checkout@v2

- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f src/slacknewsbot/requirements.txt ]; then pip install -r src/slacknewsbot/requirements.txt; fi
- name: Lint with flake8
run: |
flake8 --count --show-source --statistics
- name: Test with pytest
run: |
PYTHONPATH=./src pytest --cov=src --cov-branch --cov-report term-missing
116 changes: 116 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
builds/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cachepip install -r src/slacknewsbot/requirements.txt
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/

.vscode/

*.git
*.tfstate
*.backup
*.tfplan
.terraform*
*.zip
*output.txt
secrets*
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
repos:
- repo: git://github.com/antonbabenko/pre-commit-terraform
rev: v1.45.0
hooks:
- id: terraform_fmt
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.PHONY: check test deploy deploy-with-secrets-file

check: ## Run linters
@echo "*** running linters ***"
flake8 --count --show-source --statistics
@echo "*** all linters passing ***"
test: check ## Run tests
@echo "*** running tests ***"
PYTHONPATH=./src pytest --cov=src --cov-branch --cov-report term-missing
@echo "*** all tests passing ***"
deploy: test ## Deploy project when secrets are exported as env variables
@echo "*** running deploy ***"
terraform apply
deploy-with-secrets-file: test ## Deploy project when secrets are stored in secrets.tfvars file
@echo "*** running deploy ***"
terraform apply -var-file=secrets.tfvars
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@


## Create virtualenv
```
python3 -m venv venv
source venv/bin/activate
pip install -r src/slacknewsbot/requirements.txt
```

## Run tests
```
make tests
```

## Deploy function
### Export secrets
```
export TF_VAR_slack_bot_token=XXX
export TF_VAR_ph_api_token=XXX
```
### Deploy
```
make deploy
```
### Deploy with secrets file
```
make deploy-with-secrets-file
```
50 changes: 50 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
provider "aws" {
region = "eu-west-1"
}


module "lambda_function" {
source = "terraform-aws-modules/lambda/aws"
version = "2.2.0"

function_name = var.name
description = "Function to post popular articles to Slack"
handler = "app.lambda_handler"
runtime = "python3.8"
timeout = 30

source_path = "./src/slacknewsbot"

environment_variables = {
SLACK_CHANNEL = var.slack_channel_name
SLACK_BOT_TOKEN = var.slack_bot_token
QUERY_HN = var.query_hn
QUERY_PH = var.query_ph
PH_API_TOKEN = var.ph_api_token
STORIES_NUMBER = var.stories_number
}
allowed_triggers = {
PostToSlack = {
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.cw_cron.arn
}
}
create_current_version_allowed_triggers = false

tags = {
Name = var.name
}
}

# EVENT CONFIG
resource "aws_cloudwatch_event_rule" "cw_cron" {
name = "${name}-lambda-trigger"
description = "Lambda trigge to post to Slack"

schedule_expression = "cron(0 13 * * ? *)"
}

resource "aws_cloudwatch_event_target" "trigger_slack" {
rule = aws_cloudwatch_event_rule.cw_cron.name
arn = module.lambda_function.lambda_function_arn
}
Empty file added src/slacknewsbot/__init__.py
Empty file.
Loading

0 comments on commit 3d86333

Please sign in to comment.