Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
218 lines (184 loc) · 7.61 KB
/
Copy pathMakefile
File metadata and controls
218 lines (184 loc) · 7.61 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# Fraud Detection App - AWS Deployment Makefile
# Usage: make <target>
# Example: make deploy-all
# =============================================
# Configuration Variables
# =============================================
AWS_REGION ?= us-east-2
ECR_REPO_NAME ?= fraud-detection-app
CLUSTER_NAME ?= fraud-detection-cluster
SERVICE_NAME ?= fraud-detection-service
TASK_FAMILY ?= fraud-detection-task
# Auto-detect AWS Account ID
AWS_ACCOUNT_ID := $(shell aws sts get-caller-identity --query Account --output text)
ECR_URI := $(AWS_ACCOUNT_ID).dkr.ecr.$(AWS_REGION).amazonaws.com/$(ECR_REPO_NAME)
# Version tags
VERSION ?= $(shell date +%Y%m%d-%H%M%S)
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
BUILD_IMAGE ?= $(ECR_REPO_NAME)
GIT_SHA := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
# =============================================
# Helper Targets
# =============================================
.PHONY: help
help: ## Show this help message
@echo "Fraud Detection App - AWS Deployment"
@echo ""
@echo "Configuration:"
@echo " AWS Account: $(AWS_ACCOUNT_ID)"
@echo " Region: $(AWS_REGION)"
@echo " ECR URI: $(ECR_URI)"
@echo ""
@echo "Available targets:"
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " %-20s %s\n", $$1, $$2}'
.PHONY: check-aws
check-aws: ## Verify AWS CLI is configured
@echo "Checking AWS configuration..."
@aws sts get-caller-identity > /dev/null || (echo "AWS CLI not configured!" && exit 1)
@echo "✓ AWS CLI configured for account: $(AWS_ACCOUNT_ID)"
.PHONY: check-docker
check-docker: ## Verify Docker is running
@echo "Checking Docker..."
@docker info > /dev/null 2>&1 || (echo "Docker is not running!" && exit 1)
@echo "✓ Docker is running"
# =============================================
# ECR Setup
# =============================================
.PHONY: ecr-create
ecr-create: check-aws ## Create ECR repository
@echo "Creating ECR repository: $(ECR_REPO_NAME)..."
@aws ecr describe-repositories --repository-names $(ECR_REPO_NAME) --region $(AWS_REGION) 2>/dev/null || \
aws ecr create-repository \
--repository-name $(ECR_REPO_NAME) \
--region $(AWS_REGION) \
--image-scanning-configuration scanOnPush=true \
--encryption-configuration encryptionType=AES256
@echo "✓ ECR repository ready"
.PHONY: ecr-login
ecr-login: check-aws check-docker ## Login to ECR
@echo "Logging into ECR..."
@aws ecr get-login-password --region $(AWS_REGION) | \
docker login --username AWS --password-stdin $(AWS_ACCOUNT_ID).dkr.ecr.$(AWS_REGION).amazonaws.com
@echo "✓ Logged into ECR"
.PHONY: ecr-list
ecr-list: ## List images in ECR repository
@echo "Images in $(ECR_REPO_NAME):"
@aws ecr list-images --repository-name $(ECR_REPO_NAME) --region $(AWS_REGION)
# =============================================
# Docker Build & Push
# =============================================
.PHONY: build
build: check-docker ## Build Docker image locally
@echo "Building Docker image..."
@docker build -t $(ECR_REPO_NAME):latest .
@echo "✓ Docker image built: $(ECR_REPO_NAME):latest"
build-image:
docker buildx build --platform "linux/amd64" --tag "$(BUILD_IMAGE):$(GIT_SHA)-build" --target "build" .
docker buildx build --cache-from "$(BUILD_IMAGE):$(GIT_SHA)-build" --platform "linux/amd64" --tag "$(BUILD_IMAGE):$(GIT_SHA)" .
.PHONY: build-no-cache
build-no-cache: check-docker ## Build Docker image without cache
@echo "Building Docker image (no cache)..."
@docker build --no-cache -t $(ECR_REPO_NAME):latest .
@echo "✓ Docker image built: $(ECR_REPO_NAME):latest"
.PHONY: test-local
test-local: build ## Test Docker image locally
@echo "Testing Docker image locally..."
@docker run -d --name fraud-detection-test \
-p 8080:8080 \
-e SPRING_PROFILES_ACTIVE=local \
$(ECR_REPO_NAME):latest
@echo "✓ Container started. Waiting for health check..."
@sleep 10
@curl -f http://localhost:8080/actuator/health && echo "✓ Health check passed" || echo "✗ Health check failed"
@docker logs fraud-detection-test
@docker stop fraud-detection-test
@docker rm fraud-detection-test
.PHONY: tag
tag: ## Tag Docker image for ECR
@echo "Tagging Docker image..."
@docker tag $(ECR_REPO_NAME):latest $(ECR_URI):latest
@docker tag $(ECR_REPO_NAME):latest $(ECR_URI):$(VERSION)
@docker tag $(ECR_REPO_NAME):latest $(ECR_URI):$(GIT_COMMIT)
@echo "✓ Image tagged with: latest, $(VERSION), $(GIT_COMMIT)"
.PHONY: push
push: ecr-login tag ## Push Docker image to ECR
@echo "Pushing Docker image to ECR..."
@docker push $(ECR_URI):latest
@docker push $(ECR_URI):$(VERSION)
@docker push $(ECR_URI):$(GIT_COMMIT)
@echo "✓ Image pushed to ECR"
.PHONY: build-push
build-push: build push ## Build and push Docker image
# =============================================
# Infrastructure Setup
# =============================================
.PHONY: infra-create
infra-create: check-aws ## Create VPC, subnets, security groups
@echo "Creating infrastructure..."
@./scripts/create-infrastructure.sh
@echo "✓ Infrastructure created"
.PHONY: iam-create
iam-create: check-aws ## Create IAM roles for ECS
@echo "Creating IAM roles..."
@./scripts/create-iam-roles.sh
@echo "✓ IAM roles created"
.PHONY: params-create
params-create: check-aws ## Create Parameter Store parameters
@echo "Creating Parameter Store parameters..."
@./scripts/create-parameters.sh
@echo "✓ Parameters created"
.PHONY: logs-create
logs-create: check-aws ## Create CloudWatch log group
@echo "Creating CloudWatch log group..."
@aws logs create-log-group --log-group-name /ecs/fraud-detection --region $(AWS_REGION) 2>/dev/null || true
@aws logs put-retention-policy --log-group-name /ecs/fraud-detection --retention-in-days 7 --region $(AWS_REGION) 2>/dev/null || true
@echo "✓ Log group created"
# =============================================
# ECS Deployment
# =============================================
.PHONY: cluster-create
cluster-create: check-aws ## Create ECS cluster
@echo "Creating ECS cluster: $(CLUSTER_NAME)..."
@aws ecs create-cluster \
--cluster-name $(CLUSTER_NAME) \
--region $(AWS_REGION) \
--settings name=containerInsights,value=enabled 2>/dev/null || echo "Cluster may already exist"
@echo "✓ ECS cluster ready"
.PHONY: task-register
task-register: check-aws ## Register ECS task definition
@echo "Generating task definition..."
@./scripts/generate-task-definition.sh > task-definition.json
@echo "Registering task definition with ECS..."
@aws ecs register-task-definition \
--cli-input-json file://task-definition.json \
--region $(AWS_REGION)
@echo "✓ Task definition registered"
.PHONY: service-create
service-create: check-aws ## Create ECS service
@echo "Creating ECS service..."
@bash ./scripts/create-service.sh
@echo "✓ ECS service created"
.PHONY: deploy-init
deploy-init: infra-create iam-create logs-create cluster-create task-register service-create ## Initialize complete deployment infrastructure
@echo "✓ All infrastructure initialized and ready for deployment"
build-image-push: build-push
build-image-pull:
@echo "Pulling image from ECR..."
@docker pull $(ECR_URI):latest
build-image-promote:
@echo "Promoting image with tag $(BUILD_TAG)..."
@docker tag $(ECR_URI):latest $(ECR_URI):$(BUILD_TAG)
@docker push $(ECR_URI):$(BUILD_TAG)
up:
@echo "Starting container..."
@docker run -d --name fraud-detection-test \
-p 8080:8080 \
-e SPRING_PROFILES_ACTIVE=test \
-e AWS_REGION=$(AWS_REGION) \
$(ECR_URI):latest
@echo "✓ Container started"
down:
@echo "Stopping container..."
@docker stop fraud-detection-test 2>/dev/null || true
@docker rm fraud-detection-test 2>/dev/null || true
@echo "✓ Container stopped"