<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ethan Han on Medium]]></title>
        <description><![CDATA[Stories by Ethan Han on Medium]]></description>
        <link>https://medium.com/@ethanhan_73964?source=rss-4240d250293a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*nut-vgGqHb-nn8E_30PGug.png</url>
            <title>Stories by Ethan Han on Medium</title>
            <link>https://medium.com/@ethanhan_73964?source=rss-4240d250293a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 06:16:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ethanhan_73964/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Configure CAI Feed for Real Time Notifications about GCP Resource Changes]]></title>
            <link>https://medium.com/google-cloud/configure-cai-feed-for-real-time-notifications-about-gcp-resource-changes-a8551a9a1192?source=rss-4240d250293a------2</link>
            <guid isPermaLink="false">https://medium.com/p/a8551a9a1192</guid>
            <category><![CDATA[gcp-app-dev]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[cloud-resource]]></category>
            <category><![CDATA[infrastructure]]></category>
            <category><![CDATA[cloud-services]]></category>
            <dc:creator><![CDATA[Ethan Han]]></dc:creator>
            <pubDate>Fri, 08 Sep 2023 10:27:15 GMT</pubDate>
            <atom:updated>2023-09-08T10:27:15.067Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/proxy/0*5KzqtQdPIObD57Ri.png" /></figure><p>GCP CAI Feed is a feature of <a href="https://cloud.google.com/asset-inventory/docs/overview">Cloud Asset Inventory</a> that allows you to receive real-time notifications about changes to your Google Cloud resources and policies. The following steps shows how we can configure it in GCP.</p><h3>Create the Pub/Sub topic</h3><p>Create a Pub/Sub topic where a message is sent when an asset is changed.</p><pre>$ PROJECT_ID=my-project-id<br>$ CAI_FEED_TOPIC_ID=cai_feed<br>$ gcloud pubsub topics create $CAI_FEED_TOPIC_ID --project $PROJECT_ID</pre><h3>Add permission for CAI agent SA to the pub/sub topic</h3><p>Assign permission to Cloud Asset Inventory agent to publish message to the topic created above.</p><pre>$ gcloud pubsub topics add-iam-policy-binding \<br>projects/$PROJECT_ID/topics/$CAI_FEED_TOPIC_ID \<br>--member=serviceAccount:service-$PROJECT_ID@gcp-sa-cloudasset.iam.gserviceaccount.com \<br>--role=roles/pubsub.publisher</pre><h3>[Optional] Create the subscription to the Cloud Run API endpoint</h3><p>Create a Pub/Sub subscription of type Push that is used to receive any message whenever an asset is changed. Here, Cloud Run will be used as an example, you can define your own subscription as well.</p><pre>$ CLOUDRUN_API_URL=&quot;https://my-cloud-run-url/path&quot;<br><br># SERVICE_ACCOUNT_EMAIL is the email address of the service account granted cloud run invoker role<br># Ref: https://cloud.google.com/run/docs/authenticating/service-to-service#set-up-sa<br>$ SERVICE_ACCOUNT_EMAIL=&quot;SERVICE_ACCOUNT_EMAIL@myorg.com&quot;<br>$ gcloud pubsub subscriptions create cai_feed_subscription \<br>--topic=projects/$PROJECT_ID/topics/$CAI_FEED_TOPIC_ID \<br>--push-endpoint=&quot;$CLOUDRUN_API_URL&quot; \<br>--push-auth-service-account=${SERVICE_ACCOUNT_EMAIL} \<br>--ack-deadline=600 \<br>--min-retry-delay=30s \<br>--max-retry-delay=600s</pre><h3>Create the feed</h3><p>A CAI feed has to be created to ensure Pub/Sub messages are sent to the topic created above. Depending on the requirements, the CAI feed have to be created:</p><ul><li>At the <strong>organization level,</strong> if the customer would like to receive any asset changes for a full organization</li><li>At the <strong>folder level,</strong> if the customer would like to receive any asset changes for a specific folder</li><li>At the <strong>project level</strong> if the customer would like to receive any asset changes for a specific project</li></ul><p><em>“</em><a href="https://cloud.google.com/sdk/gcloud/reference/asset/feeds/create"><em>gcloud asset feeds create</em></a><em>”</em> is used to create the different feeds. For some parameter explanation:</p><ul><li><em>-asset-types</em> is a regexp representing the asset types that are part of the CAI feed. Depending of the different need, this parameter can be restricted or extended to ensure the CAI feed is only working for the intended asset type, for full list of supported assets can be referred <a href="https://cloud.google.com/asset-inventory/docs/overview#assets">here</a>.</li><li><em>–content-type</em> represents the asset content type part of the feed. It can be one of those values: resource, iam-policy and org-policy</li></ul><p><em>Organization Feed</em></p><pre>$ ORGANIZATION_ID=&lt;YOUR_ORG_ID&gt;<br>$ CAI_FEED=cai_feed<br><br>$ gcloud asset feeds create ${CAI_FEED}_resource \<br>--organization=$ORGANIZATION_ID \<br>--pubsub-topic=projects/$PROJECT_ID/topics/$CAI_FEED_TOPIC_ID \<br>--asset-types=&quot;^.*.googleapis.com/.*$&quot; \<br>--content-type=resource<br><br>$ gcloud asset feeds create ${CAI_FEED}_iam-policy \<br>--organization=$ORGANIZATION_ID \<br>--pubsub-topic=projects/$PROJECT_ID/topics/$CAI_FEED_TOPIC_ID \<br>--asset-types=&quot;^.*.googleapis.com/.*$&quot; \<br>--content-type=iam-policy<br><br>$ gcloud asset feeds create ${CAI_FEED}_org-policy \<br>--organization=$ORGANIZATION_ID \<br>--pubsub-topic=projects/$PROJECT_ID/topics/$CAI_FEED_TOPIC_ID \<br>--asset-types=&quot;^.*.googleapis.com/.*$&quot; \<br>--content-type=org-policy</pre><p><em>Folder Feed</em></p><pre>$ REALTIME_FOLDER_ID=&lt;YOUR_REALTIME_FOLDER_ID&gt;<br>$ CAI_FEED=cai_feed<br><br>$ gcloud asset feeds create ${CAI_FEED}_resource \<br>--folder=$REALTIME_FOLDER_ID \<br>--pubsub-topic=projects/$PROJECT_ID/topics/$CAI_FEED_TOPIC_ID \<br>--asset-types=&quot;^.*.googleapis.com/.*$&quot; \<br>--content-type=resource<br><br>$ gcloud asset feeds create ${CAI_FEED}_iam-policy \<br>--folder=$REALTIME_FOLDER_ID \<br>--pubsub-topic=projects/$PROJECT_ID/topics/$CAI_FEED_TOPIC_ID \<br>--asset-types=&quot;^.*.googleapis.com/.*$&quot; \<br>--content-type=iam-policy<br><br>$ gcloud asset feeds create ${CAI_FEED}_org-policy \<br>--folder=$REALTIME_FOLDER_ID \<br>--pubsub-topic=projects/$PROJECT_ID/topics/$CAI_FEED_TOPIC_ID \<br>--asset-types=&quot;^.*.googleapis.com/.*$&quot; \<br>--content-type=org-policy</pre><p><em>Project Feed</em></p><pre>$ REALTIME_PROJECT_ID=&lt;YOUR_REALTIME_PROJECT_ID&gt;<br>$ CAI_FEED=cai_feed<br><br>$ gcloud asset feeds create ${CAI_FEED}_resource \<br>--project=$REALTIME_PROJECT_ID \<br>--pubsub-topic=projects/$PROJECT_ID/topics/$CAI_FEED_TOPIC_ID \<br>--asset-types=&quot;^.*.googleapis.com/.*$&quot; \<br>--content-type=resource<br><br>$ gcloud asset feeds create ${CAI_FEED}_iam-policy \<br>--project=$REALTIME_PROJECT_ID \<br>--pubsub-topic=projects/$PROJECT_ID/topics/$CAI_FEED_TOPIC_ID \<br>--asset-types=&quot;^.*.googleapis.com/.*$&quot; \<br>--content-type=iam-policy<br><br>$ gcloud asset feeds create ${CAI_FEED}_org-policy \<br>--project=$REALTIME_PROJECT_ID \<br>--pubsub-topic=projects/$PROJECT_ID/topics/$CAI_FEED_TOPIC_ID \<br>--asset-types=&quot;^.*.googleapis.com/.*$&quot; \<br>--content-type=org-policy</pre><h3>Reference</h3><p><a href="https://cloud.google.com/asset-inventory/docs/monitoring-asset-changes">Monitoring asset changes | Cloud Asset Inventory Documentation | Google Cloud</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a8551a9a1192" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-cloud/configure-cai-feed-for-real-time-notifications-about-gcp-resource-changes-a8551a9a1192">Configure CAI Feed for Real Time Notifications about GCP Resource Changes</a> was originally published in <a href="https://medium.com/google-cloud">Google Cloud - Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[IaC CI/CD integration for Terraform Vet]]></title>
            <link>https://medium.com/google-cloud/iac-ci-cd-integration-for-terraform-vet-d67ef528a982?source=rss-4240d250293a------2</link>
            <guid isPermaLink="false">https://medium.com/p/d67ef528a982</guid>
            <category><![CDATA[infrastructure]]></category>
            <category><![CDATA[policy-as-code]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[infrastructure-as-code]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <dc:creator><![CDATA[Ethan Han]]></dc:creator>
            <pubDate>Fri, 23 Jun 2023 03:20:30 GMT</pubDate>
            <atom:updated>2023-06-23T03:32:57.608Z</atom:updated>
            <content:encoded><![CDATA[<p>Co-author: <a href="https://medium.com/u/bfae6c6189f3">Ran Zhang</a></p><h3>Background</h3><p>One of the biggest concern about Infrastructure as code(IaC) is about configuration errors that could lead to security and governance violations. Security and Cloud administrators need to be able to set up organization level constraints that enforce security best practices. And it must also be compatible with tools across the entire application lifecycle, from development to deployment to auditing of deployed resources.</p><p><a href="https://cloud.google.com/sdk/gcloud/reference/beta/terraform/vet">gcloud beta terraform vet</a> is a tool that can be used to enforce policy compliance as part of an infrastructure CI/CD pipeline. Please refer our <a href="https://zhangran1.medium.com/policy-validation-preventive-control-with-terraform-vet-1895c8ab47ef">articles</a> about how the <a href="https://cloud.google.com/sdk/gcloud/reference/beta/terraform/vet">gcloud beta terraform vet</a> works and how to setup <a href="https://github.com/GoogleCloudPlatform/policy-library">Policy Library</a> in your organization.</p><p>This article we will talk about how integrate <a href="https://cloud.google.com/sdk/gcloud/reference/beta/terraform/vet">gcloud beta terraform vet</a> with your CI/CD pipeline.</p><h3>Assumptions</h3><ol><li>The CI/CD is based on <a href="https://cloud.google.com/build">Cloud Build</a> but the idea works similar in tools like <a href="https://docs.gitlab.com/ee/ci/">GitLab CI/CD</a> and <a href="https://docs.gitlab.com/ee/ci/">GitHub Actions</a></li><li>The dummy terraform code and Policy Library is stored in <a href="https://cloud.google.com/source-repositories/docs">Cloud Source Repositories</a> but you can change to your preferred tools.</li><li>This article will not cover all the permissions instruction</li><li>GCP project is created and necessary resources, such as bucket</li></ol><h3>How to integrate with IaC CI/CD pipeline?</h3><p><strong>Step1: Create your Policy Library</strong></p><p>Create your Policy Library and push it to <a href="https://cloud.google.com/source-repositories/docs">Cloud Source Repositories</a>. <a href="https://cloud.google.com/source-repositories/docs">Cloud Source Repositories</a> for the details, please refer to <a href="https://zhangran1.medium.com/policy-validation-preventive-control-with-terraform-vet-1895c8ab47ef">Policy Validation — Preventive Control with Terraform Vet</a>.</p><p>Copy <a href="https://github.com/GoogleCloudPlatform/policy-library/blob/main/samples/storage_location.yaml">storage_location.yaml </a>file into policies/constraints folder and push the code.</p><pre>$ cd path/to/your/policy-library<br>$ cp samples/storage_location.yaml policies/constraints</pre><p><strong>Step2: Create Terraform Dummy Code</strong></p><p>Let’s create a repo folder with dummy terraform code contains backend.tf and main.tf files.</p><pre># backend.tf<br>terraform {<br>  backend &quot;gcs&quot; {<br>    bucket = &quot;test-demo-tfstate&quot; # make sure you have the bucket for terraform state<br>    prefix = &quot;env/dev&quot;<br>  }<br>}<br># main.tf<br>terraform {<br>  required_providers {<br>    google = {<br>      source = &quot;hashicorp/google&quot;<br>      version = &quot;~&gt; 4.36.0&quot;<br>    }<br>  }<br>}<br>provider &quot;google&quot; {<br>  project = &quot;my-proj&quot;  # change it to your project id<br>}<br>resource &quot;google_storage_bucket&quot; &quot;auto-expire&quot; {<br>  name          = &quot;my-proj-simple-demo-0&quot;<br>  # location      = &quot;asia-southeast1&quot;  # allowed location in policy library, the pipeline will success<br>  location      = &quot;us-central2&quot; # disallowed location in policy library, the pipeline will fail<br>  force_destroy = true<br>}</pre><p><strong>Step3: Define cloudbuild.yaml</strong></p><p>Create a new file cloudbuild.yaml, the file defines the IaC pipeline process. We are going to integrate the terraform vet commend before the terraform apply.</p><pre># Copyright 2023 Google LLC<br>#<br># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);<br># you may not use this file except in compliance with the License.<br># You may obtain a copy of the License at<br>#<br>#     https://www.apache.org/licenses/LICENSE-2.0<br>#<br># Unless required by applicable law or agreed to in writing, software<br># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,<br># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.<br># See the License for the specific language governing permissions and<br># limitations under the License.<br><br>timeout: 2700s<br>steps:<br># [START tf init and plan]<br>- id: &#39;tf init and plan&#39;<br>  name: &#39;hashicorp/terraform:1.3.3&#39;<br>  entrypoint: &#39;sh&#39;<br>  args: <br>  - &#39;-c&#39;<br>  - |<br>    terraform init <br>    terraform plan -out=test.tfplan<br>    terraform show -json ./test.tfplan &gt; ./tfplan.json<br>    ls -al tfplan.json <br># [END tf init and plan]<br><br># [START terraform vet]<br>- id: &#39;terraform vet and apply&#39;<br>  name: &#39;gcr.io/google.com/cloudsdktool/google-cloud-cli&#39;<br>  entrypoint: &#39;sh&#39;<br>  args: <br>  - &#39;-c&#39;<br>  - | <br>    gcloud auth list<br>    apt-get install google-cloud-sdk-terraform-tools<br>    # clone policy library<br>    gcloud source repos clone $_POLICY_LIBRARY --project=$PROJECT_ID<br>    cd /workspace/$_POLICY_LIBRARY<br>    git checkout main<br>    echo &quot;CNCac Policy list:&quot;<br>    ls -al /workspace/$_POLICY_LIBRARY/policies/constraints<br>    policy_library_dir=&quot;/workspace/${_POLICY_LIBRARY}&quot;<br>    <br>    cd /workspace/<br>    terra_vet_violations=$(gcloud beta terraform vet tfplan.json --policy-library=$policy_library_dir --format=json)<br>    ret_val=$?<br>    if [ $ret_val -eq 2 ]; then<br>      # Optional: parse the terra_vet_violations variable as json and check the severity level<br>      echo &quot;$terra_vet_violations&quot;<br>      echo &quot;Violations found; not proceeding with terraform apply&quot;<br>      exit 1<br>    fi<br>    if [ $ret_val -ne 0]; then<br>      echo &quot;Error during gcloud beta terraform vet; not proceeding with terraform apply&quot;<br>      exit 1<br>    fi<br>    echo &quot;No policy violations detected; proceeding with terraform apply&quot;<br><br># [END terraform vet]<br># [START tf apply]<br>- id: &#39;tf apply&#39;<br>  name: &#39;hashicorp/terraform:1.3.3&#39;<br>  entrypoint: &#39;sh&#39;<br>  args: <br>  - &#39;-c&#39;<br>  - |<br>    terraform apply -auto-approve<br>    <br># [END tf apply]<br><br><br>logsBucket: &#39;gs://${_BUILD_LOG_BUCKET}&#39;    <br>options:<br>  logging: GCS_ONLY<br>substitutions:<br>    # change it to your policy library repo name<br>    _POLICY_LIBRARY: cncac-policy-library<br>    # make sure you have created the bucket for cloud build log<br>    _BUILD_LOG_BUCKET: cncac-build-log-bucket</pre><p>Now your terraform dummy repo will look like this below, and push your repo to the <a href="https://cloud.google.com/source-repositories/docs">Cloud Source Repositories</a>.</p><pre>my-tf-repo<br>├── backend.tf<br>├── main.tf<br>├── cloudbuil.yaml</pre><p><strong>Step4: Define Cloud Build Trigger</strong></p><p>Here provides detailed introduction about <a href="https://cloud.google.com/build/docs/automating-builds/create-manage-triggers">Cloud Build trigger</a>. We can simply trigger for all push for demo purpose like below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3zuRJvR9fBxm9JvL6siRxw.png" /></figure><p><strong>Step5: Change Code and Commit</strong></p><pre>$ cat main.tf<br>resource &quot;google_storage_bucket&quot; &quot;auto-expire&quot; {<br>  name          = &quot;my-proj-cncac-simple-demo-0&quot;<br>  # location      = &quot;asia-southeast1&quot;  # allowed location, the pipeline will success<br>  location      = &quot;us-central2&quot; # disallowed location, the pipeline will fail<br>  force_destroy = true<br>}<br>$ git add --all .<br>$ git commit -m &quot;feat: create bucket in disallowed location&quot;<br>$ git push</pre><h3>Congratulations!</h3><p>You can see the terraform vet will fail as it managed to detect the policy violation as shown in below picture. So that you can successfully avoid provisioning non-compliance resource into production environment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H8RY0Lclwpz1FGBGCC82ng.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d67ef528a982" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-cloud/iac-ci-cd-integration-for-terraform-vet-d67ef528a982">IaC CI/CD integration for Terraform Vet</a> was originally published in <a href="https://medium.com/google-cloud">Google Cloud - Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>