Hi Team, How to use Chef cookbook here with 0.15 version? please share the steps to download cookbooks from chef-server and install them on AWS instance. I am using provisioner “chef” in resource “aws_instance” " " { }, but failing with “chef” removed in this version. Then how i can download my cookbook from chef-server and how I can run my receipt with terraform 0.15
There are a few different ways to get this done. If you are using a Linux image which runs cloud-init at boot (most “official” Linux distribution images do) then I would suggest trying cloud-init’s chef module, which can install and run Chef in a similar way than Terraform’s old provisioner would’ve, but without any need for Terraform to SSH into the system to run it.
You can pass a configuration to cloud-init using the user_data
argument for aws_instance
. For example:
resource "aws_instance" "example" {
# ...
user_data = <<-EOT
#cloud-config
${yamlencode({
chef = {
server_url = "https://chef.example.com:4000"
directories = [
"/etc/chef",
"/var/log/chef",
]
validation_cert = "system"
install_type = "omnibus"
run_list = [
"recipe[apache2]",
"role[db]",
]
exec = true
# ...
}
})}
EOT
}
My example above is using Terraform’s yamlencode
option to produce YAML configuration for cloud-config from a Terraform data structure. The arguments I used above are just examples I took from the example in the cloud-init Chef module documentation; you can see what they all mean and what other options are available in the cloud-init documentation.
Cloud-init is piece of software that typically runs early in the boot process for an EC2 instance whose AMI includes it. Cloud-init will use the EC2 metadata API to retrieve your user_data
string, parse it, and then take the actions you’ve described while the system is booting. This means that the cloud-init actions will happen far sooner in the boot process than a Terraform provisioner could, because they can run before an SSH server is even running. That’s one of the reasons why provisioners are a last resort.