David Chou
Record cloud resource with IaC tool & source code, not document !
Terraform is an open source tool for managing Infrastructure as Code
and more...
A structured configuration language that is both human and machine friendly, and specifically targeted towards DevOps tools, etc.
resource "google_compute_instance" "vm_instance" {
name = "terraform-instance"
machine_type = "f1-micro"
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
network_interface {
# A default network is created for all GCP projects
network = "default"
access_config {
}
}
}Terraform flow
$ terraform init
To initialize a working directory containing Terraform configuration files
$ terraform plan
Terraform performs a refresh, then determines what actions are necessary to achieve the desired state specified in the configuration files.
$ terraform apply
Apply the changes required to reach the desired state of the configuration
#Configure aws with a default region
provider "google" {
credentials = file("~/.gcp/david74-service-account.json")
project = "gcp-playground-332207"
}
/*Create a demo GCS bucket*/
resource "google_storage_bucket" "auto-expire" {
name = "david74-auto-expiring-bucket"
location = "asia-east1"
lifecycle_rule {
condition {
age = 3
}
action {
type = "Delete"
}
}
}#Configure aws with a default region
provider "google" {
credentials = file("~/.gcp/david74-service-account.json")
project = "gcp-playground-332207"
}
/*Create a demo GCS bucket*/
resource "google_storage_bucket" "auto-expire" {
name = "david74-auto-expiring-bucket"
location = "asia-east1"
lifecycle_rule {
condition {
age = 9999
}
action {
type = "Delete"
}
}
}resource "google_storage_bucket" "auto-expire" {
name = "david74-auto-expiring-bucket-${var.env}"
location = "asia-east1"
lifecycle_rule {
condition {
age = var.bucket_age
}
action {
type = "Delete"
}
}
}variable "env" {
default = "local"
}
variable "bucket_age" {
default = 1
}
resource "google_storage_bucket" "auto-expire" {
name = "david74-auto-expiring-bucket-${var.env}"
location = "asia-east1"
lifecycle_rule {
condition {
age = var.bucket_age
}
action {
type = "Delete"
}
}
}variable "env" {
default = "local"
}
variable "bucket_age" {
default = 1
}
env = "staging"
bucket_age = 3
env = "production"
bucket_age = 365
terraform {
backend "gcs" {
credentials = "~/.gcp/david74-service-account.json"
bucket = "david74-terraform-remote-state-storage"
prefix = "terraform-gcp-gcs"
}
}terraform {
backend "gcs" {
bucket = "cresclab-terraform-remote-state-storage"
impersonate_service_account = "terraform@cresclab.iam.gserviceaccount.com"
prefix = "line-workshop-go"
}
}provider "aws" { region = "${var.requester_region}" profile = "${var.requester_aws_profile}" } module "vpc_peering" { source = "./vpc-peering" allow_remote_vpc_dns_resolution = "${var.allow_remote_vpc_dns_resolution}" # Requester Data requester_vpc_id = "${var.requester_vpc_id}" # Accepter Data accepter_aws_profile = "${var.accepter_aws_profile}" accepter_region = "${var.accepter_region}" accepter_vpc_id = "${var.accepter_vpc_id}" }
resource "google_compute_instance" "vm" {
/* ... */
}