1

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

Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145

1 Answers1

3

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
Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • Just tested real quick to create two Lightsail instances in Virginia: one using the script you shared as start and the other not (and I ran it manually). In both reached the same error when running `sudo service codedeploy-agent status`. Not sure if [this](https://stackoverflow.com/a/64040145/5675325) is having an impact – Tiago Martins Peres Oct 02 '20 at 11:58
  • @TiagoMartinsPeres李大仁 I don't have lightsale, but if its Amazon Linux 2 I don't see why would it be different. You used my script in `UserData`? – Marcin Oct 02 '20 at 12:02
  • in one instance I added it as a launch script (defined as «You can add a shell script that will run on your instance the first time it launches.») and in the other I ran the commands manually but in both hit the same wall – Tiago Martins Peres Oct 02 '20 at 12:05
  • On the instance with my script, can you run `cat /var/log/cloud-init-output.log`. If my `UserData` fails, there should be an error message somewhere there. – Marcin Oct 02 '20 at 12:09
  • I'm trying to update the question with that information but when I select all the text it shows "Connecting to your instance" and blocks from copying. Also installed xclip but it says `Error: Can't open display: (null)`. But i don't see any error in it though – Tiago Martins Peres Oct 02 '20 at 12:29
  • 1
    just checked these steps again and got into a problem too big to describe here (so created a [new question](https://stackoverflow.com/q/64176881/5675325)). I'm still wondering the reason Amazon Linux 2 leads to error when initially ran `sudo service codedeploy-agent status` and in Amazon Linux worked fine right away. But thank you for the insisting in the manual deployment without pipeline, I could visualize things better now by opening the application and seeing not only the groups but its deployments. – Tiago Martins Peres Oct 02 '20 at 18:58
  • @TiagoMartinsPeres李大仁 Thank for letting me know. So this question is still opened, or the CD was installed? Because without CD agent, your new question will also not work? – Marcin Oct 02 '20 at 22:57
  • It's still open yes @Marcin – Tiago Martins Peres Oct 03 '20 at 00:10
  • 1
    @TiagoMartinsPeres李大仁 I updated the answer with new manual installation procedure for tests. Can you check it? Best to try on fresh instance, as existing one may already have some settings corrupted. – Marcin Oct 03 '20 at 02:32
  • 1
    You rock @Marcin! That new updated script was what was missing. The difference was the way the /etc/codedeploy-agent/conf was built, not specifying the bucket name and including the env at the end too. – Tiago Martins Peres Oct 03 '20 at 07:24