# GCP Bucket with Terraform

## Enable the Below API

Cloud DNS API\
Compute Engine\
IAM & Admin

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/6PAoQ1J2epXGp9oqIwyN/image.png" alt=""><figcaption></figcaption></figure>

## Create a New Service Account&#x20;

Goto the services Account  ->  Create A Service Account  ->  fill all the mension Details  ->  Create  ->  Assign the role - basic - OWner   ->  Create the account

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/HXfdj33RTDrdugP5D4Dd/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/HC4y0SGxwVUkahSNsOph/image.png" alt=""><figcaption></figcaption></figure>

NOw, Account Created  -&#x20;

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/v1I2vX1fPsl449CJgKbZ/image.png" alt=""><figcaption></figcaption></figure>

### Assign the API Key -

Double click on the Service account  ->  Keys  ->  Add Key -> Create New Key ->  Choose the key type - JSON (Recommended)   ->  Create

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/1wlI61tczsmOTpp0WivQ/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/28JXQMfeW6WYgce9JYJw/image.png" alt="" width="563"><figcaption></figcaption></figure>

Then, This API Key Download on the Local Machine.

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/AT8j8D2XdrSDBPr1qzBJ/image.png" alt=""><figcaption></figcaption></figure>

## Installing the terraform -

Use this official Document&#x20;

<https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli>

#### <mark style="color:red;">Repository link -</mark>

```
https://github.com/Bhushan0151/freecodecamp-terraform-with-gcp.git
```

### main.tf

```
resource "google_storage_bucket" "website" {
  provider = google
  name     = "example-Bhushan-coffee7"
  location = "US"
}

# Make new objects public
resource "google_storage_object_access_control" "public_rule" {
  object = google_storage_bucket_object.static_site_src.output_name
  bucket = google_storage_bucket.website.name
  role   = "READER"
  entity = "allUsers"
}
#resource "google_storage_default_object_access_control" "website_read" {
#  bucket = google_storage_bucket.website.name
#  role   = "READER"
#  entity = "allUsers"
#}

# Upload the html file to the bucket
resource "google_storage_bucket_object" "static_site_src" {
  name   = "index.html"
  source = "../website/index.html"
  bucket = google_storage_bucket.website.name
  
}

# Reserve an external IP
resource "google_compute_global_address" "website" {
  provider = google
  name     = "website-lb-ip"
}

# Get the managed DNS zone
data "google_dns_managed_zone" "gcp_coffeetime_dev" {
  provider = google
  name     = "Bhushan-example"
}

```

Create A Directory and it in the code Editor or VS code -

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/y5TmWno6hCDbvxdZugD0/image.png" alt=""><figcaption></figcaption></figure>

File Strcture -

<figure><img src="https://914089386-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNPLqce0CFOiVM3fTJRBY%2Fuploads%2FKXeYpUfVgJtBzGOk2cfd%2Fimage.png?alt=media&#x26;token=0df044c4-590c-4d06-93ad-1dbe399103a9" alt=""><figcaption></figcaption></figure>

#### GCP-terraform -> Infra -> main.tf ,  variable.tf , Output.tf , provider.tf

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/vs7IVMavNgnhI2lqdiQZ/image.png" alt=""><figcaption></figcaption></figure>

### terraform init&#x20;

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/gXmDS34OYrLhqazxDimf/image.png" alt=""><figcaption></figcaption></figure>

### terraform validate

### terraform plan

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/hZ4r2Y7pQt2CmC5VOi0y/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/VaxKKPW3dgeX5wuzDoB5/image.png" alt=""><figcaption></figcaption></figure>

### terraform apply

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/YMnwSKd1cjO3hfYnPsjX/image.png" alt=""><figcaption></figcaption></figure>

## OUTPUT -

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/3qeoUEvHcYxQxtAiQJtx/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/DQBCgrj108w1MNI4GXcB/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/SuKqas0hT7Dv63qraEfT/image.png" alt=""><figcaption></figcaption></figure>
