Skip to content

Commit 543347d

Browse files
committed
aws deployment configuration
1 parent 108cc6b commit 543347d

5 files changed

Lines changed: 385 additions & 0 deletions

File tree

aws-deployment/create-iam-roles.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/bash
2+
3+
# Create IAM roles for ECS if they don't exist
4+
5+
AWS_REGION="us-east-1"
6+
AWS_ACCOUNT_ID="655593806999"
7+
8+
echo "🔐 Creating IAM Roles for ECS..."
9+
10+
# Create ECS Task Execution Role
11+
cat > /tmp/ecs-task-execution-trust-policy.json << EOF
12+
{
13+
"Version": "2012-10-17",
14+
"Statement": [
15+
{
16+
"Effect": "Allow",
17+
"Principal": {
18+
"Service": "ecs-tasks.amazonaws.com"
19+
},
20+
"Action": "sts:AssumeRole"
21+
}
22+
]
23+
}
24+
EOF
25+
26+
# Check if role exists
27+
if ! aws iam get-role --role-name ecsTaskExecutionRole 2>/dev/null; then
28+
echo "Creating ecsTaskExecutionRole..."
29+
aws iam create-role \
30+
--role-name ecsTaskExecutionRole \
31+
--assume-role-policy-document file:///tmp/ecs-task-execution-trust-policy.json
32+
33+
aws iam attach-role-policy \
34+
--role-name ecsTaskExecutionRole \
35+
--policy-arn arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy
36+
37+
echo "✅ ecsTaskExecutionRole created"
38+
else
39+
echo "✅ ecsTaskExecutionRole already exists"
40+
fi
41+
42+
# Create ECS Task Role
43+
if ! aws iam get-role --role-name ecsTaskRole 2>/dev/null; then
44+
echo "Creating ecsTaskRole..."
45+
aws iam create-role \
46+
--role-name ecsTaskRole \
47+
--assume-role-policy-document file:///tmp/ecs-task-execution-trust-policy.json
48+
49+
echo "✅ ecsTaskRole created"
50+
else
51+
echo "✅ ecsTaskRole already exists"
52+
fi
53+
54+
echo "🎉 IAM Roles setup complete!"
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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 ""
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"serviceName": "banking-system-service",
3+
"cluster": "bank-app-cluster",
4+
"taskDefinition": "banking-system-task",
5+
"desiredCount": 2,
6+
"launchType": "FARGATE",
7+
"networkConfiguration": {
8+
"awsvpcConfiguration": {
9+
"subnets": [
10+
"subnet-xxxxxxxxx",
11+
"subnet-yyyyyyyyy"
12+
],
13+
"securityGroups": [
14+
"sg-xxxxxxxxx"
15+
],
16+
"assignPublicIp": "ENABLED"
17+
}
18+
},
19+
"loadBalancers": [
20+
{
21+
"targetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:655593806999:targetgroup/banking-tg/xxxxxxxxx",
22+
"containerName": "banking-app",
23+
"containerPort": 8080
24+
}
25+
],
26+
"healthCheckGracePeriodSeconds": 120,
27+
"deploymentConfiguration": {
28+
"maximumPercent": 200,
29+
"minimumHealthyPercent": 50,
30+
"deploymentCircuitBreaker": {
31+
"enable": true,
32+
"rollback": true
33+
}
34+
}
35+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/bin/bash
2+
3+
# Setup Security Group for Banking System
4+
5+
AWS_REGION="us-east-1"
6+
7+
echo "🔒 Setting up Security Group for Banking System..."
8+
9+
# Get default VPC
10+
DEFAULT_VPC=$(aws ec2 describe-vpcs --filters "Name=isDefault,Values=true" --query 'Vpcs[0].VpcId' --output text --region $AWS_REGION)
11+
12+
if [ "$DEFAULT_VPC" = "None" ] || [ -z "$DEFAULT_VPC" ]; then
13+
echo "❌ No default VPC found. Please create a VPC first."
14+
exit 1
15+
fi
16+
17+
echo "✅ Using VPC: $DEFAULT_VPC"
18+
19+
# Create Security Group
20+
SG_ID=$(aws ec2 create-security-group \
21+
--group-name banking-system-sg \
22+
--description "Security group for Banking System ECS tasks" \
23+
--vpc-id $DEFAULT_VPC \
24+
--region $AWS_REGION \
25+
--query 'GroupId' \
26+
--output text 2>/dev/null)
27+
28+
if [ $? -eq 0 ]; then
29+
echo "✅ Security Group created: $SG_ID"
30+
else
31+
# Security group might already exist
32+
SG_ID=$(aws ec2 describe-security-groups \
33+
--filters "Name=group-name,Values=banking-system-sg" "Name=vpc-id,Values=$DEFAULT_VPC" \
34+
--query 'SecurityGroups[0].GroupId' \
35+
--output text \
36+
--region $AWS_REGION)
37+
echo "✅ Using existing Security Group: $SG_ID"
38+
fi
39+
40+
# Add inbound rules
41+
echo "Adding inbound rules..."
42+
43+
# Allow HTTP traffic on port 8080
44+
aws ec2 authorize-security-group-ingress \
45+
--group-id $SG_ID \
46+
--protocol tcp \
47+
--port 8080 \
48+
--cidr 0.0.0.0/0 \
49+
--region $AWS_REGION 2>/dev/null || echo "Port 8080 rule already exists"
50+
51+
# Allow HTTPS traffic on port 443 (if using ALB)
52+
aws ec2 authorize-security-group-ingress \
53+
--group-id $SG_ID \
54+
--protocol tcp \
55+
--port 443 \
56+
--cidr 0.0.0.0/0 \
57+
--region $AWS_REGION 2>/dev/null || echo "Port 443 rule already exists"
58+
59+
# Allow HTTP traffic on port 80 (if using ALB)
60+
aws ec2 authorize-security-group-ingress \
61+
--group-id $SG_ID \
62+
--protocol tcp \
63+
--port 80 \
64+
--cidr 0.0.0.0/0 \
65+
--region $AWS_REGION 2>/dev/null || echo "Port 80 rule already exists"
66+
67+
echo "✅ Security Group setup complete!"
68+
echo "Security Group ID: $SG_ID"
69+
echo "VPC ID: $DEFAULT_VPC"
70+
71+
# Get subnets
72+
echo "Available subnets in your VPC:"
73+
aws ec2 describe-subnets \
74+
--filters "Name=vpc-id,Values=$DEFAULT_VPC" \
75+
--query 'Subnets[*].{SubnetId:SubnetId,AvailabilityZone:AvailabilityZone,CidrBlock:CidrBlock}' \
76+
--output table \
77+
--region $AWS_REGION
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"family": "banking-system-task",
3+
"networkMode": "awsvpc",
4+
"requiresCompatibilities": ["FARGATE"],
5+
"cpu": "512",
6+
"memory": "1024",
7+
"executionRoleArn": "arn:aws:iam::655593806999:role/ecsTaskExecutionRole",
8+
"taskRoleArn": "arn:aws:iam::655593806999:role/ecsTaskRole",
9+
"containerDefinitions": [
10+
{
11+
"name": "banking-app",
12+
"image": "655593806999.dkr.ecr.us-east-1.amazonaws.com/eomaxl/banking-system-registry:latest",
13+
"portMappings": [
14+
{
15+
"containerPort": 8080,
16+
"protocol": "tcp"
17+
}
18+
],
19+
"environment": [
20+
{
21+
"name": "SPRING_PROFILES_ACTIVE",
22+
"value": "production"
23+
},
24+
{
25+
"name": "SPRING_DATASOURCE_URL",
26+
"value": "jdbc:postgresql://database-1.cgrmu26cmkbf.us-east-1.rds.amazonaws.com:5432/postgres"
27+
},
28+
{
29+
"name": "SPRING_DATASOURCE_USERNAME",
30+
"value": "postgres"
31+
},
32+
{
33+
"name": "SPRING_DATASOURCE_PASSWORD",
34+
"value": "Welcome2Bank#"
35+
}
36+
],
37+
"logConfiguration": {
38+
"logDriver": "awslogs",
39+
"options": {
40+
"awslogs-group": "/ecs/banking-system",
41+
"awslogs-region": "us-east-1",
42+
"awslogs-stream-prefix": "ecs"
43+
}
44+
},
45+
"healthCheck": {
46+
"command": [
47+
"CMD-SHELL",
48+
"curl -f http://localhost:8080/actuator/health || exit 1"
49+
],
50+
"interval": 30,
51+
"timeout": 5,
52+
"retries": 3,
53+
"startPeriod": 60
54+
},
55+
"essential": true
56+
}
57+
]
58+
}

0 commit comments

Comments
 (0)