1+ #! /bin/bash
2+
3+ # Banking System AWS Deployment Script
4+ # Customized for your existing AWS infrastructure
5+
6+ set -e
7+
8+ # Configuration - Update these with your actual values
9+ AWS_REGION=" us-east-1"
10+ AWS_ACCOUNT_ID=" 655593806999"
11+ ECR_REPOSITORY=" eomaxl/banking-system-registry"
12+ ECS_CLUSTER=" bank-app-cluster"
13+ IMAGE_TAG=${1:- latest}
14+
15+ echo " 🚀 Starting Banking System Deployment"
16+ echo " 📍 Region: $AWS_REGION "
17+ echo " 🏷️ Image Tag: $IMAGE_TAG "
18+ echo " 🏦 ECR Repository: $ECR_REPOSITORY "
19+ echo " ⚙️ ECS Cluster: $ECS_CLUSTER "
20+
21+ # Colors for output
22+ GREEN=' \033[0;32m'
23+ YELLOW=' \033[1;33m'
24+ RED=' \033[0;31m'
25+ NC=' \033[0m'
26+
27+ print_status () {
28+ echo -e " ${GREEN} ✅ $1 ${NC} "
29+ }
30+
31+ print_warning () {
32+ echo -e " ${YELLOW} ⚠️ $1 ${NC} "
33+ }
34+
35+ print_error () {
36+ echo -e " ${RED} ❌ $1 ${NC} "
37+ }
38+
39+ # Step 1: Check AWS CLI configuration
40+ if ! aws sts get-caller-identity > /dev/null 2>&1 ; then
41+ print_error " AWS CLI is not configured. Please run 'aws configure' first."
42+ exit 1
43+ fi
44+
45+ print_status " AWS CLI is configured"
46+
47+ # Step 2: Build the application
48+ print_status " Building Spring Boot application..."
49+ mvn clean package -DskipTests
50+
51+ if [ ! -f " target/banking-system-1.0.0.jar" ]; then
52+ print_error " JAR file not found. Build failed."
53+ exit 1
54+ fi
55+
56+ print_status " Application built successfully"
57+
58+ # Step 3: Build Docker image
59+ print_status " Building Docker image..."
60+ docker build -t banking-system:$IMAGE_TAG .
61+
62+ # Step 4: Tag image for ECR
63+ ECR_URI=" $AWS_ACCOUNT_ID .dkr.ecr.$AWS_REGION .amazonaws.com/$ECR_REPOSITORY :$IMAGE_TAG "
64+ docker tag banking-system:$IMAGE_TAG $ECR_URI
65+
66+ print_status " Docker image tagged: $ECR_URI "
67+
68+ # Step 5: Login to ECR
69+ print_status " Logging into ECR..."
70+ aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID .dkr.ecr.$AWS_REGION .amazonaws.com
71+
72+ # Step 6: Push image to ECR
73+ print_status " Pushing image to ECR..."
74+ docker push $ECR_URI
75+
76+ print_status " Image pushed successfully"
77+
78+ # Step 7: Create CloudWatch Log Group
79+ print_status " Creating CloudWatch Log Group..."
80+ aws logs create-log-group --log-group-name " /ecs/banking-system" --region $AWS_REGION 2> /dev/null || print_warning " Log group already exists"
81+
82+ # Step 8: Create or update ECS Task Definition
83+ print_status " Registering ECS Task Definition..."
84+
85+ # Update the task definition with the new image
86+ sed " s|655593806999.dkr.ecr.us-east-1.amazonaws.com/eomaxl/banking-system-registry:latest|$ECR_URI |g" aws-deployment/task-definition.json > /tmp/task-definition-updated.json
87+
88+ TASK_DEFINITION_ARN=$( aws ecs register-task-definition \
89+ --cli-input-json file:///tmp/task-definition-updated.json \
90+ --region $AWS_REGION \
91+ --query ' taskDefinition.taskDefinitionArn' \
92+ --output text)
93+
94+ print_status " Task Definition registered: $TASK_DEFINITION_ARN "
95+
96+ # Step 9: Update ECS Service (or create if it doesn't exist)
97+ print_status " Updating ECS Service..."
98+
99+ # Check if service exists
100+ if aws ecs describe-services --cluster $ECS_CLUSTER --services banking-system-service --region $AWS_REGION --query ' services[0].serviceName' --output text 2> /dev/null | grep -q " banking-system-service" ; then
101+ print_status " Service exists, updating..."
102+
103+ aws ecs update-service \
104+ --cluster $ECS_CLUSTER \
105+ --service banking-system-service \
106+ --task-definition $TASK_DEFINITION_ARN \
107+ --region $AWS_REGION \
108+ --query ' service.serviceName' \
109+ --output text
110+
111+ print_status " Service updated successfully"
112+ else
113+ print_warning " Service doesn't exist. You'll need to create it manually or provide VPC/subnet details."
114+ echo " To create the service, you need:"
115+ echo " 1. VPC Subnets (at least 2 in different AZs)"
116+ echo " 2. Security Group allowing inbound traffic on port 8080"
117+ echo " 3. Application Load Balancer (optional but recommended)"
118+
119+ echo " "
120+ echo " Example command to create service:"
121+ echo " aws ecs create-service \\ "
122+ echo " --cluster $ECS_CLUSTER \\ "
123+ echo " --service-name banking-system-service \\ "
124+ echo " --task-definition $TASK_DEFINITION_ARN \\ "
125+ echo " --desired-count 2 \\ "
126+ echo " --launch-type FARGATE \\ "
127+ echo " --network-configuration 'awsvpcConfiguration={subnets=[subnet-xxx,subnet-yyy],securityGroups=[sg-xxx],assignPublicIp=ENABLED}'"
128+ fi
129+
130+ # Step 10: Wait for deployment to complete
131+ print_status " Waiting for service to stabilize..."
132+ aws ecs wait services-stable --cluster $ECS_CLUSTER --services banking-system-service --region $AWS_REGION
133+
134+ print_status " Deployment completed successfully!"
135+
136+ # Step 11: Get service information
137+ print_status " Getting service information..."
138+ TASKS=$( aws ecs list-tasks --cluster $ECS_CLUSTER --service-name banking-system-service --region $AWS_REGION --query ' taskArns' --output text)
139+
140+ if [ ! -z " $TASKS " ]; then
141+ print_status " Service is running with tasks: $TASKS "
142+
143+ # Get task details
144+ aws ecs describe-tasks --cluster $ECS_CLUSTER --tasks $TASKS --region $AWS_REGION --query ' tasks[0].{TaskArn:taskArn,LastStatus:lastStatus,HealthStatus:healthStatus}'
145+ else
146+ print_warning " No tasks found. Service might be starting up."
147+ fi
148+
149+ echo " "
150+ echo " 🎉 Banking System Deployment Summary:"
151+ echo " 📦 Image: $ECR_URI "
152+ echo " ⚙️ Cluster: $ECS_CLUSTER "
153+ echo " 🔧 Task Definition: $TASK_DEFINITION_ARN "
154+ echo " 📊 Service: banking-system-service"
155+ echo " "
156+ echo " 📋 Next Steps:"
157+ echo " 1. Check ECS Console: https://console.aws.amazon.com/ecs/home?region=$AWS_REGION #/clusters/$ECS_CLUSTER /services"
158+ echo " 2. Monitor CloudWatch Logs: https://console.aws.amazon.com/cloudwatch/home?region=$AWS_REGION #logsV2:log-groups/log-group/%2Fecs%2Fbanking-system"
159+ echo " 3. If you have an ALB, check the target group health"
160+ echo " 4. Test the application health endpoint: http://[PUBLIC_IP]:8080/actuator/health"
161+ echo " "
0 commit comments