Cloud Shell 편집기를 Cloud Run 서비스 및 보안 비밀 제작 환경으로 사용합니다. 편집기에는 Cloud 개발에 필요한 도구가 미리 로드되어 있습니다.
서비스를 만드는 방법은 다음과 같습니다.
Cloud Code 상태 표시줄에서 활성 프로젝트 이름을 클릭합니다.
표시되는 빠른 선택 메뉴에서 새 애플리케이션을 클릭한 다음 Cloud Run 애플리케이션을 클릭합니다.
Cloud Run 샘플 목록에서 Python(Flask): Cloud Run을 선택합니다.
샘플 폴더를 선택한 다음 새 애플리케이션 만들기를 클릭합니다.
Cloud Shell 편집기에서 서비스가 새로운 작업공간에 로드되면 탐색기 뷰에서 파일을 봅니다.
보안 비밀 만들기
Secret Manager를 사용하면 보안 비밀을 바이너리 blob이나 텍스트 문자열로 저장, 관리, 액세스할 수 있습니다.
또한 보안 비밀을 관리하므로 가상 머신이나 운영 서비스를 처리할 필요가 없습니다.
Cloud Code의 Secret Manager 통합을 사용하여 보안 비밀을 만들려면 다음 안내를 따르세요.
Secret Manager를 클릭하고 로드될 때까지 잠깐 기다립니다.
Cloud Shell에서 Google Cloud API를 호출할 수 있도록 승인하라는 메시지가 표시되면 승인을 클릭합니다.
add보안 비밀 만들기를 클릭합니다.
메시지가 표시되면 드롭다운 선택기에서 Google Cloud 프로젝트를 선택합니다.
메시지가 표시되면 Secret Manager API를 사용 설정합니다.
Secret Manager - 보안 비밀 만들기 탭이 표시되면 이름 필드에 다음을 입력합니다.
my-secret
보안 비밀 값 필드에 다음을 입력합니다.
Hello secret!
보안 비밀 만들기를 클릭하면 보안 비밀을 성공적으로 생성했다는 메시지가 표시됩니다.
코드에 보안 비밀 추가
보안 비밀은 애플리케이션 런타임에 필요한 데이터베이스 비밀번호, API 키, TLS 인증서 등의 구성 정보를 저장하는 데 유용합니다.
코드에 보안 비밀을 추가하려면 다음 안내를 따르세요.
Cloud API 뷰를 열고 Secret Manager API를 선택합니다.
그러면 제목이 Secret Manager API인 Google Cloud API 세부정보 탭이 열립니다.
클라이언트 라이브러리 설치 섹션에서 Python 탭을 클릭하고 play_arrow터미널에서 실행을 클릭합니다.
이렇게 하면 google-cloud-secret-manager 클라이언트 라이브러리가 설치됩니다.
requirements.txt를 열고 파일 하단에 다음 행을 추가합니다.
google-cloud-secret-manager==2.1.0
변경사항이 자동으로 저장됩니다.
보안 비밀의 최신 값을 가져오려면 app.py를 열고 hello 함수 뒤에 다음 함수를 복사하여 붙여넣습니다.
defaccess_secret_version(secret_version_id):"""Return the value of a secret's version"""fromgoogle.cloudimportsecretmanager# Create the Secret Manager client.client=secretmanager.SecretManagerServiceClient()# Access the secret version.response=client.access_secret_version(name=secret_version_id)# Return the decoded payload.returnresponse.payload.data.decode('UTF-8')
access_secret_version 함수를 호출하려면 메시지 변수를 다음으로 변경합니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-09-04(UTC)"],[[["\u003cp\u003eCloud Code's Secret Manager integration allows you to create, store, manage, and access secrets securely without needing to manage virtual machines or operating services.\u003c/p\u003e\n"],["\u003cp\u003eYou can create a secret within Cloud Code by using the Secret Manager view, naming the secret, and providing a value for it.\u003c/p\u003e\n"],["\u003cp\u003eSecrets can be added to your application code by installing the \u003ccode\u003egoogle-cloud-secret-manager\u003c/code\u003e client library and using the provided function to access the secret's value by its ID.\u003c/p\u003e\n"],["\u003cp\u003eCloud Code's Secret Manager allows you to create new versions of secrets, which is useful for updating secret values without changing your application code.\u003c/p\u003e\n"],["\u003cp\u003eSecret versions can be disabled, making them inaccessible, or permanently destroyed through the Secret Manager view in Cloud Code.\u003c/p\u003e\n"]]],[],null,["# Create and manage secrets with Cloud Code\n\nLearn how to create and manage secrets using Cloud Code's Secret Manager\nintegration.\n\n*** ** * ** ***\n\nTo follow step-by-step guidance for this task directly in the\nCloud Shell Editor, click **Guide me**:\n\n[Guide me](https://console.cloud.google.com/?walkthrough_id=shell--secret_manager_cloud_code_create_secret)\n\n*** ** * ** ***\n\nBefore you begin\n----------------\n\n1. In the Google Cloud console, go to the project selector page.\n\n [Go to project selector](https://console.cloud.google.com/projectselector2/home/dashboard)\n2. Select or create a Google Cloud project.\n\n | **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.\n3. Install [Git](https://git-scm.com/downloads) so that Cloud Code can perform Git operations, like cloning a sample.\n4. Install the [Cloud Code plugin](/code/docs/vscode/install#installing) if you haven't already.\n\nCreate your Cloud Run service\n-----------------------------\n\nUse Cloud Shell Editor as your environment for creating your Cloud Run service\nand secret. The editor comes preloaded with the tools needed for cloud\ndevelopment.\n\nTo create your service:\n\n1. In the Cloud Code status bar, click the active project name.\n\n2. In the Quick Pick menu that appears, select click **New Application** , and\n then click **Cloud Run Application**.\n\n3. From the list of Cloud Run samples, select **Python (Flask): Cloud Run**.\n\n4. Select a folder for your sample and then click **Create New Application**.\n\nAfter Cloud Shell Editor loads your service in a new workspace, view its files in\nthe explorer view.\n\nCreate a secret\n---------------\n\n[Secret Manager](https://cloud.google.com/secret-manager/) allows you to\nsecurely store, manage, and access secrets as binary blobs or text strings.\nAdditionally, it manages your secrets, meaning you don't have to deal with\nvirtual machines or operating services.\n\nTo create a secret with Cloud Code's Secret Manager integration:\n\n1. Click Secret Manager and give it a moment to load.\n2. If prompted to authorize Cloud Shell to make Google Cloud API calls, click **Authorize**.\n3. Click add Create Secret.\n4. If prompted, select your Google Cloud project from the dropdown selector.\n5. If prompted, enable the Secret Manager API.\n6. In the **Secret Manager - Create Secret** tab that appears, enter the\n following in the **Name** field:\n\n my-secret\n\n7. In the **Secret Value** field, enter:\n\n Hello secret!\n\n8. Click **Create Secret** and a message that your secret was successfully\n created appears.\n\nAdd a secret to your code\n-------------------------\n\nSecrets are great for storing configuration information such as database\npasswords, API keys, or TLS certificates needed by an application at runtime.\n\nTo add a secret to your code:\n\n1. Open the\n Cloud API view and select the\n Secret Manager API.\n\n This opens a Google Cloud API Detail tab with **Secret Manager API** as the\n heading.\n2. In the **Install Client Library** section, click the **Python** tab and\n click play_arrow **Run in terminal** .\n This installs the `google-cloud-secret-manager` client library.\n\n3. Open\n requirements.txt\n and add the following line to the bottom of the file:\n\n google-cloud-secret-manager==\u003cvar translate=\"no\"\u003eVERSION_NUMBER\u003c/var\u003e\n\n You can find the version number in the console after you run the\n installation in the previous step. For example, the console might show:\n `Successfully installed google-cloud-secret-manager-2.23.1`\n\n Your changes are automatically saved.\n4. To get the latest value of your secret, open\n app.py\n and copy and paste the following function:\n after the hello function:\n\n def access_secret_version(secret_version_id):\n \"\"\"Return the value of a secret's version\"\"\"\n from google.cloud import secretmanager\n\n # Create the Secret Manager client.\n client = secretmanager.https://cloud.google.com/python/docs/reference/secretmanager/latest/google.cloud.secretmanager_v1.services.secret_manager_service.SecretManagerServiceClient.html()\n\n # Access the secret version.\n response = client.https://cloud.google.com/python/docs/reference/secretmanager/latest/google.cloud.secretmanager_v1.services.secret_manager_service.SecretManagerServiceClient.html#google_cloud_secretmanager_v1_services_secret_manager_service_SecretManagerServiceClient_access_secret_version(name=secret_version_id)\n\n # Return the decoded payload.\n return response.payload.data.decode('UTF-8')\n\n5. To call the `access_secret_version` function, replace the\n message variable\n with the following:\n\n message = access_secret_version(\"\u003cSECRET_VERSION_ID\u003e\")\n\n6. If you still have the **Secret Manager - Create Secret** tab open,\n file_copy **Copy** the ID.\n\n To get the ID of a secret version at any time, navigate to\n Secret Manager \\\u003e \\[SECRET_NAME\\] \\\u003e Versions, hold the\n pointer over over your version, and click\n\n **Copy resource ID**.\n7. To add the version ID, replace the placeholder\n `\u003cSECRET_VERSION_ID\u003e`\n with your copied version ID.\n\nRun on Cloud Run emulator\n-------------------------\n\nTo test your new secret, run your Cloud Run service locally on the Cloud Run emulator.\n\n1. Launch the Cloud Code menu from the status bar.\n2. To build and deploy your service to the emulator, select **Run\n on Cloud Run Emulator**.\n3. In the Run/Debug on Cloud Run Emulator tab that appears, click **Run**.\n4. When running the configuration for the first time, this process can take up\n to 5 minutes. The\n **Output**\n panel displays the progress as your app is built and deployed.\n\n5. After your app is built, launch your app by clicking the localhost link that\n appears in your\n **Output**\n panel. Your secret's value is displayed under the success graphic.\n\nView and create a new secret version\n------------------------------------\n\nCloud Code's Secret Manager view gives you a quick look at your project's\nsecrets, with actions for managing them.\n\n### Viewing the value of a secret version\n\n1. Click the Secret Manager view.\n2. Expand your secret by clicking on it.\n3. In the **Versions** folder, right-click the numbered version that you want to view the value of and select **Show Version Value**.\n\nNote that you can't edit a secret version. To update a secret's value, you must create a\nnew *version*.\n\n### Create a new secret version\n\nThe value of a secret is stored in a secret version. A secret can have many\nversions. This is helpful in situations where a secret changes. Updating a\nsecret with a new version means you don't have to update your code.\n\n1. Click the Secret Manager view.\n2. Right-click your secret's name and select **Create Secret Version**.\n3. In the **Secret Manager - Create Version** tab that appears, enter a new value and click **Create Version**.\n4. After the **Secret Manager - Create Secret** tab opens, click file_copy **Copy** top copy the ID.\n5. To add the newer version ID, replace the current version that appears the message variable in app.py with the newer version ID that you copied.\n\nIf you always want your code to use the latest version, replace the version number at the end of your version ID with `latest`.\n\nView and manage secrets\n-----------------------\n\n### Disable a secret version\n\nSecret versions are enabled by default after creation, meaning they can be\naccessed. A disabled secret is inaccessible, but you can always restore access\nto it at any time.\n\nTo disable a secret version:\n\n1. Click **Cloud Code** and then expand the **Secret Manager** section.\n2. Expand your secret by clicking on it.\n3. Under the **Versions** folder, right-click the numbered version you want to disable.\n4. Select **Disable Version**.\n\n### Destroy a secret version\n\nWhen you destroy a secret version, it can't be accessed. Destroying a secret\nversion is permanent.\n\n1. Click **Cloud Code** and then expand the **Secret Manager** section.\n2. Expand your secret by clicking on it.\n3. Under the **Versions** folder, right-click the version you want to destroy.\n4. Select **Destroy Version**.\n\nCleaning up\n-----------\n\nTo delete just the cluster you created for this quickstart:\n\n1. Click **Cloud Code** and then expand the **Kubernetes** section.\n2. Hold the pointer over your cluster name and then click open_in_new **Open in Google Cloud console**.\n3. Click **Delete** and then click **Delete**.\n\nTo delete your project (and associated resources, including any clusters):\n\n1. Go to the Projects page in the Google Cloud console:\n\n [Go to the Projects page](https://console.cloud.google.com/project)\n2. Select the project that you created for this quickstart and then click\n **Delete**.\n\n3. Type the project ID to confirm and then click **Shut down**.\n\n This shuts down the project and schedules it for deletion."]]