user11950120
user11950120

Reputation:

AWS Cloudwatch Agent in a docker container

I am trying to set up Amazon Cloudwatch Agent to my docker as a container. This is an OnPremise installation so it's running locally, not inside AWS Kubernetes or anything of the sorts.

I've set up a basic dockerfile, agent.json and .aws/ folder for credentials and using docker-compose build to actually set it up, then launch it, but I am running into constant problems because Docker does not contain or run systemctl so I cannot run the service using AWS own documentation command:

/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m onPremise -c file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json -s

This will fail on an error when I try to run the container:

cloudwatch_1  | /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl: line 262: systemctl: command not found
cloudwatch_1  | unknown init system

I've tried to run the /start-amazon-cloudwatch-agent inside /bin as well, but no luck. No documentation on this.

Basically the issue is how can I run this as a service or a process in the foreground? Anyone have any clues? Otherwise the container won't stay up. Below is my code:

dockerfile

FROM amazonlinux:2.0.20190508
RUN yum -y install https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm
COPY agent.json /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json
CMD /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m onPremise -c file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json

agent.json

{
    "agent": {
        "metrics_collection_interval": 60,
        "region": "eu-west-1",
        "logfile": "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log",
        "debug": true
    }
}

.aws/ folder contains config and credentials, but I never got as far for the agent to actually try and make a connection.

Upvotes: 5

Views: 11674

Answers (2)

LinPy
LinPy

Reputation: 18598

just use the official image docker pull amazon/cloudwatch-agent it will handle all the things for you; REF: here

if you insist on using your own, try the following:

FROM amazonlinux:2.0.20190508
RUN yum -y install https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm
COPY agent.json  /opt/aws/amazon-cloudwatch-agent/bin/default_linux_config.json
ENV RUN_IN_CONTAINER=True
ENTRYPOINT ["/opt/aws/amazon-cloudwatch-agent/bin/start-amazon-cloudwatch-agent"]

Upvotes: 2

GusDeCooL
GusDeCooL

Reputation: 5758

Use the AWS official Docker Image, here is the example of the docker compose

version: "3.8"
services:
  agent:
    image: amazon/cloudwatch-agent:1.247350.0b251814
    volumes:
      - ./config/log-collect.json:/opt/aws/amazon-cloudwatch-agent/bin/default_linux_config.json # agent config
      - ./aws:/root/.aws # required for authentication
      - ./log:/log # sample log
      - ./etc:/opt/aws/amazon-cloudwatch-agent/etc # for debugging the config of AWS of container

From config above, only the first 2 volume sync required. Number 3 & 4 is for debug purpose.

If you interested in learning what each volumes does, you can read more at https://medium.com/@gusdecool/setup-aws-cloudwatch-agent-on-premise-server-part-1-31700e81ab8

Upvotes: 1

Related Questions