terraform-aws-ovpn-server/Makefile

90 lines
3.6 KiB
Makefile

BACKEND_CONFIG:=backend.tfvars
TERRAFORM_PLAN:=out.tfplan
OVPN_CONFIG:=aws_vpn.ovpn
DEPLOYED_REGION_FILE:=.deployed_region
AWS_REGIONS_FILE:=aws_regions.txt
INSTANCE_SIZE ?= micro
PUBLIC_IP ?= $(shell curl -s ifconfig.me)
## AWS_REGIONS_FILE must contain a list of AWS regions, in one line, separated by spaces
REGIONS := $(shell head -n 1 $(AWS_REGIONS_FILE))
## check if DEPLOYED_REGION_FILE exists, if it does, REGION must not be set
DEPLOYED_REGION_FILE_EXISTS := $(wildcard $(DEPLOYED_REGION_FILE))
ifneq ($(DEPLOYED_REGION_FILE_EXISTS),)
ifdef REGION
$(error REGION is manually set but $(DEPLOYED_REGION_FILE) exists, please destroy the existing deployment first)
endif
endif
## if REGION is not set, select a random region from AWS_REGIONS_FILE
REGION ?= $(word $(shell echo $$((RANDOM%$(words $(REGIONS))+1))), $(REGIONS))
## TERM COLORS
GREEN=\033[0;32m
RED=\033[0;31m
YELLOW=\033[0;33m
NC=\033[0m
all: help
.PHONY: help
## print help
help:
@awk '/^#/{c=substr($$0,3);next}c&&/^[[:alpha:]][[:alnum:]_-]+:/{print substr($$1,1,index($$1,":")),c}1{c=0}' $(MAKEFILE_LIST) | column -s: -t
.PHONY: init
## init terraform backend and providers
init: .terraform.lock.hcl
.terraform.lock.hcl: $(BACKEND_CONFIG) providers.tf
ifndef WORKSPACE
$(error WORKSPACE is not set)
endif
@rm -f $@
@terraform init -reconfigure -backend-config $(BACKEND_CONFIG)
@(terraform workspace new $(WORKSPACE) || terraform workspace select $(WORKSPACE))
.PHONY: plan
## plan terraform deployment in random region or REGION if set
plan: init $(TERRAFORM_PLAN)
$(TERRAFORM_PLAN): $(wildcard *.tf) $(DEPLOYED_REGION_FILE)
@echo "prepare deployment to region ${GREEN}$(shell head -n 1 $(DEPLOYED_REGION_FILE))${NC}..."
@terraform get -update
@terraform plan -var="region=$(shell head -n 1 $(DEPLOYED_REGION_FILE))" -var="ovpn_config_file=$(OVPN_CONFIG)" -var="instance_size=$(INSTANCE_SIZE)" -var="public_ip=$(PUBLIC_IP)" -input=false -out=$@
$(DEPLOYED_REGION_FILE):
@export SELECTED_REGION=$(REGION) && echo $$SELECTED_REGION > $@ && echo "selected region: ${GREEN}$$SELECTED_REGION${NC}"
@echo "deployment region saved to ${GREEN}$(DEPLOYED_REGION_FILE)${NC} file"
@echo "⚠️ ${YELLOW}warning:${NC} do not edit this file manually or delete it ⚠️"
.PHONY: deploy
## deploy the server according to the plan
deploy: plan
@echo "deploy server in region ${GREEN}$(shell head -n 1 $(DEPLOYED_REGION_FILE))${NC}..."
# always remove plan file after apply because... bug ? => always considered as stale if re-applied
@(terraform apply -input=false $(TERRAFORM_PLAN) && rm $(TERRAFORM_PLAN)) || (rm $(TERRAFORM_PLAN) && exit 1)
@cp $(OVPN_CONFIG) ~/Desktop/$(OVPN_CONFIG)
.PHONY: destroy
## destroy the server deployment
destroy: init $(DEPLOYED_REGION_FILE)
# allow destroying mannually selected REGION if deployed region file is accidentally deleted
ifneq ($(DEPLOYED_REGION_FILE_EXISTS),)
@echo "destroy server in region ${GREEN}$(shell head -n 1 $(DEPLOYED_REGION_FILE))${NC}"
@terraform destroy -auto-approve -var="region=$(shell head -n 1 $(DEPLOYED_REGION_FILE))" -var="ovpn_config_file=$(OVPN_CONFIG)" -var="instance_size=$(INSTANCE_SIZE)" -var="public_ip=$(PUBLIC_IP)"
else
@echo "destroy server in region ${GREEN}$(REGION)${NC}"
@terraform destroy -auto-approve -var="region=$(REGION)" -var="ovpn_config_file=$(OVPN_CONFIG)" -var="instance_size=$(INSTANCE_SIZE)" -var="public_ip=$(PUBLIC_IP)"
endif
@rm -f $(TERRAFORM_PLAN) $(OVPN_CONFIG) $(DEPLOYED_REGION_FILE)
.PHONY: clean
## clean builds and plan
clean:
@rm -rf $(TERRAFORM_PLAN) $(OVPN_CONFIG) .terraform .terraform.lock.hcl
.PHONY: format
## format terraform code
format:
terraform fmt -recursive