Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
aligned Makefile with other multi-func projects
  • Loading branch information
moritzzimmer committed Feb 1, 2024
commit 11fee0f608444eeed5d5616fe64e4ff022dd9a6c
62 changes: 47 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
SERVICE := image-handler
REGION ?= eu-west-1
APP_SUFFIX ?=
MODE ?= plan
Expand All @@ -9,47 +8,80 @@ VERSION = $(eval VERSION := $$(shell git rev-parse --short HEAD))$(VERSION)

TF_BACKEND_CFG = $(eval TF_BACKEND_CFG := -backend-config=bucket=terraform-state-$(ACCOUNT_ID)-$(REGION) \
-backend-config=region=$(REGION) \
-backend-config=key="regional/lambda/$(SERVICE)/terraform$(APP_SUFFIX).tfstate")$(TF_BACKEND_CFG)
-backend-config=key="regional/lambda/$(FUNC)/terraform$(APP_SUFFIX).tfstate")$(TF_BACKEND_CFG)

TF_VARS = $(eval TF_VARS := -var="region=$(REGION)" -var="account_id=$(ACCOUNT_ID)" -var="app_suffix=$(APP_SUFFIX)")$(TF_VARS)
TF_FOLDERS := $(shell find . -not -path "*/\.*" -iname "*.tf" | sed -E "s|/[^/]+$$||" | sort --unique)

WORK_DIR := source/$(SERVICE)
WORK_DIR := source/$(FUNC)

check-func:
ifndef FUNC
$(error FUNC is undefined)
endif

all :: build tf

clean ::
@echo "+ $@"
@cd $(WORK_DIR) && rm -rf ./dist/ ./node_modules/

npm/install ::
@echo "+ $@"
cd $(WORK_DIR) && npm install --cpu=arm64 --os=linux --libc=musl

npm/test ::
@echo "+ $@"
cd $(WORK_DIR) && npm run test

build ::
.PHONY: build
build: check-func ## Builds the `FUNC`
@echo "+ $@"
cd $(WORK_DIR) && if [ -f 'package.json' ] ; then \
npm run test && npm run build ; \
elif [ -f 'Cargo.toml' ] ; then \
cargo lambda build --arm64 --release --output-format zip --lambda-dir target/lambda/arm64 ; \
else \
echo 'Unknown SERVICE/Build: $(SERVICE). Aborting.' ; exit 1 ; \
echo 'Unknown FUNC/Build: $(FUNC). Aborting.' ; exit 1 ; \
fi

tf ::
tf: check-func ## Runs `terraform` for the `FUNC`
rm -f $(WORK_DIR)/terraform/.terraform/terraform.tfstate || true
terraform -chdir=$(WORK_DIR)/terraform init $(TF_VARS) -reconfigure -upgrade=$(DO_TF_UPGRADE) $(TF_BACKEND_CFG)
if [ "true" == "$(DO_TF_UPGRADE)" ]; then terraform -chdir=$(WORK_DIR)/terraform providers lock -platform=darwin_amd64 -platform=darwin_arm64 -platform=linux_amd64; fi
terraform -chdir=$(WORK_DIR)/terraform $(MODE) $(TF_VARS)

invoke :: # invoke the running docker lambda by posting a sample API-GW-Event
@echo nothing to do


upload :: # upload/push the app to production (given sufficient permissions)
if [ "image-handler" = "$(SERVICE)" ] ; then \
.PHONY: deploy
deploy: check-func build ## Uploads the `FUNC` to start CodePipeline deployment
@echo "+ $@"
if [ "image-handler" = "$(FUNC)" ] ; then \
aws s3 cp $(WORK_DIR)/dist/image-handler.zip s3://ci-$(ACCOUNT_ID)-$(REGION)/image-handler/image-handler$(APP_SUFFIX).zip ; \
elif [ "thumbs" = "$(SERVICE)" ] ; then \
elif [ "thumbs" = "$(FUNC)" ] ; then \
aws s3 cp $(WORK_DIR)/target/lambda/arm64/thumbs/bootstrap.zip s3://ci-$(ACCOUNT_ID)-$(REGION)/image-thumbs/image-thumbs$(APP_SUFFIX).zip ; \
else \
echo "Unknown SERVICE/Upload: $(SERVICE). Aborting." ; exit 1 ; \
echo "Unknown FUNC/Upload: $(FUNC). Aborting." ; exit 1 ; \
fi

all :: build tf
.PHONY: help
help: ## Display this help screen
@grep -E '^[0-9a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

.PHONY: providers
providers: update ## Upgrades all providers and platform independent dependency locks (slow)
@echo "+ $@"
@for f in $(TF_FOLDERS) ; do \
echo upgrading: $$f ;\
terraform -chdir=$$f init -upgrade=true -backend=false;\
terraform -chdir=$$f providers lock -platform=darwin_amd64 -platform=darwin_arm64 -platform=linux_amd64 ;\
done

.PHONY: update
update: ## Upgrades Terraform core, providers and modules constraints recursively using https://github.com/minamijoyo/tfupdate
@echo "+ $@"
@command -v tfupdate >/dev/null 2>&1 || { echo >&2 "Please install tfupdate: 'brew install minamijoyo/tfupdate/tfupdate'"; exit 1; }
@tfupdate terraform -v "~> 1" -r .
@tfupdate module -v "7.5.0" registry.terraform.io/moritzzimmer/lambda/aws -r .
@tfupdate module -v "7.5.0" registry.terraform.io/moritzzimmer/lambda/aws//modules/deployment -r .
@tfupdate provider aws -v "~> 5" -r .
@tfupdate provider opensearch -v "~> 2" -r .

8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ The following environment variables are used by the image-handler:

To build the package run:

```make build```
```make FUNC=image-handler build```

### Testing

Expand All @@ -79,7 +79,7 @@ Run tests using the following Make command:

Deploy the infrastructure using Terraform with the following Make command:

```make tf```
```make FUNC=image-handler tf```

## Image Thumbs

Expand All @@ -95,7 +95,7 @@ Image-thumbs is an implementation of [thumbhash](https://evanw.github.io/thumbha

To build the package run:

```make SERVICE=thumbs build```
```make FUNC=thumbs build```

### Test / Invoke

Expand All @@ -112,4 +112,4 @@ cargo lambda invoke --data-file apigw-request.json

To deploy the infrastructure run:

```make SERVICE=thumb tf```
```make FUNC=thumb tf```
2 changes: 1 addition & 1 deletion source/image-handler/terraform/backend.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ terraform {
}
}

required_version = "~> 1.0"
required_version = "~> 1"
}
2 changes: 1 addition & 1 deletion source/thumbs/terraform/backend.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ terraform {
}
}

required_version = "~> 1.0"
required_version = "~> 1"
}
2 changes: 1 addition & 1 deletion source/thumbs/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module "lambda" {

architectures = ["arm64"]
layers = [
"arn:aws:lambda:${var.region}:${var.account_id}:layer:CustomLoggingExtensionOpenSearch-Arm64:10"
"arn:aws:lambda:${var.region}:${var.account_id}:layer:CustomLoggingExtensionOpenSearch-Arm64:11"
]
cloudwatch_logs_enabled = false
description = "provider of cute kitty thumbs."
Expand Down