Search code examples
amazon-web-servicesaws-code-deployamazon-lightsailamazon-linux-2

Can't install CodeDeploy in Lightsail instance with Amazon Linux 2


As wasn't particularly satisfied with only being able to use Amazon Linux (wanted to use Amazon Linux 2 as well), created two instances using both OS versions and adding the same script

mkdir /etc/codedeploy-agent/

mkdir /etc/codedeploy-agent/conf

cat <<EOT >> /etc/codedeploy-agent/conf/codedeploy.onpremises.yml

---

aws_access_key_id: ACCESS

aws_secret_access_key: SECRET

iam_user_arn: arn:aws:iam::525221857828:user/GeneralUser

region: eu-west-2

EOT

wget https://aws-codedeploy-us-west-2.s3.us-west-2.amazonaws.com/latest/install

chmod +x ./install

sudo ./install auto

The difference I noted between the two is that in the instance that has Linux 2, the folder /etc/codedeploy-agent/conf/ has only one file

Amazon Lightsail Linux 2

and in Linux has two files

Amazon Lightsail Linux

Knowing this, I created a new file in the Linux 2 instance with the same name

touch codedeployagent.yml

, changed its permissions from

-rw-r--r-- 1 root root 261 Oct  2 10:43 codedeployagent.yml

to

-rwxr-xr-x 1 root root 261 Oct  2 10:43 codedeployagent.yml

File permissions

, and added the same content

:log_aws_wire: false
:log_dir: '/var/log/aws/codedeploy-agent/'
:pid_dir: '/opt/codedeploy-agent/state/.pid/'
:program_name: codedeploy-agent
:root_dir: '/opt/codedeploy-agent/deployment-root'
:verbose: false
:wait_between_runs: 1
:proxy_uri:
:max_revisions: 5

codedeployagent.yml content

and then rebooted the machine. Still, this didn't fix the issue as when I run

sudo service codedeploy-agent status

will still get

Redirecting to /bin/systemctl status codedeploy-agent.service Unit codedeploy-agent.service could not be found.

Error remains

Also ensured all the updates were in place, rebooted the machine but that didn't work either.

Error remains even though updates are in place


Solution

  • I can provide details of my setup for Amazon Linux 2 instances to deploy CodeDeployGitHubDemo (based on past question).

    1. CodeDeploy agent

    Used the following as UserData (you may need to adjust region if not us-east-1):

    #!/bin/bash
    
    yum update -y
    yum install -y ruby wget
    
    cd /home/ec2-user
    
    wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/latest/install
    
    chmod +x ./install
    ./install auto
    

    It did not require hard-coding credentials. The following works perfectly fine on Amazon Linux 2 instances that I've used.

    2. Instance role

    Your instance needs a role suitable for CodeDeploy. I used an EC2 instance role with policy listed here:

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Action": [
                    "s3:Get*",
                    "s3:List*"
                ],
                "Effect": "Allow",
                "Resource": "*"
            }
        ]
    }
    

    3. Deployment group

    I had three instances for tests in an AutoScaling group, called myasg:

    enter image description here

    4. Deployment

    I deployed from S3 without Load Balancer:

    enter image description here

    5. Results

    No issues were found and deployment was successful:

    enter image description here

    And the website running (need to open port 80 in security groups):

    enter image description here

    Update

    For manual installation on Amazon Linux 2. You can sudo su - to become root after login.

    mkdir -p /etc/codedeploy-agent/conf
    
    cat <<EOT >> /etc/codedeploy-agent/conf/codedeploy.onpremises.yml
    ---
    
    aws_access_key_id: ACCESS
    
    aws_secret_access_key: SECRET
    
    iam_user_arn: arn:aws:iam::525221857828:user/GeneralUser
    
    region: eu-west-2
    
    EOT
    
    yum install -y wget ruby
    
    wget https://aws-codedeploy-us-west-2.s3.us-west-2.amazonaws.com/latest/install
    
    chmod +x ./install
    
    env AWS_REGION=eu-west-2 ./install rpm
    

    To check its status:

    systemctl status codedeploy-agent
    

    With this you should get something like this

    ● codedeploy-agent.service - AWS CodeDeploy Host Agent
       Loaded: loaded (/usr/lib/systemd/system/codedeploy-agent.service; enabled; vendor prese
    t: disabled)
       Active: active (running) since Sat 2020-10-03 07:18:57 UTC; 3s ago
      Process: 3609 ExecStart=/bin/bash -a -c [ -f /etc/profile ] && source /etc/profile; /opt
    /codedeploy-agent/bin/codedeploy-agent start (code=exited, status=0/SUCCESS)
     Main PID: 3623 (ruby)
       CGroup: /system.slice/codedeploy-agent.service
               ├─3623 codedeploy-agent: master 3623
               └─3627 codedeploy-agent: InstanceAgent::Plugins::CodeDeployPlugin::CommandPo...
    
    Oct 03 07:18:57 ip-172-26-8-137.eu-west-2.compute.internal systemd[1]: Starting AWS Cod...
    Oct 03 07:18:57 ip-172-26-8-137.eu-west-2.compute.internal systemd[1]: Started AWS Code...
    Hint: Some lines were ellipsized, use -l to show in full.
    

    If you run

    sudo service codedeploy-agent status
    

    you'll get (meaning it's working as expected)

    The AWS CodeDeploy agent is running as PID 3623
    

    It works!!

    To start if not running:

    systemctl start codedeploy-agent