Init shell command issue

0

Hi, This works on an Amazon AMI: ec2.InitCommand.shellCommand('sudo ls /etc/resolv.conf')

and so does this ec2.InitFile.fromFileInline('/etc/resolv2.conf', './myContent/resolv2.conf')

and this ec2.InitCommand.shellCommand('sudo chmod 777 /etc/resolv.conf')

but this doesn't ec2.InitCommand.shellCommand('sudo rm -f /etc/resolv.conf')

and neither does this ec2.InitCommand.shellCommand('sudo sed -i "s/^nameserver/#nameserver/g" /etc/resolv.conf').

I think it must be a permissions thing but I can't figure it out. The ls -al output is here:

-rw-r--r-- 1 root root 121 Apr 30 12:37 /etc/resolv.conf

File contents are:

; generated by /sbin/dhclient-script

search eu-west-3.compute.internal

options timeout:2 attempts:5

nameserver 10.10.0.2

Any thoughts would be appreciated

Thanks, Gary

gary
asked a year ago430 views
2 Answers
1
Accepted Answer

Check the log output in "/var/log/cloud-init-output.log".
See if there are any errors or other problems.

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html

Also, by default, InitFile is run as the root user, so errors around permissions are unlikely to occur.

profile picture
EXPERT
answered a year ago
  • There were no errors in /var/log/cloud-init-output.log. In fact, when I look at /etc/resolv.config, the changes have been made, but, and the bit I forgot to mention is, the error message "Failed to receive 1 resource signal(s) within the specified duration". Is it possible that certain linux commands don't work well with init-signal in certain situations?

  • This document describes how to troubleshoot that error when it appears. https://repost.aws/knowledge-center/cloudformation-failed-signal Basically, we recognize that Linux commands can be executed with cloud-init.

  • ec2.InitCommand.shellCommand('rm -f /etc/securetty'), => file is removed and signal returned to cfn/cdk

    ec2.InitCommand.shellCommand('rm -f /etc/resolv.conf') => file is removed and no signal returned to cdk/cfn. As a result, "The following resource(s) failed to create: [myclientami5D1A95132]" was assumed, even though it was up and running before the rollback. Not sure if the troubleshooting doc can help anymore

  • Are there any errors in logs such as "/var/log/cloud-init.log" and "/var/log/cfn-init.log"?

  • more /var/log/cloud-init.log | grep resolv

    Apr 30 16:06:28 cloud-init[2518]: stages.py[INFO]: Skipping modules ['resolv-conf'] because they are not verified on distro 'amazon'. To run anyway, add them to 'unverified_modules' in config. Apr 30 16:06:28 cloud-init[2518]: stages.py[DEBUG]: Running module resolv-conf (<module 'cloudinit.config.cc_resolv_conf' from '/usr/lib/python2.7/dist-packages/cloudinit/config/cc_resolv_conf.pyc'>) with frequency once-per-instance Apr 30 16:06:28 cloud-init[2518]: util.py[DEBUG]: Writing to /var/lib/cloud/instances/i-0d74593409a869245/sem/config_resolv_conf - wb: [644] 20 bytes Apr 30 16:06:28 cloud-init[2518]: helpers.py[DEBUG]: Running config-resolv-conf using lock (<FileLock using file '/var/lib/cloud/instances/i-0d74593409a869245/sem/config_resolv_conf'>) Apr 30 16:06:28 cloud-init[2518]: cc_resolv_conf.py[DEBUG]: Skipping module named resolv-conf, no 'manage_resolv_conf' key in configuration

1

Thanks for the extra insight. I have it working with bash but I'll try userdata and take a look at the link when I get another chance.

gary
answered a year 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.

Guidelines for Answering Questions