├── .gitignore ├── provider.tf ├── USAGE.md ├── create-instance.tf └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # credentials 2 | credentials.json 3 | # Local .terraform directories 4 | **/.terraform/* 5 | # .tfstate files 6 | *.tfstate 7 | *.tfstate.* 8 | *.DS_Store -------------------------------------------------------------------------------- /provider.tf: -------------------------------------------------------------------------------- 1 | # Specify the provider (GCP, AWS, Azure) 2 | provider "google" { 3 | credentials = "${file("credentials.json")}" 4 | project = "ac-shared-playground" 5 | region = "us-central1" 6 | } -------------------------------------------------------------------------------- /USAGE.md: -------------------------------------------------------------------------------- 1 | # Using GCP and Terraform 2 | 3 | ## Installation 4 | 1. Clone project locally 5 | ```bash 6 | $ git clone https://github.com/thiagofernandocosta/gcp_vm-in-few-steps && cd gcp_vm-in-few-steps 7 | ``` 8 | 9 | 2. Install **Terraform** 10 | 11 | `Linux` 12 | ```bash 13 | $ wget -q https://releases.hashicorp.com/terraform/0.11.6/terraform_0.11.6_linux_amd64.zip 14 | $ unzip terraform_0.11.6_linux_amd64.zip 15 | $ sudo mv terraform /usr/local/bin/terraform 16 | $ terraform version 17 | ``` 18 | `Mac` 19 | ```bash 20 | $ brew install terraform 21 | $ terraform version 22 | ``` 23 | 24 | ## Usage 25 | ### Running and getting up 26 | After installing terraform and configuring google cloud account. We have to run application using `terraform`, let's go. 27 | 28 | There are many arguments in terraform, but this case we just wanna run application for first steps. 29 | 30 | 1. **terraform commands** 31 | * `terraform init` 32 | This command sets up the environment. 33 | 34 | * `terraform plan` 35 | This command reports configuration will be applied. 36 | 37 | * `terraform apply -auto-approve` 38 | This command applies configuration defined on terraform files approving automatically changes. 39 | 40 | * `terraform destroy -auto-approve` 41 | Against of command above, this remove everything created. 42 | 43 | **It's important to know, but not explained deeply in this tutorial that terraform maintains a state from your infrastructure.** -------------------------------------------------------------------------------- /create-instance.tf: -------------------------------------------------------------------------------- 1 | resource "google_compute_instance" "default" { 2 | name = "virtual-machine-from-terraform" 3 | machine_type = "f1-micro" 4 | zone = "us-central1-a" 5 | 6 | boot_disk { 7 | initialize_params { 8 | image = "debian-cloud/debian-9" 9 | } 10 | } 11 | 12 | network_interface { 13 | network = "default" 14 | 15 | access_config { 16 | // Include this section to give the VM an external ip address 17 | } 18 | } 19 | 20 | metadata_startup_script = "sudo apt-get update && sudo apt-get install apache2 -y && echo '

Avenue Code is the leading software consulting agency focused on delivering end-to-end development solutions for digital transformation across every vertical. We pride ourselves on our technical acumen, our collaborative problem-solving ability, and the warm professionalism of our teams.!

' | sudo tee /var/www/html/index.html" 21 | 22 | // Apply the firewall rule to allow external IPs to access this instance 23 | tags = ["http-server"] 24 | } 25 | 26 | resource "google_compute_firewall" "http-server" { 27 | name = "default-allow-http-terraform" 28 | network = "default" 29 | 30 | allow { 31 | protocol = "tcp" 32 | ports = ["80"] 33 | } 34 | 35 | // Allow traffic from everywhere to instances with an http-server tag 36 | source_ranges = ["0.0.0.0/0"] 37 | target_tags = ["http-server"] 38 | } 39 | 40 | output "ip" { 41 | value = "${google_compute_instance.default.network_interface.0.access_config.0.nat_ip}" 42 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GCP Virtual machine in few steps 2 | 3 | Deploy Virtual machine on Google Cloud Plataform (GCP) and set up initial content with nginx via terraform. 4 | 5 | This project requires: 6 | - [Terraform](https://www.terraform.io/ "Terraform") 7 | - [Google Account](https://cloud.google.com/ "Google Account") 8 | 9 | ![Terraform and Google Cloud Plataform](https://cdn-images-1.medium.com/max/1600/1*b5s-MJeJMdDgq4sqnuMfBg.png?raw=true) 10 | 11 | ## Installation and Usage 12 | See [**Installation and Usage**](USAGE.md) for instructions on getting up and running. 13 | Visit [USAGE.md](USAGE.md "Installation & Usage"). 14 | 15 | ## How It Works 16 | * **Google Cloud Plataform** 17 | * Google Cloud Platform (GCP), offered by Google, is a suite of cloud computing services that runs on the same infrastructure that Google uses internally for its end-user products. 18 | * Alongside a set of management tools, it provides a series of modular cloud services including computing, data storage, data analytics and machine learning. Registration requires a credit card or bank account details. 19 | * Google Cloud Platform provides infrastructure as a service, platform as a service, and serverless computing environments. 20 | 21 | * **Terraform** 22 | * Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions. 23 | * Configuration files describe to Terraform the components needed to run a single application or your entire datacenter. Terraform generates an execution plan describing what it will do to reach the desired state, and then executes it to build the described infrastructure. As the configuration changes, Terraform is able to determine what changed and create incremental execution plans which can be applied. 24 | * The infrastructure Terraform can manage includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc. --------------------------------------------------------------------------------