Skip to content

Commit

Permalink
add organize and state modules
Browse files Browse the repository at this point in the history
  • Loading branch information
jimbol committed Feb 20, 2022
1 parent 37652e8 commit 2dd8eec
Show file tree
Hide file tree
Showing 21 changed files with 859 additions and 26 deletions.
52 changes: 52 additions & 0 deletions add-provider/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~>3.0"
}
google = {
source = "hashicorp/google"
version = "~>4.0"
}
}
}

provider "aws" {
region = "us-east-2"
profile = "default"
}

resource "aws_instance" "test_server" {
ami = "ami-0f19d220602031aed"
instance_type = "t2.nano"

key_name = "terraformclass"

tags = {
name: "Test server"
}
}

# Google Cloud infrastructure
provider "google" {
credentials = file(pathexpand("~/.config/gcloud/terraform-class-327014.json"))
region = "us-east1"
project = "terraform-class-327014"
}

resource "google_compute_instance" "test_gcp_instance" {
name = "test-gcp-instance-us-east1"
machine_type = "e2-micro"
zone = "us-east1-b"

boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}

network_interface {
network = "default"
access_config {}
}
}
25 changes: 0 additions & 25 deletions basic-grammar/main.tf

This file was deleted.

80 changes: 80 additions & 0 deletions guide/ADD_PROVIDER_RESOURCE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Add another provider
Let's practice adding another provider. You can have one or many providers for a given project.

The complete example can be found in the [add-provider](../add-provider) folder.

## Google Cloud to Terraform Block
First we can update the terraform block to lock us into a major version of the Google provider.

```tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~>3.0"
}
google = {
source = "hashicorp/google"
version = "~>4.0"
}
}
}
```

Both AWS and Google co-manage the providers, so we can be sure the implementation is sound.


## Add Google Cloud provider
To continue we need a Google Cloud "project" and credentials on our computer. [Here is a guide to setting up a Google Cloud project.](https://cloud.google.com/resource-manager/docs/creating-managing-projects)

Lower in our file we can include the Google Cloud provider.

```tf
# Google Cloud infrastructure
provider "google" {
credentials = file(pathexpand("~/.config/gcloud/terraform-class-327014.json"))
region = "us-east1"
project = "terraform-class-327014"
}
```

## Add Google compute instance
Now that the Google cloud provider has been included, we can add a compute instance.

```tf
resource "google_compute_instance" "test_gcp_instance" {
name = "test-gcp-instance-us-east1"
machine_type = "e2-micro"
zone = "us-east1-b"
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
network_interface {
network = "default"
access_config {}
}
}
```

I can use the gcloud cli to connect to that instance. [Here is a guide to set up gcloud cli.](https://cloud.google.com/sdk/docs/install).

```
gcloud compute ssh --zone "us-east1-b" "test-gcp-instance-us-east1" --project "terraform-class-327014"
```

## Arguments, Requirements, and Implementation
Resources in Terraform tend to be very well documented. We can see the docs for google_compute_instance [here](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_instance).

We can see the all the argument options. There are a lot of them because we can do a lot with google_compute_instance. But we really only need the *required* arguments in order to deploy something.

All this information is tied to the provider. Given a valid configuration, the provider will call the gcloud commands required deploy the infrastructure.

1. It compares what is in the configurations with what is live in the cloud.
2. It prepares a list of changes that need to be made.
3. It uses Google's Cloud Shell to make changes to GCP.

[Next: State](STATE.md)
3 changes: 2 additions & 1 deletion guide/INTRO.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ Examples of infrastructure as code tools:
## What is Terraform
Terraform is HashiCorp's infrastructure as code tool. It uses the HashiCorp configuration language to allow devops engineers to write readable, declarative, reusable configurations for infrastructure. It also provids a command-line tool that provides a straight-forward workflow for updates.

Terraform has some advantages over other IoT options:
Terraform has some advantages over other IaC options:
- You can use it to deploy to multiple cloud platforms and services.
- It used HashiCorp Configuration Language (HCL) which is easy to read, understand, and write.
- It has a built-in workflow.
- Provider specific definitions

## Terraform Use Cases
Terraform use cases are as varied as all devops projects.
Expand Down
Loading

0 comments on commit 2dd8eec

Please sign in to comment.