Skip to content

code deploy on ec2

-1

using below ec2 config in cdk code while creating ec2 for code deploy

=========================

    # EC2 CONFIG
    # =========================
    ami = ec2.MachineImage.generic_linux({
        "us-east-1": "ami-0030e4319cbf4dbf2"  # Ubuntu 22.04 LTS x86_64
    })
    instance_type = ec2.InstanceType("t3.medium")
    subnet_selection = ec2.SubnetSelection(
        subnet_type=ec2.SubnetType.PRIVATE_WITH_EGRESS
    )

    # User Data: Install CodeDeploy Agent via official S3 method
    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",
        "sudo systemctl start codedeploy-agent",
        "sudo systemctl enable codedeploy-agent",
    )

when checked in ec2 got $ sudo systemctl status codedeploy-agent Unit codedeploy-agent.service could not be found.

how to solve this using cdk

asked 8 months ago104 views

1 Answer
0

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:

  1. Use sudo service codedeploy-agent commands instead of systemctl - the CodeDeploy agent on Ubuntu may use the older init.d service management
  2. Add a sleep command to allow the installation to complete before starting the service
  3. Use restart instead of separate start and enable commands 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

answered 8 months ago

EXPERT

reviewed 8 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.