This project demonstrates how to create a Cloud Run service on Google Cloud Platform (GCP) using Terraform.
- Install Terraform
- Install Google Cloud SDK
- Set up a GCP account and create a billing account
- Google Cloud Platform Account: You need a GCP account with appropriate permissions to create and manage resources. Also, ensure that you have a JSON service account key file (
terraform-gcp-provider.json
) for authentication. - Clone this repository:
https://github.com/singhragvendra503/DevOps-Candidates-task.git
cd DevOps-Candidates-task
.
├── README.md
├── task-1
│ ├── Dockerfile
│ ├── terraform-gcp-provider.json
│ ├── index.html
│ ├── main.tf
│ ├── output.tf
│ ├── terraform.tfstate
│ ├── terraform.tfstate.backup
│ ├── terraform.tfvars
│ └── variable.tf
└── task-2
├── dev_env
│ ├── terraform-gcp-provider.json
│ ├── main.tf
│ ├── output.tf
│ ├── terraform.tfvars
│ └── variable.tf
├── modules
│ ├── cloud-run
│ │ ├── 1-main.tf
│ │ ├── 2-variable.tf
│ │ └── 3-output.tf
│ └── vpc
│ ├── 1-vpc.tf
│ ├── 2-subnets.tf
│ ├── 3-router.tf
│ ├── 4-nat.tf
│ ├── 5-internet-gateway.tf
│ ├── 6-firewalls.tf
│ ├── 7-variable.tf
│ └── 8-output.tf
├── prod_env
│ ├── terraform-gcp-provider.json
│ ├── main.tf
│ ├── output.tf
│ ├── terraform.tfvars
│ └── variable.tf
└── stg_env
├── terraform-gcp-provider.json
├── main.tf
├── output.tf
├── terraform.tfvars
└── variable.tf
- Create
terraform.tfvars
as you see in above file tree structure
###############################################################################################################
# VPC_network varibales #
###############################################################################################################
region = "us-east4"
project_id = "<project_name>"
ports = [ "22", "80", "443" ]
public_subnets_cidr_range = [
"10.0.1.0/24",
"10.0.2.0/24",
"10.0.3.0/24",
]
private_subnets_cidr_range = [
"10.0.4.0/24",
"10.0.5.0/24",
"10.0.6.0/24",
]
appname = "hello-world"
environment = "demo"
###############################################################################################################
# Cloud Run varibales #
###############################################################################################################
service_name = "hello-world"
container_image = "${REGION}-docker.pkg.dev/${PROJECT_ID}/aws-move/hello-world:latest"
container_port = 80
memory = "500Mi"
cpu = "2"
max_instances = 2
vpc_connector_name = "hello-connector"
vpc_connector_cidr_range = "10.8.0.0/28"
- Create a simple
"Hello World"
Docker image:
docker build -f Dockerfile -t ${REGION}-docker.pkg.dev/${PROJECT_ID}/aws-move/hello-world:latest .
- Authenticate GCR registry
cat terraform-gcp-provider.json | docker login -u _json_key --password-stdin ${REGION}-docker.pkg.dev/${PROJECT_ID}/aws-move
- Build and push the Docker image to Artifact Registry:
docker push ${REGION}-docker.pkg.dev/${PROJECT_ID}/aws-move/hello-world:latest
Replace ${REGION}
and ${PROJECT_ID}
with your chosen region and project ID.
- Initialize Terraform:
terraform init
- Validate terraform code syntex
terraform validate
- Preview the changes:
terraform plan
- Apply the changes:
terraform apply
- After the deployment is complete, Terraform will output the URL of your Cloud Run service. You can access your "Hello World" application at this URL.
To remove all resources created by Terraform:
terraform destroy
Scan Policy Validation Using terrascan
Mothly Cost Calculated Using infracost
Note: This will delete the GCP project and all associated resources.