Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (5.6k points)
Defining user-data for instances in AWS seems really useful for doing all kinds of bootstrap-type actions. But I have to use a custom CentOS AMI that didn't originate from one of the provided AMI for PCI reasons, so cloud-init is not really installed and configured. How do I get it working?

1 Answer

0 votes
by (12.4k points)

Cloud-init is a very powerful, but undocumented tool. Even once installed there are a lot of modules active by default that overwrites things that you may have defined on your AMI.

So here are the instructions for a minimal setup from scratch:

Instructions

Install cloud-init from a standard repository. If you're worried about PCI, you probably don't want to use AWS's custom repositories.

# rpm -Uvh https://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

# yum install cloud-init

Edit /etc/cloud/cloud.cfg, a YAML file, to reflect your desired configuration. Below is a minimal configuration with the documentation for each module.

#If this is not explicitly false, cloud-init will change things so that root

#login via ssh is disabled. If you don't want it to do anything, set it false.

disable_root: false

#Set this if you want cloud-init to manage hostname. The current

#/etc/hosts file will be replaced with the one in /etc/cloud/templates.

manage_etc_hosts: true

#Since cloud-init runs at multiple stages of boot, this needs to be set so

#it can log in all of them to /var/log/cloud-init.

syslog_fix_perms: null

#This is the bit that makes userdata work. You need this to have userdata

#scripts be run by cloud-init.

datasource_list: [Ec2]

datasource:

  Ec2:

    metadata_urls: ['http://169.254.169.254']

#modules that run early in boot

cloud_init_modules:

 - bootcmd  #for running commands in pre-boot. Commands can be defined in cloud-config userdata.

 - set-hostname  #These 3 make hostname setting work

 - update-hostname

 - update-etc-hosts

#modules that run after boot

cloud_config_modules:

 - runcmd  #like bootcmd, but runs after boot. Use this instead of bootcmd unless you have a good reason for doing so.

#modules that run at some point after config is finished

cloud_final_modules:

 - scripts-per-once  #all of these run scripts at specific events. Like bootcmd, can be defined in cloud-config.

 - scripts-per-boot

 - scripts-per-instance

 - scripts-user

 - phone-home  #if defined, can make a post request to a specified url when done booting

 - final-message  #if defined, can write a specified message to the log

 - power-state-change  #can trigger stuff based on power state changes

system_info:

  #works because amazon's linux AMI is based on CentOS

  distro: amazon

If there is a defaults.cfg in /etc/cloud/cloud.cfg.d/, delete it.

To take advantage of this configuration, define the following user-data for new instances:

#cloud-config

hostname: myhostname

fqdn: myhostname.mydomain.com

runcmd:

 - echo "I did this thing post-boot"

 - echo "I did this too"

Do Check out the AWS Certification Course offered by Intellipaat.

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...