Defining userdata for instances in AWS seems really useful for doing all kinds of bootstrap-type actions. Unfortunately, I have to use a custom CentOS AMI that didn't originate from one of the provided AMIs for PCI reasons, so cloud-init is not already installed and configured. I only really want it to set a hostname and run a small bash script. How do I get it working?
相关问题
- How to generate 12 digit unique number in redshift
- Use awslogs with kubernetes 'natively'
- Assume/switch role in aws toolkit for eclipse 2.0
- 'no SavedModel bundles found!' on tensorfl
- Installing Python dependencies in AWS Codestar wit
相关文章
- springboot 站点 部署到centos 不用安装tomcat 哇
- linux 下运行selenium报错?
- 为什么nfs在不同版本的Linux下安装的文件都不一样
- Right way to deploy Rails + Puma + Postgres app to
- how many objects are returned by aws s3api list-ob
- AWS S3 in rails - how to set the s3_signature_vers
- Passthrough input to output in AWS Step Functions
- I got error in run time that “Cannot assign reques
Expanding on the prior answer for anyone trying to create a CentOS AMI that is
cloud-init
enabled (and capable of actually executing your CloudFormation scripts), you might have some success by doing the following:sudo yum install -y cloud-init
rm -rf /var/lib/cloud/data
rm -rf /var/lib/cloud/instance
rm -rf /var/lib/cloud/instances/*
/etc/cloud/cloud.cfg
with the configuration in the answer above but make sure you setdistro: rhel
Had a heck of a time trying to figure out why my UserData was not being invoked until I realized that the images in the marketplace naturally only run your UserData once per instance AND of course they had already run. Removing the indicators that those had already been executed along with changing the
distro: rhel
in thecloud.cfg
file did the trick.For the curious, the
distro:
value should correspond to one of the python scripts in/usr/lib/python2.6/site-packages/cloudinit/distros
. As it turns out the AMI I launched had noamazon.py
, so you need to userhel
for CentOS. Depending on the AMI you launch and the version of cloud-init, YMMV.cloud-init is a very powerful, but very undocumented tool. Even once it's installed, there are lot of modules active by default that overwrite things you may have already defined on your AMI. Here are 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.
Edit
/etc/cloud/cloud.cfg
, a yaml file, to reflect your desired configuration. Below is a minimal configuration with documentation for each module.If there is a
defaults.cfg
in/etc/cloud/cloud.cfg.d/
, delete it.To take advantage of this configuration, define the following userdata for new instances:
You can also simply run a bash script by replacing
#cloud-config
with#!/bin/bash
and putting the bash script in the body, but if you do, you should remove all of the hostname-related modules fromcloud_init_modules
.Additional Notes
Note that this is a minimal configuration, and cloud-init is capable of managing users, ssh keys, mount points, etc. Look at the references below for more documentation on those specific features.
In general, it seems that cloud-init does stuff based on the modules specified. Some modules, like "disable-ec2-metadata", do stuff simply by being specified. Others, like "runcmd", only do stuff if their parameters are specified, either in cloud.cfg, or in cloud-config userdata. Most of the documentation below only tell you what parameters are possible for each module, not what the module is called, but the default cloud.cfg should have a complete module list to begin with. The best way I've found to disable a module is simply to remove it from the list.
In some cases, "rhel" may work better for the "distro" tag than "amazon". I haven't really figured out when.
References
Here is a brief tutorial on how to run scripts during startup using cloud-init on AWS EC2 (CentOS).
Configuration file
The configuration file below is on AWS CentOS6. For Amazon Linux, see here.
Directory Tree
Here is what the cloud path
/var/lib/cloud/scripts
looks like:Content of the Script Files
Here are the contents of the example script files.
The files have to be under user
root
. See my way on creating the boot script.Result of Execution
In the case of initial start-up
In the case of a reboot
In the case of start from in the AMI
Reference
The timing at which the script is run in cloud-init (CentOS6) was examined (translated)