AmitKumarDas / fun-with-programming

ABC - Always Be Coding
2 stars 2 forks source link

terranova #78

Closed AmitKumarDas closed 1 year ago

AmitKumarDas commented 3 years ago
//
//
// https://github.com/johandry/terranova
AmitKumarDas commented 3 years ago
// tags: How Terranova works?
//
// Code: It’s basically the content of the configuration file(s), 
// it may be a plain text or a Go template. This is the 
// Infrastructure as a code. This is very specific to Terranova.
//
// Providers: A Provider is the interface between terraform and
// the underlying platform which is usually an IaaS (i.e. AWS, GCP, 
// MS Azure, VMWare), PaaS (i.e. Heroku) or SaaS (i.e. DNSimple). 
// The entire list is here: https://www.terraform.io/docs/providers/
//
// Provisioners: The Provisioners are used to execute scripts on a 
// local or remote machine, transfer files to remote machines and 
// handling of configuration managers (i.e. Check, Salt). 
// To know more or get the entire list, check this out: 
// -- https://www.terraform.io/docs/provisioners/
//
// Variables: The Code may have references to terraform variables. 
// This may be optional as we can handle variables in different ways 
// in the Go code.
//
// State: This is final state of the infrastructure when the build or a 
// change is done. It’s important to keep the state in a safe place to 
// apply the further changes or destroy everything that was built.
// The only Provider that is loaded (go mod download) by default 
// is null. It’s a resource that allows you to configure provisioners 
// that are not directly associated with a single existing resource.
// The most common and useful provisioner to use are:
//
// -- file: used to copy files or directories from the local machine
// to the newly created resource.
//
// -- local-exec: invokes a local executable after a resource is 
// created on the local machine.
//
// -- remote-exec: invokes a script on a remote resource after 
// it is created.
AmitKumarDas commented 3 years ago
// http://blog.johandry.com/post/terranova-terraform-from-go/
//
// In this example the Terraform code is to create a given number 
// of AWS EC2 Ubuntu instances on the AWS region us-west-2. 
// Also to create a Key Pair made from the public key.

var code string

func init() {
  code = `
  variable "count"            {}
  variable "public_key_file"  { default = "~/.ssh/id_rsa.pub" }
  variable "private_key_file" { default = "~/.ssh/id_rsa" }
  locals {
    public_key    = "${file(pathexpand(var.public_key_file))}"
    private_key   = "${file(pathexpand(var.private_key_file))}"
  }
  provider "aws" {
    region        = "us-west-2"
  }
  resource "aws_instance" "server" {
    instance_type = "t2.micro"
    ami           = "ami-6e1a0117"
    count         = "${var.count}"
    key_name      = "server_key"

    provisioner "file" {
      content     = "ami used: ${self.ami}"
      destination = "/tmp/file.log"

      connection {
        user        = "ubuntu"
        private_key = "${local.private_key}"
      }
    }
  }
  resource "aws_key_pair" "keypair" {
    key_name    = "server_key"
    public_key  = "${local.public_key}"
  }
`
}