Last active
September 7, 2020 11:49
-
-
Save irvingpop/b7bc6d1684df6975de3e7eeb73d83e4e to your computer and use it in GitHub Desktop.
chef-solo like solution for Terraform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"mycookbook": { | |
"attribute1": "${attribute1}", | |
"attribute2": "${attribute2}" | |
}, | |
"run_list": [ | |
"recipe[${recipe}]" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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"] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
chef-solo is missing
--chef-license accept-silent
thanks for sharing