반응형
Cloud function을 모듈로 만들어 관리하는 방법에 대해 기술한다.
1.cloud_functions.tf
cloud function 소스 코드 저장하는 버킷을 생성하고 cloud function 모듈을 만들어 추가해준다.
참고할 점은 functions_version이라는 변수를 주어 1st, 2nd 버전을 구분하도록 설정하였다.
#clouid function 소스 코드 저장 버킷
resource "google_storage_bucket" "function_bucket" {
name = "function-bucket-hr"
location = "ASIA"
project = "pjt-an3-dev-vm-2"
force_destroy = false
}
# function_version으로 버전 구분 (1st , 2nd)
module "my_cloud_function" {
source = "./modules/cloud_function"
# 소스 코드 저장 zip 파일로 저장
object_name = "test/function.zip"
bucket_name = google_storage_bucket.function_bucket.name
gcs_source ="./hello_http/hello_http.zip"
function_version = 1
function_name = "my-function2"
description = "My Cloud Function"
runtime = "python39"
entry_point = "hello_http"
timeout = 60
region = "asia-northeast3"
project = "pjt-an3-dev-vm-2"
trigger_http = true
available_memory_mb = 256
environment_variables = {
EXAMPLE_KEY = "example-value"
}
service_account_email = module.svcacc_test_account_2.service_account_email
}
cloud_function 모듈 정보
1) main.tf
google_storage_bucket_object 리소스를 통해 버킷에 로컬에 있는 소스 파일을 업로드한다.
count를 통해 functions_version이 1 혹은 2일 경우 해당 리소스를 생성하도록 설정한다.
resource "google_storage_bucket_object" "archive" {
name = var.object_name
bucket = var.bucket_name
source = var.gcs_source
}
resource "google_cloudfunctions_function" "function_1st_gen" {
count = var.function_version == 1 ? 1 : 0
name = var.function_name
description = var.description
runtime = var.runtime
entry_point = var.entry_point
timeout = var.timeout
region = var.region
project = var.project
source_archive_bucket = var.bucket_name
source_archive_object = var.object_name
trigger_http = var.trigger_http
available_memory_mb = var.available_memory_mb
environment_variables = var.environment_variables
service_account_email = var.service_account_email
}
resource "google_cloudfunctions2_function" "function_2nd_gen" {
count = var.function_version == 2 ? 1 : 0
name = var.function_name
location = var.region
description = var.description
project = var.project
build_config {
runtime = var.runtime
entry_point = var.entry_point
source {
storage_source {
bucket = var.bucket_name
object = var.object_name
}
}
}
service_config {
max_instance_count = 1
available_memory = var.available_memory_mb
timeout_seconds = var.timeout
environment_variables = var.environment_variables
service_account_email = var.service_account_email
}
}
2) output
3) variables.tf
function_version 변수에 validation 설정을 통해 1,2 값만 전달되도록 설정했다.
variable "function_version" {
description = "Cloud Function version (1 or 2)"
default = 1
validation {
condition = var.function_version == 1 || var.function_version == 2
error_message = "Version must be either 1 or 2"
}
}
variable "object_name" {
description = "Name of the GCS object_name"
}
variable "gcs_source" {
description = "Name of the GCS source"
}
variable "function_name" {
description = "Name of the Cloud Function"
}
variable "description" {
description = "Description of the Cloud Function"
}
variable "runtime" {
description = "Runtime of the Cloud Function"
}
variable "entry_point" {
description = "Entry point of the Cloud Function"
}
variable "timeout" {
description = "Timeout for the Cloud Function execution"
}
variable "region" {
description = "Region for the Cloud Function deployment"
}
variable "project" {
description = "Project ID where the Cloud Function will be deployed"
}
variable "bucket_name" {
description = "Name of the Cloud Storage bucket containing the Cloud Function code"
}
variable "trigger_http" {
description = "Whether the Cloud Function should be triggered by HTTP requests"
}
variable "available_memory_mb" {
description = "Available memory for the Cloud Function execution"
}
variable "environment_variables" {
description = "Environment variables for the Cloud Function"
type = map(string)
}
variable "service_account_email" {
description = "Email address of the service account associated with the Cloud Function"
}
2. GCP 콘솔에서 생성 확인
GCS
Cloud function
반응형
'클라우드 > GCP 자원 테라폼으로 관리해보기' 카테고리의 다른 글
(6) 공유 VPC 서비스 프로젝트에 서브넷 생성 및 연결하기 (0) | 2024.06.09 |
---|---|
(5) GCP 공유 VPC 설정 모듈화 (0) | 2024.06.09 |
(3-2) GCP IAM Policy 관리 - serviceAccount, group, user를 role에 연결해주는 모듈 (0) | 2024.06.09 |
(3-1) GCP IAM Policy 관리 - custom role을 구성하는 모듈 (0) | 2024.06.09 |
(2) GCP 방화벽 룰 생성 (0) | 2024.06.09 |