- sudo apt-get -y install / sudo yum install -y zip unzip (if these are not installed)
- wget https://releases.hashicorp.com/terraform/0.9.8/terraform_0.9.8_linux_amd64.zip
- unzip terraform_0.9.8_linux_amd64.zip
- sudo mv terraform /usr/local/bin/
- Confirm terraform binary is accessible: terraform --version
-
Generate a api token from the digitalocean UI and export it to the terminal,
$export DO_PAT=YOUR_PERSONAL_ACCESS_TOKEN
-
Get the MD5 SHA of you rsa_pub key:
To get the SHA values:
$ssh-keygen -E md5 -lf ~/.ssh/id_rsa.pub | awk '{print $2}' MD5:68:d3:3e:fr:rg:rt:67:67:34:56:67:as:rf
2.2 Add you rsa_pub key to your Digital ocean
-
Above are the variables which we will be using in our droplett creation
-
Initailase the project directory.
$terraform init
-
Now test the project sing the terraform plan, it downloads the respective plugin.
$terraform plan \ -var "do_token=${DO_PAT}" \ -var "pub_key=$HOME/.ssh/id_rsa.pub" \ -var "pvt_key=$HOME/.ssh/id_rsa" \ -var "ssh_fingerprint=your_SHA_value" #with out MD5 prefix 68:d3:3e:fr:rg:rt:67:67:34:56:67:as:rf
-
Now create the provider.tf file which has all the variables that we will be using in the project. $ vi mytest.tf
variable "do_token" {} variable "pub_key" {} variable "pvt_key" {} variable "ssh_fingerprint" {} provider "digitalocean" { token = "${var.do_token}" }
Save and Exit
-
Now create the mytest.tf file which contains the resourse of creating the droplets.
$vi mytest.tf
resource "digitalocean_droplet" "web" { image = "ubuntu-16-04-x64" name = "mytest" region = "blr1" size = "s-1vcpu-1gb" private_networking = true ssh_keys = [ "${var.ssh_fingerprint}" ] connection { user = "root" type = "ssh" private_key = "${file(var.pvt_key)}" timeout = "2m" } provisioner "remote-exec" { inline = [ "export PATH=$PATH:/usr/bin", # install tomcat "sudo apt-cache search apache", "sudo apt-get install apache2" # install nginx #"sudo apt-get update", #"sudo apt-get -y remove nginx", ] } }
Save and Exit.
-
Now run the terraform plan and then apply cmd.
$terraform plan \ -var "do_token=${DO_PAT}" \ -var "pub_key=$HOME/.ssh/id_rsa.pub" \ -var "pvt_key=$HOME/.ssh/id_rsa" \ -var "ssh_fingerprint=your SHA value"
(It diplays the output stating "mytest" droplet will be created)
$terraform apply \ -var "do_token=${DO_PAT}" \ -var "pub_key=$HOME/.ssh/id_rsa.pub" \ -var "pvt_key=$HOME/.ssh/id_rsa" \ -var "ssh_fingerprint=your SHA value"
(It launches a droplet and install Apache at port 80)