Skip to content

Instantly share code, notes, and snippets.

@jitsejan
Created May 13, 2019 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jitsejan/0df200f8f1c55b6e3b605a6505e4e7c6 to your computer and use it in GitHub Desktop.
Save jitsejan/0df200f8f1c55b6e3b605a6505e4e7c6 to your computer and use it in GitHub Desktop.
Terraform, AWS Lambda and Looker
#!/usr/bin/env bash
export PKG_DIR="python"
export PY_VERSION="python3.7"
printf "\033[1;33m[1/3] Creating packages for Lambda \033[0m\n"
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
LAMBDA_DIR="sources/lambda-functions"
FULL_DIR=${SCRIPT_DIR}/${LAMBDA_DIR}
printf "\033[1;35m> Checking for Lambda functions in ${FULL_DIR} \033[0m\n"
for fldr in ${FULL_DIR}/*
do
printf "\033[1;35m>> Zipping ${fldr} \033[0m\n"
cd ${fldr} && rm -rf ${PKG_DIR} && mkdir -p ${PKG_DIR}
docker run --rm -v $(pwd):/foo -w /foo lambci/lambda:build-${PY_VERSION} \
pip install -r requirements.txt -t ${PKG_DIR} --no-deps
cd ${fldr}/${PKG_DIR}
find . -type d -name '__pycache__' -print0 | xargs -0 rm -rf
rm ${fldr}/lambda.zip && zip --quiet -r ${fldr}/lambda.zip .
cd ${fldr} && zip --quiet -r ${fldr}/lambda.zip lambda.py
rm -rf ${fldr}/${PKG_DIR}
done
cd ${SCRIPT_DIR}
printf "\033[1;33m[2/3] Deploying on AWS\033[0m\n"
terraform apply
printf "\033[1;33m[3/3] Executing the initial run script\033[0m\n"
${PY_VERSION} initial_run.py
""" initial_run.py """
import boto3
PREFIX = 'dev-'
lambda_client = boto3.client("lambda", "eu-west-2")
# Trigger the Lambda functions
for function in [
fun["FunctionName"]
for fun in lambda_client.list_functions()["Functions"]
if fun["FunctionName"].startswith(PREFIX)
]:
print("> Running function `%s`." % function)
response = lambda_client.invoke(FunctionName=function)
print("< Response: %s" % response)
import boto3
import os
import lookerapi as looker
session = boto3.Session()
client = boto3.client("s3")
ssm = boto3.client("ssm")
LOOKER_BASE_URL = "https://marketinvoice.looker.com:19999/api/3.0/"
LOOKER_CLIENT_ID = ssm.get_parameter(Name="LOOKER_CLIENT_ID", WithDecryption=True)[
"Parameter"
]["Value"]
LOOKER_CLIENT_SECRET = ssm.get_parameter(
Name="LOOKER_CLIENT_SECRET", WithDecryption=True
)["Parameter"]["Value"]
def _get_look_from_looker(look_number):
""" Return the result of executing the look """
unauthenticated_client = looker.ApiClient(LOOKER_BASE_URL)
unauthenticated_authApi = looker.ApiAuthApi(unauthenticated_client)
token = unauthenticated_authApi.login(
client_id=LOOKER_CLIENT_ID, client_secret=LOOKER_CLIENT_SECRET
)
client = looker.ApiClient(
LOOKER_BASE_URL, "Authorization", "token " + token.access_token
)
lookApi = looker.LookApi(client)
return lookApi.run_look(look_number, "csv")
def upload_look(look_number):
""" Upload the look to the public S3 bucket """
result = _get_look_from_looker(look_number)
client.put_object(
Bucket="dev-jwat",
Key=f"looker-look-{look_number}.csv",
Body=result,
ACL="public-read",
)
def handler(event, context):
""" Main function """
return upload_look(1234)
certifi==2019.3.9
lookerapi==3.0.0
python-dateutil==2.8.0
six==1.12.0
urllib3==1.24.2
region = "eu-west-2"
variable "handler" {
default = "lambda.handler"
}
variable "region" {}
variable "runtime" {
default = "python3.7"
}
variable "schedule_midnight" {
default = "cron(0 0 * * ? *)"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment