Skip to content

Commit da0b09d

Browse files
committed
Add Secrets Manager policy and improve ECS deployment stability checks
- Introduce a new IAM policy for Secrets Manager access in container.ts to enhance security for task execution roles. - Update buildspec.yml to include error handling for ECS service updates, ensuring deployment failures are logged and handled gracefully. - Enhance deploy.sh with a timeout for ECS service stabilization and detailed logging for debugging failed deployments.
1 parent ccb1437 commit da0b09d

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

apps/infra/modules/container.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,29 @@ export function createContainer(
144144
}
145145
);
146146

147+
// Add policy for Secrets Manager access to task execution role
148+
const taskExecutionSecretsPolicy = new aws.iam.RolePolicy(
149+
"pathfinder-task-execution-secrets-policy",
150+
{
151+
role: taskExecutionRole.id,
152+
policy: database.secretArn.apply((secretArn) =>
153+
JSON.stringify({
154+
Version: "2012-10-17",
155+
Statement: [
156+
{
157+
Effect: "Allow",
158+
Action: [
159+
"secretsmanager:GetSecretValue",
160+
"secretsmanager:DescribeSecret",
161+
],
162+
Resource: secretArn,
163+
},
164+
],
165+
})
166+
),
167+
}
168+
);
169+
147170
// ECS Task Role for application permissions
148171
const taskRole = new aws.iam.Role("pathfinder-task-role", {
149172
name: "pathfinder-ecs-task-role",

apps/web/buildspec.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,15 @@ phases:
9292

9393
post_build:
9494
commands:
95+
- echo "Pushing images to ECR..."
9596
- docker push $REPOSITORY_URI:$IMAGE_TAG
9697
- docker push $REPOSITORY_URI:latest
9798
- echo "Updating ECS service to deploy new image..."
98-
- aws ecs update-service --cluster $ECS_CLUSTER_NAME --service $ECS_SERVICE_NAME --force-new-deployment
99+
- |
100+
if ! aws ecs update-service --cluster $ECS_CLUSTER_NAME --service $ECS_SERVICE_NAME --force-new-deployment; then
101+
echo "❌ Failed to update ECS service"
102+
exit 1
103+
fi
99104
- echo "Writing image definitions file..."
100105
- 'printf "[{\"name\":\"pathfinder-app\",\"imageUri\":\"%s\"}]" $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json'
101106

scripts/deploy.sh

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,52 @@ echo -e "${YELLOW}🔍 Step 3: Verifying deployment...${NC}"
6767

6868
# Wait for ECS service to stabilize after the build updated it
6969
echo -e "${YELLOW}⏳ Waiting for ECS deployment to stabilize...${NC}"
70-
aws ecs wait services-stable \
70+
71+
# Set a timeout of 10 minutes (600 seconds) for the wait
72+
if ! timeout 600 aws ecs wait services-stable \
7173
--cluster "$CLUSTER_NAME" \
72-
--services pathfinder
74+
--services pathfinder; then
75+
76+
echo -e "${RED}❌ ECS service failed to stabilize within 10 minutes${NC}"
77+
78+
# Get the current service status for debugging
79+
echo -e "${RED}Current service status:${NC}"
80+
aws ecs describe-services \
81+
--cluster "$CLUSTER_NAME" \
82+
--services pathfinder \
83+
--query 'services[0].{desired:desiredCount,running:runningCount,pending:pendingCount,status:status}' \
84+
--output table
85+
86+
# Get recent events
87+
echo -e "${RED}Recent service events:${NC}"
88+
aws ecs describe-services \
89+
--cluster "$CLUSTER_NAME" \
90+
--services pathfinder \
91+
--query 'services[0].events[0:5]' \
92+
--output json
93+
94+
# Check for failed tasks
95+
echo -e "${RED}Checking for task failures:${NC}"
96+
TASK_ARNS=$(aws ecs list-tasks \
97+
--cluster "$CLUSTER_NAME" \
98+
--service-name pathfinder \
99+
--desired-status STOPPED \
100+
--query 'taskArns[0:3]' \
101+
--output json)
102+
103+
if [ "$TASK_ARNS" != "[]" ] && [ -n "$TASK_ARNS" ]; then
104+
aws ecs describe-tasks \
105+
--cluster "$CLUSTER_NAME" \
106+
--tasks $TASK_ARNS \
107+
--query 'tasks[*].{taskArn:taskArn,stoppedReason:stoppedReason}' \
108+
--output table
109+
fi
110+
111+
echo -e "${RED}❌ Deployment failed - ECS service is not stable${NC}"
112+
exit 1
113+
fi
114+
115+
echo -e "${GREEN}✅ ECS service is stable${NC}"
73116

74117
# Get ALB DNS name
75118
alb_dns=$(aws elbv2 describe-load-balancers \

0 commit comments

Comments
 (0)