Create a Flask application on Compute Engine
Stay organized with collections
Save and categorize content based on your preferences.
Use Compute Engine, Networking, Storage, and KMS to create a Flask application.
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["# Create a Flask application on Compute Engine\n\nUse Compute Engine, Networking, Storage, and KMS to create a Flask application.\n\nCode sample\n-----------\n\n### Terraform\n\n\nTo learn how to apply or remove a Terraform configuration, see\n[Basic Terraform commands](/docs/terraform/basic-commands).\n\n\nFor more information, see the\n[Terraform provider reference documentation](https://registry.terraform.io/providers/hashicorp/google/latest/docs).\n\n resource \"google_compute_network\" \"vpc_network\" {\n name = \"my-custom-mode-network\"\n auto_create_subnetworks = false\n mtu = 1460\n }\n\n resource \"google_compute_subnetwork\" \"default\" {\n name = \"my-custom-subnet\"\n ip_cidr_range = \"10.0.1.0/24\"\n region = \"us-west1\"\n network = google_compute_network.vpc_network.id\n }\n\n # Create a single Compute Engine instance\n resource \"google_compute_instance\" \"default\" {\n name = \"flask-vm\"\n machine_type = \"f1-micro\"\n zone = \"us-west1-a\"\n tags = [\"ssh\"]\n\n boot_disk {\n initialize_params {\n image = \"debian-cloud/debian-11\"\n }\n }\n\n # Install Flask\n metadata_startup_script = \"sudo apt-get update; sudo apt-get install -yq build-essential python3-pip rsync; pip install flask\"\n\n network_interface {\n subnetwork = google_compute_subnetwork.default.id\n\n access_config {\n # Include this section to give the VM an external IP address\n }\n }\n }\n\n resource \"google_compute_firewall\" \"ssh\" {\n name = \"allow-ssh\"\n allow {\n ports = [\"22\"]\n protocol = \"tcp\"\n }\n direction = \"INGRESS\"\n network = google_compute_network.vpc_network.id\n priority = 1000\n source_ranges = [\"0.0.0.0/0\"]\n target_tags = [\"ssh\"]\n }\n\n\n resource \"google_compute_firewall\" \"flask\" {\n name = \"flask-app-firewall\"\n network = google_compute_network.vpc_network.id\n\n allow {\n protocol = \"tcp\"\n ports = [\"5000\"]\n }\n source_ranges = [\"0.0.0.0/0\"]\n }\n\n # Create new multi-region storage bucket in the US\n # with versioning enabled\n\n resource \"google_kms_key_ring\" \"terraform_state\" {\n name = \"${random_id.bucket_prefix.hex}-bucket-tfstate\"\n location = \"us\"\n }\n\n resource \"google_kms_crypto_key\" \"terraform_state_bucket\" {\n name = \"test-terraform-state-bucket\"\n key_ring = google_kms_key_ring.terraform_state.id\n rotation_period = \"86400s\"\n\n lifecycle {\n prevent_destroy = false\n }\n }\n\n # Enable the Cloud Storage service account to encrypt/decrypt Cloud KMS keys\n data \"google_project\" \"project\" {\n }\n\n resource \"google_project_iam_member\" \"default\" {\n project = data.google_project.project.project_id\n role = \"roles/cloudkms.cryptoKeyEncrypterDecrypter\"\n member = \"serviceAccount:service-${data.google_project.project.number}@gs-project-accounts.iam.gserviceaccount.com\"\n }\n\n resource \"random_id\" \"bucket_prefix\" {\n byte_length = 8\n }\n\n resource \"google_storage_bucket\" \"default\" {\n name = \"${random_id.bucket_prefix.hex}-bucket-tfstate\"\n force_destroy = false\n location = \"US\"\n storage_class = \"STANDARD\"\n versioning {\n enabled = true\n }\n encryption {\n default_kms_key_name = google_kms_crypto_key.terraform_state_bucket.id\n }\n depends_on = [\n google_project_iam_member.default\n ]\n }\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=storage)."]]