-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
anupam
committed
Feb 14, 2023
1 parent
8da05ee
commit 996c7c7
Showing
1 changed file
with
14 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,48 @@ | ||
# Terraform Code Sets | ||
This repository contains Terraform code for Individual Resource deployment on Google Public Cloud. | ||
# Terraform GCP Lab | ||
This repository contains Terraform code for infrastructure resource deployment on Google Public Cloud. | ||
|
||
## Prerequisites | ||
Below prerequisites must be fulfilled for successfull execution of code. | ||
|
||
### Software Requirement: | ||
Resources in this repository are meant for use with Terraform 1.3.6 (Check the version using `terraform --version`). If you don't have the compatible version, download it from official Terraform repository. | ||
### Software Requirement | ||
Resources in this repository are meant for use with Terraform 1.3.6 (Check the version using `terraform version`). If you don't have the compatible version, download it from official Terraform repository. See [Installation-Guide](./docs/install.md) on how to install Terraform. | ||
|
||
- [gcloud sdk](https://cloud.google.com/sdk/install) >= 332.0.0 | ||
- [gcloud sdk](https://cloud.google.com/sdk/install) >= 414.0.0 | ||
- [Terraform](https://www.terraform.io/downloads.html) >= 1.3.6 | ||
- [terraform-provider-google] plugin = 4.52.0 | ||
- [terraform-provider-google-beta] plugin = 4.52.0 | ||
- [terraform-provider-random] plugin = 3.4.0 | ||
|
||
### Permissions Requirement: | ||
### Permissions Requirement | ||
In order to execute these templates you must have a Service Account with the following roles. Access can be more fine-grained to follow Principle of least privilege (PoLP). | ||
|
||
- `roles/resourcemanager.projectOwner` on all the projects where you want to house your resources using service account's email. | ||
- `roles/storage.admin` on the project housing terraform state files. | ||
|
||
### Project API Requirement: | ||
### Project API Requirement | ||
In order to use the services, required APIs must be enabled before resource deployment. Enable the APIs using below mentioned gcloud command. | ||
|
||
gcloud services enable servicenetworking.googleapis.com \ | ||
cloudresourcemanager.googleapis.com \ | ||
compute.googleapis.com \ | ||
iam.googleapis.com | ||
|
||
### Remote Backend Setup: | ||
### Remote Backend Setup | ||
To use a remote backend, create a GCS Bucket and set the versioning. Use below gcloud commands. | ||
|
||
gcloud config set project PROJECT_ID | ||
gsutil mb -c standard -l eu gs://bucket-name | ||
gsutil versioning set on gs://bucket-name | ||
|
||
## Create cloud build trigger | ||
If you are using the user-specified service account to create build trigger, You must create or start builds in the command line using 'gcloud builds submit' or 'gcloud beta builds triggers create'. | ||
|
||
``` | ||
gcloud builds triggers create github --name="terraform-gcp-lab-trigger" --service-account="projects/clear-tape-374908/serviceAccounts/[email protected]" --repo-owner="anupam-sy" --repo-name="terraform-gcp-lab" --description="terraform-gcp-lab" --branch-pattern="^master$" --build-config=".cloudbuild/cloudbuild.yaml" | ||
``` | ||
|
||
Note: To use the Triggers page in the Google Cloud console, the user-specified service account and the build trigger must be in the same project. | ||
|
||
## Execution: | ||
## Execution | ||
For multiple environment provisioning, use different tfstate files in backend. To execute the Terraform code, go to command prompt and then run the following commands: | ||
|
||
- [Required] `terraform init` | ||
- To initialize the terraform with remote backend, use "-backend-config=PATH" flag partial backend configuration. To specify a single key/value pair, use the -backend-config="KEY=VALUE" option when running terraform init. | ||
|
||
- [Optional] `terraform validate` | ||
- To check whether a configuration is syntactically valid and internally consistent, regardless of any provided variables or existing state. | ||
|
||
- [Optional] `terraform fmt` | ||
- The terraform fmt command is used to rewrite Terraform configuration files to a canonical format and style. use "-recursive" flag to format the code inside all folders. | ||
|
||
- [Optional] `terraform plan -var-file="resource.tfvars" -out=tfplan` | ||
- It creates an execution plan. You can use the optional -out=FILE option to save the generated plan to a file on disk, which you can later execute by passing the file to terraform apply as an extra argument. | ||
|
||
- [Required] `terraform apply -var-file="resource.tfvars" -auto-approve` | ||
- It executes the actions proposed in a Terraform plan. In the default case, with no saved plan file, Terraform will prompt you to approve the plan before taking the described actions, unless you override that prompt using the -auto-approve option. Terraform ignores the -auto-approve flag when you pass a previously-saved plan file, because Terraform considers you passing the plan file as the approval. | ||
- [Optional] `terraform plan` | ||
- [Required] `terraform apply -auto-approve` | ||
|
||
- [Optional] `terraform destroy -target="resource_type.resource_name"` | ||
- The terraform destroy command is used to destory the resources defined in your Terraform configuration. To delete the specific resource, use the "target" option with destroy command. | ||
**Note:** See [Terraform-Guide](./docs/info.md) to get real-quick overview of Terraform. | ||
|
||
## Reference: | ||
- https://github.com/GoogleCloudPlatform/solutions-terraform-cloudbuild-gitops | ||
- https://cloud.google.com/build/docs/securing-builds/configure-user-specified-service-accounts | ||
## Reference | ||
- https://github.com/GoogleCloudPlatform/solutions-terraform-cloudbuild-gitops |