|
| 1 | +.PHONY: init ssh-key plan-vpc plan-subnets plan-gateway plan apply destroy clean |
| 2 | + |
| 3 | +.DEFAULT_GOAL = help |
| 4 | + |
| 5 | +TERRAFORM = "aws-env terraform" |
| 6 | + |
| 7 | +# Hardcoding value of 3 minutes when we check if the plan file is stale |
| 8 | +STALE_PLAN_FILE := `find "tf.out" -mmin -3 | grep -q tf.out` |
| 9 | + |
| 10 | +## Check if tf.out is stale (Older than 2 minutes) |
| 11 | +check-plan-file: |
| 12 | + @if ! ${STALE_PLAN_FILE} ; then \ |
| 13 | + echo "ERROR: Stale tf.out plan file (older than 3 minutes)!"; \ |
| 14 | + exit 1; \ |
| 15 | + fi |
| 16 | + |
| 17 | +## Runs terraform get and terraform init for env |
| 18 | +init: |
| 19 | + $terraform init |
| 20 | + |
| 21 | +## Create ssh key |
| 22 | +ssh-key: |
| 23 | + @ssh-keygen -q -N "" -b 4096 -C "SSH key for vpc-scenario-1 example" -f ./id_rsa |
| 24 | + |
| 25 | +## use 'terraform plan' to 'target' the vpc in the vpc module |
| 26 | +plan-vpc: |
| 27 | + @terraform plan \ |
| 28 | + -target="module.vpc.module.vpc" \ |
| 29 | + -out=tf.out |
| 30 | + |
| 31 | +## use 'terraform plan' to 'target' the public subnets in the vpc module |
| 32 | +plan-subnets: |
| 33 | + @terraform plan \ |
| 34 | + -target="module.vpc.module.public-subnets" \ |
| 35 | + -out=tf.out |
| 36 | + |
| 37 | +## use 'terraform plan' to 'target' the public gateway in the vpc module |
| 38 | +plan-gateway: |
| 39 | + @terraform plan \ |
| 40 | + -target="module.vpc.module.public-gateway" \ |
| 41 | + -out=tf.out |
| 42 | + |
| 43 | +## use 'terraform plan' to map out updates to apply |
| 44 | +plan: |
| 45 | + @terraform plan -out=tf.out |
| 46 | + |
| 47 | +## use 'terraform apply' to apply updates in a 'tf.out' plan file |
| 48 | +apply: check-plan-file |
| 49 | + @terraform apply tf.out |
| 50 | + |
| 51 | +## use 'terraform destroy' to remove all resources from AWS |
| 52 | +destroy: |
| 53 | + @terraform destroy |
| 54 | + |
| 55 | +## rm -rf all files and state |
| 56 | +clean: |
| 57 | + @rm -f tf.out |
| 58 | + @rm -f id_rsa |
| 59 | + @rm -f id_rsa.pub |
| 60 | + @rm -f terraform.tfvars |
| 61 | + @rm -f terraform.*.backup |
| 62 | + @rm -f terraform.tfstate |
| 63 | + |
| 64 | +## Show help screen. |
| 65 | +help: |
| 66 | + @echo "Please use \`make <target>' where <target> is one of\n\n" |
| 67 | + @awk '/^[a-zA-Z\-\_0-9]+:/ { \ |
| 68 | + helpMessage = match(lastLine, /^## (.*)/); \ |
| 69 | + if (helpMessage) { \ |
| 70 | + helpCommand = substr($$1, 0, index($$1, ":")-1); \ |
| 71 | + helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \ |
| 72 | + printf "%-30s %s\n", helpCommand, helpMessage; \ |
| 73 | + } \ |
| 74 | + } \ |
| 75 | + { lastLine = $$0 }' $(MAKEFILE_LIST) |
0 commit comments