简体   繁体   中英

AWS cloud-init configuration file

I have a ec2 instance in aws that is controlled by a auto scaling group and launch configuration.

The launch configuration file, initially all what had to do is install nginx. As follow:

#cloud-config

package_update: true
package_upgrade: false

packages:
  - nginx

write_files:
  - content: |
        (NGINX FILE CONFIGURATION HERE
    path: /etc/nginx/nginx.conf
runcmd:
- nginx -s reload
- sudo mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.txt
- sudo systemctl restart nginx

This was working just perfectly fine. No I wanted to install awslogs, I am on a ubuntu ec2 instance which involves download the script and executing it.

I wanted to make this process through the cloud-ini as I need this configured on all the instances that they will be created by the auto scaling.

so I updated my cloud-init script as follow:

#cloud-config

package_update: true
package_upgrade: false

packages:
  - nginx

write_files:
  - content: |
        (NGINX FILE)
    path: /etc/nginx/nginx.conf  

runcmd:
- nginx -s reload
- sudo mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.txt
- sudo systemctl restart nginx
- curl https://s3.amazonaws.com//aws-cloudwatch/downloads/latest/awslogs-agent-setup.py -O
- sudo python3 awslogs-agent-setup.py --region eu-west-1


write_files:
  - content: |
        [/var/log/nx]
        log_stream_name = {instance_id}
        initial_position = start_of_file
        datetime_format = %d/%b/%Y:%H:%M:%S
        file = /var/log/nginx/access.log
        log_group_name = nginx-logs
        buffer_duration = 5000
        
    path: /var/awslogs/etc/awslogs.conf

runcmd:
- sudo systemctl start awslogs
- sudo systemctl enable awslogs
- sudo systemctl reload awslogs

This of course gives me an error that I cannot have 2 runcmd or 2 write_files.

Here is where I am totally stuck.

How can I replace 2 content file in one launch configuration cloud init?

The awslogs for some reasons when you run it (contrary to yum version) it force you to configure the file, through the terminal. Which is not ideal and I need to create those resource in this specific order

  • nginx
  • replace nginx configuration file
  • install awslogs
  • replace aws Configuration file.

Please I am really lost and struggling here if anyone can help or guide me through this approach I would so much appreciate it.

UPDATE: The cloud-init fails at run command to install the awslogs agent. The installation requires some manual inputs, which basically are the second content in my write file. But as this is a terminal process, it gets stuck there throwing the error:

Step 1 of 5: Installing pip ...libyaml-dev does not exist in system python-dev does not exist in system DONE

Step 2 of 5: Downloading the latest CloudWatch Logs agent bits ... DONE

Step 3 of 5: Configuring AWS CLI ... 
AWS Access Key ID [None]: 
EOF when reading a line

Step 4 of 5: Configuring the CloudWatch Logs Agent ... 
Path of log file to upload [/var/log/syslog]: Traceback (most recent call last):
  File "awslogs-agent-setup.py", line 1365, in <module>
    main()
  File "awslogs-agent-setup.py", line 1361, in main
    setup.setup_artifacts()
  File "awslogs-agent-setup.py", line 914, in setup_artifacts
    self.aws_logs_configure()
  File "awslogs-agent-setup.py", line 1015, in aws_logs_configure
    log_file_path = prompter.get_value(default_log_file_path, file_path_msg)
  File "awslogs-agent-setup.py", line 1207, in get_value
    response = input("%s [%s]: " % (prompt_text, current_value))
EOFError: EOF when reading a line

This process happens on with ubuntu, the yum command installs directly awslogs and later you can configure the file

How can I replace 2 content file in one launch configuration cloud init?

Just combine them:

#cloud-config

package_update: true
package_upgrade: false

packages:
  - nginx

write_files:
  - content: |
        (NGINX FILE)
    path: /etc/nginx/nginx.conf  
  - content: |
        [/var/log/nx]
        log_stream_name = {instance_id}
        initial_position = start_of_file
        datetime_format = %d/%b/%Y:%H:%M:%S
        file = /var/log/nginx/access.log
        log_group_name = nginx-logs
        buffer_duration = 5000
    path: /var/awslogs/etc/awslogs.conf

runcmd:
  - nginx -s reload
  - sudo mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.txt
  - sudo systemctl restart nginx
  - curl https://s3.amazonaws.com//aws-cloudwatch/downloads/latest/awslogs- agent-setup.py -O
  - sudo python3 awslogs-agent-setup.py --region eu-west-1
  - sudo systemctl start awslogs
  - sudo systemctl enable awslogs
  - sudo systemctl reload awslogs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM