Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #17 from AutomationD/master
Browse files Browse the repository at this point in the history
Add Serverless beta, update ECS cmds
  • Loading branch information
AutomationD authored Jun 8, 2020
2 parents ba59d17 + 3b9c603 commit 6f379a1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
37 changes: 25 additions & 12 deletions aws/ecs.mk
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,29 @@ DOCKER_IMAGE_NAME ?= $(NAMESPACE)-$(SVC)
DOCKERFILE ?= Dockerfile
PROJECT_PATH ?= projects/$(SVC)

SERVICE_TASK_DEFINITION_ARN = $(shell cat $(INFRA_DIR)/env/$(ENV)/output.json | $(JQ) -r '.$(shell echo $(SVC) | sed 's/-/_/g')_task_definition_arn.value')
CMD_SERVICE_DEPLOY = @$(ECS) deploy --profile $(AWS_PROFILE) $(ECS_CLUSTER_NAME) $(ECS_SERVICE_NAME) --task $(ECS_TASK_NAME) --image $(SVC) $(DOCKER_REGISTRY)/$(DOCKER_IMAGE_NAME):$(TAG) --diff --rollback
CMD_SERVICE_DOCKER_BUILD = $(DOCKER) build \

ECS_SERVICE_TASK_ID = $(shell $(AWS) ecs --profile $(AWS_PROFILE) run-task --cluster $(NAMESPACE)-$(ENV) --task-definition "$(ECS_SERVICE_TASK_DEFINITION_ARN)" | $(JQ) -r '.tasks[].taskArn' | $(REV) | $(CUT) -d'/' -f1 | $(REV) && sleep 1)
ECS_SERVICE_TASK_DEFINITION_ARN = $(shell cat $(INFRA_DIR)/env/$(ENV)/output.json | $(JQ) -r '.$(shell echo $(SVC) | sed 's/-/_/g')_task_definition_arn.value')

CMD_ECS_SERVICE_DEPLOY = @$(ECS) deploy --profile $(AWS_PROFILE) $(ECS_CLUSTER_NAME) $(ECS_SERVICE_NAME) --task $(ECS_TASK_NAME) --image $(SVC) $(DOCKER_REGISTRY)/$(DOCKER_IMAGE_NAME):$(TAG) --diff --rollback
CMD_ECS_SERVICE_DOCKER_BUILD = $(DOCKER) build \
. \
-t $(DOCKER_IMAGE_NAME) \
-t $(DOCKER_REGISTRY)/$(DOCKER_IMAGE_NAME):$(TAG) \
-f $(PROJECT_PATH)/$(DOCKERFILE) \
--build-arg PROJECT_PATH=$(PROJECT_PATH)

CMD_SERVICE_DOCKER_PUSH = $(DOCKER) push $(DOCKER_REGISTRY)/$(DOCKER_IMAGE_NAME):$(TAG)
SERVICE_TASK_ID = $(shell $(AWS) ecs --profile $(AWS_PROFILE) run-task --cluster $(NAMESPACE)-$(ENV) --task-definition "$(SERVICE_TASK_DEFINITION_ARN)" | $(JQ) -r '.tasks[].taskArn' | $(REV) | $(CUT) -d'/' -f1 | $(REV) && sleep 1)
CMD_ECS_SERVICE_DOCKER_PUSH = $(DOCKER) push $(DOCKER_REGISTRY)/$(DOCKER_IMAGE_NAME):$(TAG)

# TODO: Add log polling instead of sleep?
CMD_SERVICE_TASK_RUN = @echo "Task for definition $(SERVICE_TASK_DEFINITION_ARN) has been started.\nLogs: https://console.aws.amazon.com/ecs/home?region=us-east-1$(HASHSIGN)/clusters/$(NAMESPACE)-$(ENV)/tasks/$(SERVICE_TASK_ID)/details"

CMD_SERVICE_SCALE = @$(ECS) scale --profile $(AWS_PROFILE) $(ENV)-$(NAMESPACE) $(ENV)-$(SVC) $(SCALE)
CMD_SERVICE_DESTROY = echo "Destroy $(SVC) is not implemented"
CMD_ECS_SERVICE_TASK_RUN = @echo "Task for definition $(ECS_SERVICE_TASK_DEFINITION_ARN) has been started.\nLogs: https://console.aws.amazon.com/ecs/home?region=us-east-1$(HASHSIGN)/clusters/$(NAMESPACE)-$(ENV)/tasks/$(ECS_SERVICE_TASK_ID)/details"
CMD_ECS_SERVICE_SCALE = @$(ECS) scale --profile $(AWS_PROFILE) $(ENV)-$(NAMESPACE) $(ENV)-$(SVC) $(SCALE)
CMD_ECS_SERVICE_DESTROY = echo "Destroy $(SVC) is not implemented"

CMD_SERVICE_LOCAL_UP = $(ECS_CLI) local up --task-def-remote $(SERVICE_TASK_DEFINITION_ARN)
CMD_SERVICE_LOCAL_DOWN = $(ECS_CLI) local down --task-def-remote $(SERVICE_TASK_DEFINITION_ARN)
CMD_ECS_SERVICE_LOCAL_UP = $(ECS_CLI) local up --task-def-remote $(ECS_SERVICE_TASK_DEFINITION_ARN)
CMD_ECS_SERVICE_LOCAL_DOWN = $(ECS_CLI) local down --task-def-remote $(ECS_SERVICE_TASK_DEFINITION_ARN)

CMD_SERVICE_BIN = $(DOCKER) run -it --rm $(DOCKER_REGISTRY)/$(DOCKER_IMAGE_NAME):$(TAG) $(SVC)
CMD_ECS_SERVICE_BIN = $(DOCKER) run -it --rm $(DOCKER_REGISTRY)/$(DOCKER_IMAGE_NAME):$(TAG) $(SVC)

ECS ?= $(DOCKER) run -v $(HOME)/.aws/:/root/.aws -i fabfuel/ecs-deploy:1.7.1 ecs
ECS_CLI ?= $(DOCKER) run \
Expand Down Expand Up @@ -59,3 +60,15 @@ ecs-cli:
ifeq (, $(ECS_CLI))
$(error "AWS ecs-cli is not installed or incorrectly configured." )
endif

# Backwards Compatibility, should be removed in 2.0
########################################################################################################################
CMD_SERVICE_DEPLOY = $(CMD_ECS_SERVICE_DEPLOY)
CMD_SERVICE_DOCKER_BUILD = $(CMD_ECS_SERVICE_DOCKER_BUILD)
CMD_SERVICE_DOCKER_PUSH = $(CMD_ECS_SERVICE_DOCKER_PUSH)
CMD_SERVICE_TASK_RUN = $(CMD_ECS_SERVICE_TASK_RUN)
CMD_SERVICE_SCALE = $(CMD_ECS_SERVICE_SCALE)
CMD_SERVICE_DESTROY = $(CMD_ECS_SERVICE_DESTROY)
CMD_SERVICE_LOCAL_UP =$(CMD_ECS_SERVICE_LOCAL_UP)
CMD_SERVICE_LOCAL_DOWN = $(CMD_ECS_SERVICE_LOCAL_DOWN)
CMD_SERVICE_BIN = $(CMD_ECS_SERVICE_BIN)
28 changes: 28 additions & 0 deletions serverless/main.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Macroses
########################################################################################################################
SLS ?= @$(DOCKER) run --entrypoint=serverless -v $(ROOT_DIR)/$(PROJECT_PATH):/opt/app -v $(HOME)/.aws/:/root/.aws -i amaysim/serverless:1.72.0
# Tasks
########################################################################################################################
#aws.debug: ## Show environment information for debug purposes
# @echo "\033[32m=== AWS Environment Info ===\033[0m"
# @echo "\033[36mENV\033[0m: $(ENV)"
# @echo "\033[36mAWS_DEV_ENV_NAME\033[0m: $(AWS_DEV_ENV_NAME) (set devEnvironmentName here https://console.aws.amazon.com/iam/home?region=us-east-1#/users/$(AWS_USER)?section=tags)"
# @echo "\033[36mAWS_ACCOUNT\033[0m: $(AWS_ACCOUNT)"
# @echo "\033[36mAWS_PROFILE\033[0m: $(AWS_PROFILE)"
# @echo "\033[36mAWS_USER\033[0m: $(AWS_USER)"
# @echo "\033[36mTAG\033[0m: $(TAG)"
#
#aws.profile:
# $(shell mkdir -p ~/.aws && echo "[$(AWS_PROFILE)]\naws_access_key_id = $(AWS_ACCESS_KEY_ID)\naws_secret_access_key = $(AWS_SECRET_ACCESS_KEY)\nregion = $(AWS_REGION)" >> ~/.aws/credentials)


# Dependencies
########################################################################################################################
# Ensures aws toolchain is installed
aws:
ifeq (, $(SLS))
$(error "aws cli toolchain is not installed or incorrectly configured.")
endif

CMD_SLS_SERVICE_DEPLOY = $(SLS) deploy --env $(ENV) --region $(AWS_REGION) --profile $(AWS_PROFILE)
CMD_SLS_SERVICE_DESTROY = $(SLS) remove --env $(ENV) --region $(AWS_REGION) --profile $(AWS_PROFILE)
6 changes: 3 additions & 3 deletions terraform/main.mk
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ terraform.init: gomplate terraform
# TODO: Potentionally replace gomplate by terragrunt
# TODO:? Implement -target approach so we can deploy specific apps only
# TODO: generate env vars into tfvars in only one task
terraform.apply: terraform.init terraform ## Deploy infrastructure
terraform.apply: terraform.init ## Deploy infrastructure
@ cd $(INFRA_DIR)/env/$(ENV) && \
$(TERRAFORM) plan -out=tfplan -input=false && \
$(TERRAFORM) apply -input=false tfplan && \
$(TERRAFORM) output -json > output.json

terraform.test: terraform.init terraform ## Test infrastructure
terraform.test: terraform.init ## Test infrastructure
$(CHECKOV)
@ cd $(INFRA_DIR)/env/$(ENV) && \
$(TERRAFORM) validate ./ && \
$(TERRAFORM) plan -input=false
@ $(CHECKOV)

terraform.refresh: terraform.init terraform ## Test infrastructure
terraform.refresh: terraform.init ## Test infrastructure
@ cd $(INFRA_DIR)/env/$(ENV) && \
$(TERRAFORM) refresh

Expand Down

0 comments on commit 6f379a1

Please sign in to comment.