Skip to content

Commit

Permalink
Merge pull request #5 from MITLibraries/ADR
Browse files Browse the repository at this point in the history
Update Lambda Handler Name
  • Loading branch information
cabutlermit committed Jan 24, 2024
2 parents 00ee988 + d5acd48 commit 47c24a8
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 12 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/prod-cf-deploy-lambda.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
### This is the Terraform-generated GitHub Actions workflow for cf-lambda ###
### applications for dev/stage/prod. It will publish the Lambda zip to S3 ###
### and automatically run Terraform in the Terraform Cloud ###
### workloads-libraries-website-prod workspace. It will NOT auto-apply in ###
### Terraform Cloud; rather, TfC will wait for human input to either cancel ###
### or apply the result. ###
name: Prod CF Lambda@Edge Full Deploy

on:
workflow_dispatch:
release:
types: [published]

defaults:
run:
shell: bash

jobs:
deploy-lambda:
if: github.ref == 'refs/heads/main'
name: Push zip to S3 and Deploy CloudFront Distribution
uses: mitlibraries/.github/.github/workflows/cf-lambda-shared-deploy.yml@main
secrets: inherit
with:
AWS_REGION: us-east-1
ENVIRONMENT: prod
GHA_ROLE: ledge-custom-domain-gha-prod
TF_AUTO_APPLY: false
TF_WORKSPACE: workloads-libraries-website-prod
UPLOAD_ZIP: true
24 changes: 24 additions & 0 deletions docs/adrs/01-pick-lamba-handler-name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 1. Pick the Custom Domain Lambda Handler Name

Date: 2024-01-19

## Status

Accepted

## Context

Per the AWS documentation for [Lambda function handlers in Python](https://docs.aws.amazon.com/lambda/latest/dg/python-handler.html?icmpid=docs_lambda_help), we need to ensure that what Terraform defines for the handler when it create the Lambda function matches up with what the developer uses for a file name and function name in the dependent application repository.

See ADR#8 in [mitlib-tf-workloads-libraries-website](https://github.com/MITLibraries/mitlib-tf-workloads-libraries-website) which says the exact same thing.

## Decision

1. The "custom domain" Lambda function handler is `handler`.
2. The "custom domain" Lambda function filename in which the handler function lives is `lambda_edge.py`.

The name of the handler that is used in the `resource "aws_lambda_function" "lambda_edge_custom_domain" {}` resource is `lambda_edge.handler`.

## Consequences

If anyone decides to rename Python files in this repo, then the definition of the Lambda function in the [mitlib-tf-workloads-libraries-website](https://github.com/MITLibraries/mitlib-tf-workloads-libraries-website) repo might need to be udpated.
2 changes: 1 addition & 1 deletion lambdas/lambda_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
logger.setLevel(logging.INFO)


def lambda_handler(event, context):
def handler(event, context):
## Capture the request header
request = event["Records"][0]["cf"]["request"]
## The following lines are left here but commented out for any future debugging
Expand Down
2 changes: 1 addition & 1 deletion tests/test_lambda_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

def test_lambda_edge():
with open("tests/fixtures/s3-origin-request.json", encoding="utf-8") as json_file:
assert lambda_edge.lambda_handler(json.load(json_file), "context")
assert lambda_edge.handler(json.load(json_file), "context")
8 changes: 4 additions & 4 deletions tests/test_lambda_edge_cdn_fol.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

def test_lambda_edge_dev():
with open("tests/fixtures/origin-event-fol-dev.json", encoding="utf-8") as json_file:
lambda_response = lambda_edge.lambda_handler(json.load(json_file), "context")
lambda_response = lambda_edge.handler(json.load(json_file), "context")
assert lambda_response["origin"]["s3"]["path"] == "/fol"
assert lambda_response["uri"] == "/index.html"

Expand All @@ -17,7 +17,7 @@ def test_lambda_edge_stage():
with open(
"tests/fixtures/origin-event-fol-stage.json", encoding="utf-8"
) as json_file:
lambda_response = lambda_edge.lambda_handler(json.load(json_file), "context")
lambda_response = lambda_edge.handler(json.load(json_file), "context")
assert lambda_response["origin"]["s3"]["path"] == "/fol"
assert lambda_response["uri"] == "/index.html"

Expand All @@ -26,7 +26,7 @@ def test_lambda_edge_prod_1():
with open(
"tests/fixtures/origin-event-fol-prod-1.json", encoding="utf-8"
) as json_file:
lambda_response = lambda_edge.lambda_handler(json.load(json_file), "context")
lambda_response = lambda_edge.handler(json.load(json_file), "context")
assert lambda_response["origin"]["s3"]["path"] == "/fol"
assert lambda_response["uri"] == "/index.html"

Expand All @@ -35,6 +35,6 @@ def test_lambda_edge_prod_2():
with open(
"tests/fixtures/origin-event-fol-prod-2.json", encoding="utf-8"
) as json_file:
lambda_response = lambda_edge.lambda_handler(json.load(json_file), "context")
lambda_response = lambda_edge.handler(json.load(json_file), "context")
assert lambda_response["origin"]["s3"]["path"] == "/fol"
assert lambda_response["uri"] == "/index.html"
4 changes: 2 additions & 2 deletions tests/test_lambda_edge_cdn_gc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_lambda_edge_prod_1():
with open(
"tests/fixtures/origin-event-gc-prod-1.json", encoding="utf-8"
) as json_file:
lambda_response = lambda_edge.lambda_handler(json.load(json_file), "context")
lambda_response = lambda_edge.handler(json.load(json_file), "context")
assert lambda_response["origin"]["s3"]["path"] == "/grandchallenges"
assert lambda_response["uri"] == "/index.html"

Expand All @@ -19,6 +19,6 @@ def test_lambda_edge_prod_2():
with open(
"tests/fixtures/origin-event-gc-prod-2.json", encoding="utf-8"
) as json_file:
lambda_response = lambda_edge.lambda_handler(json.load(json_file), "context")
lambda_response = lambda_edge.handler(json.load(json_file), "context")
assert lambda_response["origin"]["s3"]["path"] == "/grandchallenges"
assert lambda_response["uri"] == "/index.html"
8 changes: 4 additions & 4 deletions tests/test_lambda_edge_cdn_standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

def test_lambda_edge_dev():
with open("tests/fixtures/origin-event-cdn-dev.json", encoding="utf-8") as json_file:
lambda_response = lambda_edge.lambda_handler(json.load(json_file), "context")
lambda_response = lambda_edge.handler(json.load(json_file), "context")
assert lambda_response["origin"]["s3"]["path"] == ""
assert lambda_response["uri"] == "/index.html"

Expand All @@ -17,7 +17,7 @@ def test_lambda_edge_stage():
with open(
"tests/fixtures/origin-event-cdn-stage.json", encoding="utf-8"
) as json_file:
lambda_response = lambda_edge.lambda_handler(json.load(json_file), "context")
lambda_response = lambda_edge.handler(json.load(json_file), "context")
assert lambda_response["origin"]["s3"]["path"] == ""
assert lambda_response["uri"] == "/index.html"

Expand All @@ -26,7 +26,7 @@ def test_lambda_edge_prod_1():
with open(
"tests/fixtures/origin-event-cdn-prod-1.json", encoding="utf-8"
) as json_file:
lambda_response = lambda_edge.lambda_handler(json.load(json_file), "context")
lambda_response = lambda_edge.handler(json.load(json_file), "context")
assert lambda_response["origin"]["s3"]["path"] == ""
assert lambda_response["uri"] == "/index.html"

Expand All @@ -35,6 +35,6 @@ def test_lambda_edge_prod_2():
with open(
"tests/fixtures/origin-event-cdn-prod-2.json", encoding="utf-8"
) as json_file:
lambda_response = lambda_edge.lambda_handler(json.load(json_file), "context")
lambda_response = lambda_edge.handler(json.load(json_file), "context")
assert lambda_response["origin"]["s3"]["path"] == ""
assert lambda_response["uri"] == "/index.html"

0 comments on commit 47c24a8

Please sign in to comment.