Deploying serverless applications with infrastructure-as-code
This project demonstrates how to deploy a simple "Hello World" application using Google Cloud Platform (GCP). The application is built using a Cloud Function, exposed through a Load Balancer, and secured with appropriate configurations. The entire infrastructure is defined using Terraform, an infrastructure-as-code tool.
A serverless function that returns "Hello World!" when accessed.
Distributes incoming traffic to the Cloud Function.
Implements secure configurations for the Cloud Function and Load Balancer.
Uses Terratest to validate the infrastructure.
This project requires following prerquisites:
Clone this repository to your local machine:
git clone https://github.com/your-repo/terraform-gcf.git
cd terraform-gcf
Authenticate with GCP:
gcloud auth application-default login
Set Your GCP Project:
gcloud config set project YOUR_PROJECT_ID
terraform init
terraform apply
Terraform will show you a plan of the resources it will create. Type yes to confirm and proceed.
Once the deployment is complete, Terraform will output the following:
Open the Cloud Function console and test the connection:
curl -m 70 -X POST https://us-central1-hello-world-454204.cloudfunctions.net/hello-world-function \
-H "Authorization: bearer $(gcloud auth print-identity-token)" \
-H "Content-Type: application/json" \
-d '{
"message": "Hello World"
}'
Response: "Hi There, Its a deployment on Cloudfunction_1stgen"
The same test goes for the loadbalancer also. If we need the function to hit only by lb ip then we need to change the permission to lb only.
This project includes automated tests using Terratest. To run the tests:
Navigate to the tests directory:
cd tests
go test -v
The tests will:
Destroy the infrastructure to avoid unnecessary charges, after completing the project:
terraform destroy
Type yes to confirm and proceed.
Here's an overview of the files and directories in this project:
By default, Cloud Functions are configured to allow only internal traffic or traffic from the Load Balancer. To make the URL public, set the ingress_settings to ALLOW_ALL in the Cloud Function configuration.