- Newest
- Most votes
- Most comments
The issue you're encountering is that the CodeDeploy agent installation script creates a service file but may not be placing it in the correct location for systemd to find it automatically. Here's how to solve this in your CDK code:
Update your user_data commands to ensure the CodeDeploy agent service is properly registered with systemd:
user_data = ec2.UserData.for_linux() user_data.add_commands( "sudo apt update -y", "sudo apt install ruby wget -y", "cd /tmp", "wget https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install", "chmod +x ./install", "sudo ./install auto", # Wait for installation to complete "sleep 10", # Start the agent using the service command "sudo service codedeploy-agent start", # Verify it's running "sudo service codedeploy-agent status" )
Alternatively, you can use a more robust approach that checks if the agent is installed correctly:
user_data = ec2.UserData.for_linux() user_data.add_commands( "#!/bin/bash", "set -e", "sudo apt update -y", "sudo apt install ruby wget -y", "cd /tmp", "wget https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install", "chmod +x ./install", "sudo ./install auto", # Use service command instead of systemctl "sudo service codedeploy-agent restart", # Verify the agent is running "until sudo service codedeploy-agent status | grep -q 'running'; do", " echo 'Waiting for CodeDeploy agent to start...'", " sleep 5", "done", "echo 'CodeDeploy agent is running'" )
The key differences are:
- Use
sudo service codedeploy-agentcommands instead ofsystemctl- the CodeDeploy agent on Ubuntu may use the older init.d service management - Add a sleep command to allow the installation to complete before starting the service
- Use
restartinstead of separatestartandenablecommands to ensure the service starts properly
Make sure your EC2 instance also has the proper IAM role attached with permissions for CodeDeploy operations, and that your instance is tagged appropriately so CodeDeploy can target it for deployments.
Sources
Create an Amazon EC2 instance for CodeDeploy (AWS CLI or Amazon EC2 console) - AWS CodeDeploy
Community | Bootstrapping an Amazon EC2 Instance Using User-data to Run a Python Web App
Relevant content
asked 8 months ago
asked 8 months ago
asked 8 months ago
- AWS OFFICIALUpdated 6 months ago
