Skip to content

Instantly share code, notes, and snippets.

@irvingpop
Last active September 7, 2020 11:49
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irvingpop/b7bc6d1684df6975de3e7eeb73d83e4e to your computer and use it in GitHub Desktop.
Save irvingpop/b7bc6d1684df6975de3e7eeb73d83e4e to your computer and use it in GitHub Desktop.
chef-solo like solution for Terraform
{
"mycookbook": {
"attribute1": "${attribute1}",
"attribute2": "${attribute2}"
},
"run_list": [
"recipe[${recipe}]"
]
}
# chef attributes file - takes in MySQL, Redis, etc connection info
data "template_file" "dna" {
template = "${file("dna.json.tpl")}"
vars {
attribute1 = "value1"
attribute2 = "value2"
recipe = "mycookbook::default"
}
}
# Package the cookbooks into a single file for easy uploading
resource "null_resource" "berks_package" {
# asuming this is run from a cookbook/terraform directory
provisioner "local-exec" {
command = "rm -f ${path.module}/cookbooks.tar.gz ; berks package ${path.module}/cookbooks.tar.gz --berksfile=../Berksfile"
}
}
# launch EC2 instance
resource "aws_instance" "my_server" {
count = 1
ami = "ami-20631a36"
instance_type = "t2.medium"
key_name = "id_rsa"
subnet_id = "${element(data.aws_subnet_ids.default-vpc.ids, count.index)}"
associate_public_ip_address = true
tags {
Name = "my-server-${count.index}"
}
connection {
user = "ubuntu"
private_key = "${file("~/.ssh/id_rsa")}"
}
provisioner "file" {
source = "${path.module}/cookbooks.tar.gz"
destination = "/tmp/cookbooks.tar.gz"
}
provisioner "file" {
content = "${data.template_file.dna.rendered}"
destination = "/tmp/dna.json"
}
provisioner "remote-exec" {
inline = [
"curl -LO https://www.chef.io/chef/install.sh && sudo bash ./install.sh",
"sudo chef-solo --recipe-url /tmp/cookbooks.tar.gz -j /tmp/dna.json"
]
}
depends_on = ["null_resource.berks_package"]
}
@ericpardee
Copy link

chef-solo is missing --chef-license accept-silent
thanks for sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment