-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
70 lines (60 loc) · 2.68 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
LAMBDA_FUNCTION_NAME := lambda_quotes
# The CPU architecture of the machine running the AWS lambda function.
LAMBDA_ARCH ?= arm64
LAMBDA_FUNCTION_ZIPFILE := deployment_package.zip
# Python packages required to run the lambda
PYTHON_PKGS_ZIPFILE_LAYER := layer_package_$(LAMBDA_ARCH).zip
# Root dir where the Python packages gets populated during build
PIP_INSTALL_TARGET_DIR := /tmp/lima/python-pkg-$(LAMBDA_FUNCTION_NAME)/python
# Support creating the Python layer on macOS
ifeq (Darwin,$(shell uname -s))
UID := $(shell id -u)
GID := $(shell id -g)
PIP3_HAL_CMD := lima podman run --arch $(LAMBDA_ARCH) --rm -it -v $(PIP_INSTALL_TARGET_DIR):$(PIP_INSTALL_TARGET_DIR) -u $(UID):$(GID) python:3.11
endif
.PHONY: update-function
update-function: $(LAMBDA_FUNCTION_ZIPFILE)
aws lambda update-function-code \
--function-name $(LAMBDA_FUNCTION_NAME) \
--zip-file fileb://$<
$(LAMBDA_FUNCTION_ZIPFILE): aws_lambda
cd aws_lambda && zip -9 $@ lambda_function.py
mv aws_lambda/$@ .
$(PYTHON_PKGS_ZIPFILE_LAYER): $(PIP_INSTALL_TARGET_DIR)
rm -f $@
cd $(PIP_INSTALL_TARGET_DIR)/.. && zip -9 -r /tmp/$@ $(shell basename $(PIP_INSTALL_TARGET_DIR))
mv /tmp/$@ .
.PHONY: $(PIP_INSTALL_TARGET_DIR)
$(PIP_INSTALL_TARGET_DIR):
if [ -d $(PIP_INSTALL_TARGET_DIR) ]; then rm -r $(PIP_INSTALL_TARGET_DIR); fi
mkdir -p $(PIP_INSTALL_TARGET_DIR)
$(PIP3_HAL_CMD) pip3 install yfinance==0.2.28 --upgrade --no-cache-dir --target $(PIP_INSTALL_TARGET_DIR)
find $(PIP_INSTALL_TARGET_DIR) -type d -name "__pycache__" | xargs -I _ rm -r _
patch -p1 -d $(PIP_INSTALL_TARGET_DIR)/yfinance < yfinance-patches.diff
.PHONY: update-layer
update-layer: $(PYTHON_PKGS_ZIPFILE_LAYER)
aws lambda publish-layer-version \
--layer-name yfinance \
--zip-file fileb://$<
@echo "Delete the old layer version with\n" \
"> aws delete-layer-version --version-number VERSION"
@echo "Update the function to use the updated version with\n" \
"> aws update-function-configuration --function-name $(LAMBDA_FUNCTION_NAME) --layers arn:aws:lambda:REGION::layer:yfinance:VERSION # (i.e. LayerVersionArn from above)\n" \
"or via the console."
clean:
rm -f $(LAMBDA_FUNCTION_ZIPFILE) $(PYTHON_PKGS_ZIPFILE_LAYER)
if [ -d $(PIP_INSTALL_TARGET_DIR) ]; then rm -r $(PIP_INSTALL_TARGET_DIR); fi
.PHONY: lambda-logs
lambda-logs:
get_log_stream() { \
local -r log_stream_name="$$(aws logs describe-log-streams \
--log-group-name '/aws/lambda/$(LAMBDA_FUNCTION_NAME)' \
--query 'logStreams[*].logStreamName' | \
jq -r '.[-1]'\
)"; \
aws logs get-log-events \
--log-group-name '/aws/lambda/$(LAMBDA_FUNCTION_NAME)' \
--log-stream-name "$${log_stream_name}" | \
jq -r '.events | .[].message' | \
sed '/^$$/d'; \
}; get_log_stream