Provider "registry.terraform.io/-/google" v1.19.1 is not compatible with Terraform 0.12.18. Provider version 2.5.0 is the earliest compatible version. Select it with the following version constraint:
version = "~> 2.5" Terraform checked all of the plugin versions matching the given constraint: ~> 1.19.0 Consult the documentation for this provider for more information on compatibility between provider and Terraform versions. Downloading plugin for provider "random" (hashicorp/random) 2.2.1… Error: incompatible provider version
error 는 backing_file.tf 파일에서 발생하고 있었다. 간단하게 version 차이..
provider.google v2.20.1 로 1.19보다 많이 높은 상태였다. 일단 진행해 보기로 했으니.. backing_file.tf 수정
provider "google" { version = "~> 1.19.0" region = "us-central1" zone = "us-central1-c" }
provider "google" { version = "~> 2.5" region = "us-central1" zone = "us-central1-c" }
수정후에 다시 terraform init 을 실행 하였다.
Initializing provider plugins… Checking for available provider plugins… Downloading plugin for provider "google" (hashicorp/google) 2.20.1… Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.
정상적으로 실행 되는것을 확인하였다.
init 후에 apply 하니 다시 Warning 과 함께 error 가 발생하였다.
Warning: Interpolation-only expressions are deprecated on main.tf line 9, in resource "google_compute_instance" "instance": 9: image = "${data.google_compute_image.debian_image.self_link}"Error: Error loading zone 'us-central1-a': googleapi: Error 403: Access Not Configured. Compute Engine API has not been used in project 45002 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/compute.googleapis.com/overview?project=304102002 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry., accessNotConfigured on main.tf line 1, in resource "google_compute_instance" "instance": 1: resource "google_compute_instance" "instance" {
이건 이전에도 겪은 케이스 같다...functions api 셋팅할때 발생한 거였는데...googleapi 관련 에러다. cloudshell 에서 컴퓨팅쪽의 api를 사용할 수 없어서 발생하는 에러로 로그에 보이는 페이지로 이동해서 그냥 허용해 준다.
Enter a value: yes google_compute_instance.instance: Creating… google_compute_instance.instance: Still creating… [10s elapsed] google_compute_instance.instance: Creation complete after 10s [id=vm-instance-optimum-badger] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
허용후에 다시 terraform apply 를 치면 정상적으로 실행이 된다. 그럼 확인해 보자.
인스턴스가 생성이 됬다. 그렇다면 이젠 간단히 생성하는것 까지 완료했으므로. 이젠 기본 템플릿을 이용한 구성을 만들것이다. 생성한 테라폼은 terraform destroy 명령어로 삭제 했다.
google_compute_instance.instance: Destroying… [id=vm-instance-optimum-badger] google_compute_instance.instance: Still destroying… [id=vm-instance-optimum-badger, 10s elapsed] google_compute_instance.instance: Still destroying… [id=vm-instance-optimum-badger, 20s elapsed] google_compute_instance.instance: Still destroying… [id=vm-instance-optimum-badger, 30s elapsed] google_compute_instance.instance: Destruction complete after 38s random_pet.suffix: Destroying… [id=optimum-badger] random_pet.suffix: Destruction complete after 0s
정상적으로 삭제되는것을 확인할수 있었다.
gcp cloud shell 에서 terraform 을 사용하는 방법을 테스트해 보았다. 다음엔 VPC 구성과 인스턴스 그룹 생성 로드벨런서 구성까지 한방에 진행할것이다.
여담으로 cloud shell은 진짜 강력한 도구이다.
스크린샷과 같이 shell을 지원하면서 동시에 에디터도 지원한다.
vi 에 익숙한 나같은경우에는 그냥 vi 로 작업하지만 익숙하지 않은 사용자의 경우에는 우와할정도다..
hoceneco@cloudshell:~ (sage-facet-22972)$ sudo gcloud components update Your current Cloud SDK version is: 274.0.0 You will be upgraded to version: 274.0.1 ┌──────────────────────────────────────────────────┐ │ These components will be updated. │ ├──────────────────────────┬────────────┬──────────┤ │ Name │ Version │ Size │ ├──────────────────────────┼────────────┼──────────┤ │ Cloud SDK Core Libraries │ 2019.12.27 │ 12.7 MiB │ └──────────────────────────┴────────────┴──────────┘
gcloud 구성요소를 업데이트한다 - 꼭할필요는 없다.
오늘 미션에서 필요한 준비물은 Cloud Functions, Google Cloud Vision API, ImageMagick, Cloud Storage 이렇게 다.
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git 명령어를 이용하여 test code를 다운받고 다운받은 디렉토리로 이동한다. 일단 먼저 테스트하는것은 아래 Docs 를 참고하여 진행하였다.
이미지 업로드가 끝나면 functionc로그를 확인하면 정상작동됬는지 확인할수 있다. 첫번째 업로드에선 api 에러가 발생했는데. 이때 콘솔에서 api 를 사용할수 있도록 설정해줬다.
E blurOffensiveImages 909862418886020 2019-12-28 03:42:15.967 Error: 7 PERMISSION_DENIED: Cloud Vision API has not been used in project 300346160521 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/vision.googleapis.com/overview?project= then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry. at Object.callErrorFromStatus (/srv/node_modules/@grpc/grpc-js/build/src/call.js:30:26) at Http2CallStream.call.on (/srv/node_modules/@grpc/grpc-js/build/src/client.js:96:33) at emitOne (events.js:121:20) at Http2CallStream.emit (events.js:211:7) at process.nextTick (/srv/node_modules/@grpc/grpc-js/build/src/call-stream.js:97:22) at _combinedTickCallback (internal/process/next_tick.js:132:7) at process._tickDomainCallback (internal/process/next_tick.js:219:9) D blurOffensiveImages 909862418886020 2019-12-28 03:42:16.027 Function execution took 570 ms, finished with status: 'error'
D blurOffensiveImages 909862418886020 2019-12-28 03:42:16.027 Function execution took 570 ms, finished with status: 'error' D blurOffensiveImages 909865384788642 2019-12-28 03:47:22.883 Function execution started I blurOffensiveImages 909865384788642 2019-12-28 03:47:28.294 Analyzing zombie-949916_1280.jpg. I blurOffensiveImages 909865384788642 2019-12-28 03:47:29.696 Detected zombie-949916_1280.jpg as inappropriate. I blurOffensiveImages 909865384788642 2019-12-28 03:47:30.683 Downloaded zombie-949916_1280.jpg to /tmp/zombie-949916_1280.jpg. I blurOffensiveImages 909865384788642 2019-12-28 03:47:40.091 Blurred image: zombie-949916_1280.jpg I blurOffensiveImages 909865384788642 2019-12-28 03:47:40.414 Uploaded blurred image to: gs://gs://linuxer-convert/zombie-949916_1280.jpg D blurOffensiveImages 909865384788642 2019-12-28 03:47:40.419 Function execution took 17551 ms, finished with status: 'ok' D blurOffensiveImages 909860416701916 2019-12-28 03:51:04.290 Function execution started I blurOffensiveImages 909860416701916 2019-12-28 03:51:04.295 Analyzing zombie-949916_1280.jpg. I blurOffensiveImages 909860416701916 2019-12-28 03:51:05.663 Detected zombie-949916_1280.jpg as inappropriate. I blurOffensiveImages 909860416701916 2019-12-28 03:51:06.283 Downloaded zombie-949916_1280.jpg to /tmp/zombie-949916_1280.jpg.
정상적으로 컨버팅이 된거다.
원본
블러처리된것.
자동으로 블러 처리가 완료된것을 확인할수 있다.
오늘의 미션은 resize이므로 resize를 하기위해선 index.js를 수정해야한다. 오늘의 실습에선 GraphicsMagick for node.js 을 이용해서 테스트를 진행했으므로 아주 수월했다. 원래사용한 blur 부분만 수정하면 될거 같았다.
blur Accepts a radius and optional sigma (standard deviation). gm("img.png").blur(radius [, sigma])
옵션은 위와 같고
resize Resize the image. options %, @, !, < or > see the GraphicsMagick docs for details gm("img.png").resize(width [, height [, options]]) To resize an image to a width of 40px while maintaining aspect ratio: gm("img.png").resize(40) To resize an image to a height of 50px while maintaining aspect ratio: gm("img.png").resize(null, 50) To resize an image to a fit a 40x50 rectangle while maintaining aspect ratio: gm("img.png").resize(40, 50) To override the image's proportions and force a resize to 40x50: gm("img.png").resize(40, 50, "!")
그러니까
이부분을 .blur(0, 16) 부분을 resize 옵션으로 변경만 하면되는것이다.
.resize(200, 200) 으로 수정을 하였다. 그리고 functions deploy 하고
gcloud functions deploy blurOffensiveImages --runtime nodejs8 --trigger-bucket=gs://linuxer-upload --set-env-vars BLURRED_BUCKET_NAME=gs://linuxer-convert Deploying function (may take a while - up to 2 minutes)…done. availableMemoryMb: 256 entryPoint: blurOffensiveImages environmentVariables: BLURRED_BUCKET_NAME: gs://linuxer-convert eventTrigger: eventType: google.storage.object.finalize failurePolicy: {} resource: projects/_/buckets/linuxer-upload service: storage.googleapis.com labels: deployment-tool: cli-gcloud name: projects/sage-facet-22972/locations/us-central1/functions/blurOffensiveImages runtime: nodejs8 serviceAccountEmail: sage-facet-22972@appspot.gserviceaccount.com sourceUploadUrl: https://storage.googleapis.com/gcf-upload-us-central1-ede08b3c-b370-408b-ba59-95f296f2e3e/4a80effd-0371-4317-8651-0416cffc0563.zip?GoogleAccessId=service-300346160521@gcf-admin-robot.iam.gserviceaccount.com&Expires=1577519172&Signature=CrNrgjDfo8gsr%2FoeGkcEiRvnMskkK5J4JHRoDMyh9DpXnXmp4ivWOlRsQ136GL9iK4FBvsxmAtIby4WgTECry4dYU%2FN6UkfjZSBLVtzJnJxR%2F5h7ZLY9PMd%2BDYcV1AAVbw9i1paFgBNjAq1WhNiMmmXonFBpyRHBlqMLn4CKuW7QAmA7NXOugTpQY3b%2BQ9E1ia9uIZtNwqcKfv1C1GM8e2%2FdKhMwwlUPU2EYy9gb4nirHvsdrYbzdewabmPwlRtgq1b2wTjWiuMM53vO9fDy6skNaB58tqumSfUeHM%2FTrjQrlqGejjon2cx9IlH9xF5kGKfLGzBesXEHj%2B6K6ZqilA%3D%3D status: ACTIVE timeout: 60s updateTime: '2019-12-28T07:17:00Z' versionId: '5'
정상적으로 생성이 되면 업로드를 진행하였다.
D blurOffensiveImages 909966712855810 2019-12-28 07:17:16.904 Function execution started I blurOffensiveImages 909966712855810 2019-12-28 07:17:17.003 Analyzing zombie-949916_1280.jpg.
functions 이 정상적으로 실행이 완료되고
이미지가 리사이즈 되는것을 확인할수 있었다. 355.3KB -> 12.69KB로 작아졌다. 물론이미지 사이즈도..