automated terminal push
This commit is contained in:
73
terraform/user_data.sh
Normal file
73
terraform/user_data.sh
Normal file
@@ -0,0 +1,73 @@
|
||||
#!/bin/bash
|
||||
# Enhanced user data script with SSM and better logging
|
||||
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
|
||||
|
||||
echo "=== Starting EC2 User Data Script ==="
|
||||
echo "Timestamp: $(date)"
|
||||
echo "Instance ID: $(curl -s http://169.254.169.254/latest/meta-data/instance-id)"
|
||||
echo "Cluster Name: ${cluster_name}"
|
||||
|
||||
# Update system
|
||||
echo "=== Updating system packages ==="
|
||||
yum update -y
|
||||
|
||||
# Install and configure SSM agent (should already be installed on Amazon Linux 2)
|
||||
echo "=== Configuring SSM Agent ==="
|
||||
yum install -y amazon-ssm-agent
|
||||
systemctl enable amazon-ssm-agent
|
||||
systemctl start amazon-ssm-agent
|
||||
|
||||
# Install ECS agent
|
||||
echo "=== Installing ECS Agent ==="
|
||||
yum install -y ecs-init
|
||||
|
||||
# Configure ECS cluster
|
||||
echo "=== Configuring ECS Cluster ==="
|
||||
cat > /etc/ecs/ecs.config << EOF
|
||||
ECS_CLUSTER=${cluster_name}
|
||||
ECS_ENABLE_LOGGING=true
|
||||
ECS_LOGLEVEL=info
|
||||
ECS_ENABLE_CONTAINER_METADATA=true
|
||||
ECS_ENABLE_TASK_IAM_ROLE=true
|
||||
ECS_AVAILABLE_LOGGING_DRIVERS=["json-file","awslogs"]
|
||||
ECS_CONTAINER_STOP_TIMEOUT=30s
|
||||
ECS_CONTAINER_START_TIMEOUT=3m
|
||||
ECS_DISABLE_IMAGE_CLEANUP=false
|
||||
EOF
|
||||
|
||||
# Start Docker and ECS
|
||||
echo "=== Starting Docker and ECS services ==="
|
||||
systemctl enable docker
|
||||
systemctl start docker
|
||||
systemctl enable ecs
|
||||
systemctl start ecs
|
||||
|
||||
# Wait for services to be ready
|
||||
echo "=== Waiting for services to initialize ==="
|
||||
sleep 30
|
||||
|
||||
# Verify services
|
||||
echo "=== Service Status Check ==="
|
||||
echo "SSM Agent Status:"
|
||||
systemctl status amazon-ssm-agent --no-pager || echo "SSM agent status check failed"
|
||||
|
||||
echo "Docker Status:"
|
||||
systemctl status docker --no-pager || echo "Docker status check failed"
|
||||
|
||||
echo "ECS Status:"
|
||||
systemctl status ecs --no-pager || echo "ECS status check failed"
|
||||
|
||||
# Check ECS agent connection
|
||||
echo "=== ECS Agent Status ==="
|
||||
for i in {1..5}; do
|
||||
if curl -s http://localhost:51678/v1/metadata; then
|
||||
echo "ECS agent is responding"
|
||||
break
|
||||
else
|
||||
echo "ECS agent not ready yet, attempt $i/5"
|
||||
sleep 10
|
||||
fi
|
||||
done
|
||||
|
||||
echo "=== User Data Script Completed ==="
|
||||
echo "Timestamp: $(date)"
|
Reference in New Issue
Block a user