<?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 Khalid Saifullah on Medium]]></title>
        <description><![CDATA[Stories by Khalid Saifullah on Medium]]></description>
        <link>https://medium.com/@khalid-au?source=rss-b0d08af551a8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*MsSHd3zDOoSXG2djhRWErA.jpeg</url>
            <title>Stories by Khalid Saifullah on Medium</title>
            <link>https://medium.com/@khalid-au?source=rss-b0d08af551a8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 06:22:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@khalid-au/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[Access GCP services from Azure]]></title>
            <link>https://medium.com/@khalid-au/access-gcp-services-from-azure-08b4ad67f164?source=rss-b0d08af551a8------2</link>
            <guid isPermaLink="false">https://medium.com/p/08b4ad67f164</guid>
            <category><![CDATA[azure]]></category>
            <category><![CDATA[identity-federation]]></category>
            <category><![CDATA[entra-id]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <dc:creator><![CDATA[Khalid Saifullah]]></dc:creator>
            <pubDate>Tue, 21 May 2024 00:36:04 GMT</pubDate>
            <atom:updated>2024-05-21T00:49:37.329Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Problem statement:</strong> We have a workload that we would like to run in <a href="https://learn.microsoft.com/en-us/azure/container-apps/overview">Azure Container App</a> and it would deliver an output file in <a href="https://cloud.google.com/storage?hl=en">Google Cloud Storage</a>.</p><p>Although the statement above is referring to specific services from both cloud providers, we would develop a solution that is agnostic to any services. We are going to see how an <a href="https://learn.microsoft.com/en-us/entra/identity/managed-identities-azure-resources/overview">Azure Managed Identity</a> can impersonate as a <a href="https://cloud.google.com/iam/docs/principal-identifiers#v2">Google Cloud IAM Principal</a> and we can assign any role to that Principal.</p><p>We would use <a href="https://developer.hashicorp.com/terraform">terraform</a> to develop the infrastructure configuration and <a href="https://docs.python.org/3/">python</a> for application code.</p><p><a href="https://cloud.google.com/iam/docs/workload-identity-federation">Google Workload Identity Federation</a> enables us to manage GCP access for external identities. The service has two components, Pool and Provider. We shouldn’t need more than one Pool for an Org. Then we need to create one Provider per external identity provider instance. For example, we would create one Provider per AWS account, Azure AD tenant, K8S cluster or GitHub Org. Please follow the <a href="https://cloud.google.com/iam/docs/best-practices-for-using-workload-identity-federation">best practices</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0i1f1otTij4PJX9jld1oIw.jpeg" /><figcaption>Flow diagram: Exchange azure identity token with Google access token</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2lRK8YO_vPOX-Wlo82dS6w.jpeg" /><figcaption>Sequence diagram: Exchange azure identity token with Google access token</figcaption></figure><p><strong>Prerequsites:</strong></p><ol><li>Create an App Registration in <a href="https://entra.microsoft.com/">Microsoft Entra ID</a>, along with a service principal.</li></ol><pre>resource &quot;azuread_application_registration&quot; &quot;this&quot; {<br>  display_name = &quot;azure-gcp-audience&quot;<br>  description  = &quot;Entra app to connect to GCP&quot;<br>}<br><br>resource &quot;azuread_service_principal&quot; &quot;this&quot; {<br>  client_id = azuread_application_registration.this.client_id<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/579/1*RJPk1EegIAC8VQ-RPFjVkA.png" /><figcaption>Registered App should show up in the Entra ID console</figcaption></figure><p>2. Create a Workload Identity Pool and attach a Provider for the Azure tenant.</p><pre>locals {<br>  azure_tenant_id = &quot;9b123b94-08c1-43ba-92ca-b2392da493bf&quot;<br>}<br><br>resource &quot;google_iam_workload_identity_pool&quot; &quot;this&quot; {<br>  project                   = google_project.seed.project_id<br>  workload_identity_pool_id = &quot;my-org&quot;<br>  display_name              = &quot;my-org&quot;<br>  description               = &quot;Identity Pool for My Org&quot;<br>}<br><br>resource &quot;google_iam_workload_identity_pool_provider&quot; &quot;entra_id&quot; {<br>  project                            = google_project.seed.project_id<br>  workload_identity_pool_id          = google_iam_workload_identity_pool.this.workload_identity_pool_id<br>  workload_identity_pool_provider_id = &quot;entra-id&quot;<br>  display_name                       = &quot;Entra ID&quot;<br>  description                        = &quot;OIDC identity pool provider for Entra ID&quot;<br>  attribute_mapping = {<br>    &quot;google.subject&quot; = &quot;assertion.sub&quot;<br>  }<br>  oidc {<br>    allowed_audiences = [azuread_application_registration.this.client_id]<br>    issuer_uri        = &quot;https://login.microsoftonline.com/${local.azure_tenant_id}/v2.0&quot;<br>  }<br>}</pre><p>3. Set Application ID URI for the application created in step-1. The value is the URI of Google Workload Identity Pool Provider.</p><pre>resource &quot;azuread_application_identifier_uri&quot; &quot;this&quot; {<br>  application_id = azuread_application_registration.this.id<br>  identifier_uri = &quot;https://iam.googleapis.com/${google_iam_workload_identity_pool_provider.entra_id.name}&quot;<br>}</pre><p>The resources created in these 3 steps set up the foundation of trust-relationship from Azure to Google. For a set of Azure Tenant and Google Organisation, this setup is required only once. <em>This single setup should be used with any number of Azure Subscriptions in that tenant and Google Projects in that Organisation</em>.</p><p>We have created an App in step-1 to register Google Worklod Identity Pool Provider and we have set the Application ID URI with the value of Provider URI in step-3.</p><p>We have created Google Workload Identity Pool and Provider in step-2. The Provider has a trust relation with the Azure Tenant and the registered App (see the oidc configuration).</p><p><strong>Develop a Solution for the Problem Statement</strong></p><ol><li>Prepare application code.</li></ol><pre>import json<br>import os<br>import requests<br>from azure.identity import ManagedIdentityCredential<br>from google.oauth2.credentials import Credentials<br>from google.cloud import storage<br><br><br># Use the Managed Identity attached to the Azure service running this workload.<br>def get_azure_token(scope: str) -&gt; str:<br>    credential = ManagedIdentityCredential()<br>    return credential.get_token(scope).token<br><br># Exchange Azure Identity Token with Google Access Token.<br>def get_google_access_token(azure_token: str, audience: str) -&gt; str:<br>    payload = {<br>        &quot;audience&quot;: audience,<br>        &quot;grant_type&quot;: &quot;urn:ietf:params:oauth:grant-type:token-exchange&quot;,<br>        &quot;requested_token_type&quot;: &quot;urn:ietf:params:oauth:token-type:access_token&quot;,<br>        &quot;scope&quot;: &quot;https://www.googleapis.com/auth/cloud-platform&quot;,<br>        &quot;subject_token_type&quot;: &quot;urn:ietf:params:oauth:token-type:jwt&quot;,<br>        &quot;subject_token&quot;: azure_token,<br>    }<br>    response = requests.post(url=&quot;https://sts.googleapis.com/v1/token&quot;, data=json.dumps(payload))<br>    return response.json().get(&quot;access_token&quot;)<br><br># Use Google Access Token to access Google Services.<br>def write_object(project_id: str, access_token: str, bucket_name: str, object_name: str, data: str) -&gt; None:<br>    storage_client = storage.Client(<br>        project=project_id,<br>        credentials=Credentials(access_token),<br>    )<br>    bucket = storage_client.bucket(bucket_name)<br>    blob = bucket.blob(object_name)<br>    blob.upload_from_string(data)<br><br># Application Entry Point.<br>if __name__ == &quot;__main__&quot;:<br>    audience = f&quot;//iam.googleapis.com/{os.getenv(&#39;GCP_IDENTITY_PROVIDER&#39;)}&quot;<br>    scope = f&quot;https:{audience}&quot;<br>    azure_token = get_azure_token(scope)<br>    google_access_token = get_google_access_token(azure_token, audience)<br>    write_object(<br>        os.getenv(&quot;GOOGLE_PROJECT_ID&quot;),<br>        google_access_token,<br>        os.getenv(&quot;GCS_BUCKET_NAME&quot;),<br>        &quot;foo/bar/test.txt&quot;,<br>        &quot;Hello, World!&quot;<br>    )</pre><p>2. Create container image and publish the image in a registry.</p><p>Azure Container Registry has some base cost (~ USD 6 / month minimum). I didn’t like this in their pricing and decided not to use that for this tutorial. I have decided to use a public docker hub repository and put no sensitive information in the image. See the details <a href="https://github.com/brainstation-au/cross-cloud-access-examples/blob/main/azure-job-to-gcp/image.tf">here</a>.</p><p>3. Deploy the app as a Azure Container App Job. Also create storage bucket and permission in Google.</p><pre>locals {<br>  app_name  = &quot;azure-job-to-gcp&quot;<br>  image_uri = &quot;somewhere/azure-job-to-gcp:latest&quot;<br>}<br><br>resource &quot;azurerm_resource_group&quot; &quot;this&quot; {<br>  name     = local.app_name<br>  location = &quot;Australia East&quot;<br>}<br><br>resource &quot;azurerm_log_analytics_workspace&quot; &quot;this&quot; {<br>  name                = local.app_name<br>  location            = azurerm_resource_group.this.location<br>  resource_group_name = azurerm_resource_group.this.name<br>  sku                 = &quot;PerGB2018&quot;<br>  retention_in_days   = 30<br>}<br><br>resource &quot;azurerm_container_app_environment&quot; &quot;this&quot; {<br>  name                       = local.app_name<br>  location                   = azurerm_resource_group.this.location<br>  resource_group_name        = azurerm_resource_group.this.name<br>  log_analytics_workspace_id = azurerm_log_analytics_workspace.this.id<br>}<br><br>resource &quot;azurerm_container_app_job&quot; &quot;this&quot; {<br>  name                         = local.app_name<br>  location                     = azurerm_resource_group.this.location<br>  resource_group_name          = azurerm_resource_group.this.name<br>  container_app_environment_id = azurerm_container_app_environment.this.id<br><br>  replica_timeout_in_seconds = 60<br>  replica_retry_limit        = 1<br><br>  identity {<br>    type = &quot;SystemAssigned&quot;<br>  }<br><br>  manual_trigger_config {<br>    parallelism              = 1<br>    replica_completion_count = 1<br>  }<br><br>  template {<br>    container {<br>      image  = local.image_uri<br>      name   = local.app_name<br>      cpu    = 0.25<br>      memory = &quot;0.5Gi&quot;<br>      env {<br>        name  = &quot;GCP_IDENTITY_PROVIDER&quot;<br>        value = google_iam_workload_identity_pool_provider.entra_id.name<br>      }<br>      env {<br>        name  = &quot;GOOGLE_PROJECT_ID&quot;<br>        value = var.google_project_id<br>      }<br>      env {<br>        name  = &quot;GCS_BUCKET_NAME&quot;<br>        value = google_storage_bucket.this.name<br>      }<br>    }<br>  }<br>}<br><br>resource &quot;google_storage_bucket&quot; &quot;this&quot; {<br>  name                        = local.app_name<br>  location                    = &quot;AUSTRALIA-SOUTHEAST1&quot;<br>  force_destroy               = true<br>  uniform_bucket_level_access = true<br><br>  public_access_prevention = &quot;enforced&quot;<br>}<br><br>resource &quot;google_storage_bucket_iam_member&quot; &quot;this&quot; {<br>  bucket = google_storage_bucket.this.name<br>  role   = &quot;roles/storage.objectAdmin&quot;<br>  member = &quot;principal://iam.googleapis.com/${google_iam_workload_identity_pool.this.name}/subject/${one(azurerm_container_app_job.this.identity[*].principal_id)}&quot;<br>}</pre><p>Find the container app in your azure console and trigger a run. Check the storage bucket in Google console and you should see an object in less than a minute.</p><p>You can find an implementation of this solution here: <a href="https://github.com/brainstation-au/cross-cloud-access-examples">https://github.com/brainstation-au/cross-cloud-access-examples</a>. To follow the best practices, Workload Identity Pool and Providers are managed in a seperate project and this project requires their names through input variables. Same for App Registraion in Entra ID and Resource Group in Azure.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=08b4ad67f164" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Access AWS services from GCP]]></title>
            <link>https://medium.com/@khalid-au/access-aws-services-from-gcp-4a9b230bcc02?source=rss-b0d08af551a8------2</link>
            <guid isPermaLink="false">https://medium.com/p/4a9b230bcc02</guid>
            <category><![CDATA[identity-federation]]></category>
            <category><![CDATA[gcp]]></category>
            <category><![CDATA[access-management]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Khalid Saifullah]]></dc:creator>
            <pubDate>Sun, 12 May 2024 23:32:10 GMT</pubDate>
            <atom:updated>2024-05-17T02:22:15.042Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Problem statement:</strong> We have a workload that we would like to run in Google Cloud Functions and it would deliver an output file in AWS S3.</p><p>Although the statement above is referring to specific services from both cloud providers, we would develop a solution that is agnostic to any services. We are going to explore how to assume an AWS IAM Role using <a href="https://cloud.google.com/iam/docs/principal-identifiers">Google Cloud Identity Principal</a>.</p><p>We would use <a href="https://developer.hashicorp.com/terraform">terraform</a> to develop the infrastructure configuration and <a href="https://docs.python.org/3/">python</a> for application code.</p><ol><li>Create an AWS S3 bucket to store the output object.</li></ol><pre>resource &quot;aws_s3_bucket&quot; &quot;this&quot; {<br>  bucket        = &quot;my-bucket-for-gcp-functions-to-aws&quot;<br>  force_destroy = true<br>}<br><br>resource &quot;aws_s3_bucket_ownership_controls&quot; &quot;this&quot; {<br>  bucket = aws_s3_bucket.this.id<br>  rule {<br>    object_ownership = &quot;BucketOwnerPreferred&quot;<br>  }<br>}<br><br>resource &quot;aws_s3_bucket_acl&quot; &quot;this&quot; {<br>  depends_on = [aws_s3_bucket_ownership_controls.this]<br>  bucket     = aws_s3_bucket.this.id<br>  acl        = &quot;private&quot;<br>}</pre><p>2. Create a Google Service Account and an AWS IAM Role establishing <strong>trust relationship</strong> with the GCP SA.</p><pre>resource &quot;google_service_account&quot; &quot;this&quot; {<br>  account_id   = &quot;my-service-account&quot;<br>  display_name = &quot;GCP FN to AWS&quot;<br>  project      = var.google_project_id<br>}<br><br>resource &quot;aws_iam_role&quot; &quot;this&quot; {<br>  name = &quot;my-iam-role&quot;<br><br>  assume_role_policy = jsonencode({<br>    Version = &quot;2012-10-17&quot;<br>    Statement = [<br>      {<br>        Action = &quot;sts:AssumeRoleWithWebIdentity&quot;<br>        Effect = &quot;Allow&quot;<br>        Principal = {<br>          Federated = &quot;accounts.google.com&quot;<br>        }<br>        Condition = {<br>          StringEquals = {<br>            &quot;accounts.google.com:aud&quot; = google_service_account.this.unique_id<br>          }<br>        }<br>      },<br>    ]<br>  })<br>}</pre><p>3. Let the IAM Role have full access to our bucket created in step-1.</p><pre>resource &quot;aws_iam_role_policy&quot; &quot;this&quot; {<br>  name = &quot;s3-access&quot;<br>  role = aws_iam_role.this.id<br>  policy = jsonencode({<br>    Version = &quot;2012-10-17&quot;<br>    Statement = [<br>      {<br>        Action   = &quot;s3:ListBucket&quot;<br>        Effect   = &quot;Allow&quot;<br>        Resource = aws_s3_bucket.this.arn<br>      },<br>      {<br>        Action   = &quot;s3:*&quot;<br>        Effect   = &quot;Allow&quot;<br>        Resource = &quot;${aws_s3_bucket.this.arn}/*&quot;<br>      },<br>    ]<br>  })<br>}</pre><p>4. Prepare code for Google Cloud Functions.</p><pre>import os<br><br>import requests<br>import boto3<br>import functions_framework<br><br># Get JWT token which identifies the service account attached with the Google Cloud Function (compute instance).<br>def get_identity_token():<br>    url = &quot;http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity&quot;<br>    params = {<br>        &quot;format&quot;: &quot;standard&quot;,<br>        &quot;audience&quot;: &quot;aws.amazon.com&quot;<br>    }<br>    headers = {<br>        &quot;Metadata-Flavor&quot;: &quot;Google&quot;<br>    }<br>    return requests.get(<br>        url=url,<br>        params=params,<br>        headers=headers,<br>    ).text<br><br># Assume AWS IAM Role and get credentials.<br>def get_aws_credentials(id_token):<br>    client = boto3.client(&#39;sts&#39;)<br>    return client.assume_role_with_web_identity(<br>        RoleArn=os.getenv(&quot;AWS_ROLE_ARN&quot;),<br>        RoleSessionName=os.getenv(&quot;AWS_ROLE_SESSION_NAME&quot;),<br>        WebIdentityToken=id_token,<br>    ).get(&quot;Credentials&quot;)<br><br># Use AWS credentials to write an object in an S3 bucket.<br>def write_object(credentials, key, content):<br>    client = boto3.client(<br>        &#39;s3&#39;,<br>        aws_access_key_id=credentials.get(&quot;AccessKeyId&quot;),<br>        aws_secret_access_key=credentials.get(&quot;SecretAccessKey&quot;),<br>        aws_session_token=credentials.get(&quot;SessionToken&quot;),<br>    )<br>    return client.put_object(<br>        Bucket=os.getenv(&quot;S3_BUCKET_NAME&quot;),<br>        Key=key,<br>        Body=content,<br>    )<br><br># Entrypoint for Goole Cloud Function.<br>@functions_framework.http<br>def hello_http(request):<br>    id_token = get_identity_token()<br>    credentials = get_aws_credentials(id_token)<br>    metadata = write_object(credentials, &quot;foo/bar/test.txt&quot;, b&quot;Hello, World!&quot;)<br>    return metadata.get(&quot;ETag&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A25fuE1vboDD22zQQlPNrQ.jpeg" /><figcaption>Flow diagram on how to assume AWS IAM role from GCP compute services</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6fReW6cS9b9WO8PlKUPNYg.jpeg" /><figcaption>Sequence diagram on how to assume AWS IAM role from GCP compute services</figcaption></figure><p>5. Upload the function code to a Google Cloud Storage (GCS) bucket.</p><pre>resource &quot;google_storage_bucket&quot; &quot;this&quot; {<br>  name                        = &quot;my-gcs-bucket-for-gcp-functions-to-aws&quot;<br>  location                    = var.google_cloud_region<br>  force_destroy               = true<br>  uniform_bucket_level_access = true<br><br>  public_access_prevention = &quot;enforced&quot;<br>}<br><br>data &quot;archive_file&quot; &quot;this&quot; {<br>  type        = &quot;zip&quot;<br>  output_path = &quot;${path.module}/tmp/functions.zip&quot; # A temporary zip file to upload to GCS bucket.<br>  source_dir  = &quot;${path.module}/functions&quot; # Folder where the function code is saved.<br>}<br><br>resource &quot;google_storage_bucket_object&quot; &quot;this&quot; {<br>  name   = data.archive_file.this.id<br>  source = data.archive_file.this.output_path<br>  bucket = google_storage_bucket.this.name<br>}</pre><p>6. Create Google Cloud Function.</p><pre>resource &quot;google_cloudfunctions2_function&quot; &quot;this&quot; {<br>  name        = &quot;my-cloud-function&quot;<br>  location    = var.google_cloud_region<br>  description = &quot;GCP FN to AWS&quot;<br><br>  build_config {<br>    runtime     = &quot;python311&quot;<br>    entry_point = &quot;hello_http&quot;<br>    source {<br>      storage_source {<br>        bucket = google_storage_bucket.this.name<br>        object = google_storage_bucket_object.this.name<br>      }<br>    }<br>  }<br><br>  service_config {<br>    max_instance_count    = 1<br>    available_memory      = &quot;256M&quot;<br>    timeout_seconds       = 60<br>    service_account_email = google_service_account.this.email<br>    environment_variables = {<br>      AWS_DEFAULT_REGION    = var.aws_region<br>      AWS_ROLE_ARN          = aws_iam_role.this.arn<br>      AWS_ROLE_SESSION_NAME = &quot;gcp-functions-to-aws&quot;<br>      S3_BUCKET_NAME        = aws_s3_bucket.this.id<br>    }<br>  }<br>}</pre><p>7. <a href="https://cloud.google.com/functions/docs/create-deploy-http-python#test_your_deployed_function">Execute the Google Cloud Function for test</a> and you should find an S3 object with key and content set in step-4.</p><p>You can find an implementation of this solution here: <a href="https://github.com/brainstation-au/cross-cloud-access-examples">https://github.com/brainstation-au/cross-cloud-access-examples</a>.</p><p>There is another article on how to <a href="https://medium.com/@khalid-au/access-gcp-services-from-aws-5199901916ef">access GCP services from AWS</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4a9b230bcc02" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Access GCP services from AWS]]></title>
            <link>https://medium.com/@khalid-au/access-gcp-services-from-aws-5199901916ef?source=rss-b0d08af551a8------2</link>
            <guid isPermaLink="false">https://medium.com/p/5199901916ef</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[workload-identity]]></category>
            <category><![CDATA[aws-iam-role]]></category>
            <category><![CDATA[identity-federation]]></category>
            <category><![CDATA[gcp]]></category>
            <dc:creator><![CDATA[Khalid Saifullah]]></dc:creator>
            <pubDate>Tue, 30 Apr 2024 00:17:55 GMT</pubDate>
            <atom:updated>2024-05-28T00:10:44.162Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Problem statement:</strong> We have a workload that we would like to run in AWS Lambda and it would deliver an output file in Google Cloud Storage (GCS).</p><p>Although the statement above is referring to specific services from both cloud providers, we would develop a solution that is agnostic to any services. We are going to see how an AWS IAM Role can impersonate as a Google Cloud Principal and we can assign any role to that Principal.</p><p>We would use <a href="https://developer.hashicorp.com/terraform">terraform</a> to develop the infrastructure configuration and <a href="https://docs.python.org/3/">python</a> for application code.</p><p><a href="https://cloud.google.com/iam/docs/workload-identity-federation">Google Workload Identity Federation</a> enables us to manage GCP access for external identities. The service has two components, Pool and Provider. We shouldn’t need more than one Pool for an Org. Then we need to create one Provider per external identity provider instance. For example, we would create one Provider per AWS account, Azure AD tenant, K8S cluster or GitHub Org. Please follow the <a href="https://cloud.google.com/iam/docs/best-practices-for-using-workload-identity-federation">best practices</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xB1J8IyFcBsV477qrIUXgw.jpeg" /><figcaption>Flow diagram: Exchange signed GetCallerIdentity request with Google access token</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5r8UpzzKt52bA81HVqX6dw.jpeg" /><figcaption>Sequence diagram: Exchange signed GetCallerIdentity request with Google access token</figcaption></figure><p><strong>Prerequisite:</strong> Create a Workload Identity Pool and attach a Provider for the AWS account.</p><pre>resource &quot;google_iam_workload_identity_pool&quot; &quot;this&quot; {<br>  project                   = google_project.seed.project_id<br>  workload_identity_pool_id = &quot;my-org&quot;<br>  display_name              = &quot;my-org&quot;<br>  description               = &quot;Identity Pool for My Org&quot;<br>}<br><br>resource &quot;google_iam_workload_identity_pool_provider&quot; &quot;aws&quot; {<br>  for_each = {<br>    &quot;111122223333&quot; = &quot;Account Name&quot;<br>  }<br>  project                            = var.google_project_id<br>  workload_identity_pool_id          = google_iam_workload_identity_pool.this.workload_identity_pool_id<br>  workload_identity_pool_provider_id = &quot;aws-${each.key}&quot;<br>  display_name                       = &quot;AWS ${each.value}&quot;<br>  description                        = &quot;Workflow identity pool provider for AWS Account ${each.key}&quot;<br>  attribute_condition                = &quot;assertion.arn.startsWith(&#39;arn:aws:sts::${each.key}:assumed-role/&#39;)&quot;<br>  attribute_mapping = {<br>    &quot;google.subject&quot;     = &quot;assertion.arn&quot;<br>    &quot;attribute.account&quot;  = &quot;assertion.account&quot;<br>    &quot;attribute.aws_role&quot; = &quot;&#39;arn:aws:sts::${each.key}:assumed-role/&#39; + assertion.arn.extract(&#39;assumed-role/{role_name}/&#39;)&quot;<br>  }<br>  aws {<br>    account_id = each.key<br>  }<br>}</pre><p>Idially we should create the Identity Pool and Providers in a dedicated Google Project. All other projects in your Google Organisation them. We need only one Provider for an AWS Account in a Google Organisation.</p><p><strong>Develop a Solution for the Problem Statement</strong></p><ol><li>Prepare application code.</li></ol><pre>import os<br><br>from google.auth import aws<br>from google.cloud import storage<br><br><br># Use google-auth to setup Credentials.<br>def get_credentials(google_identity_provider: str) -&gt; aws.Credentials:<br>    config = {<br>        &quot;type&quot;: &quot;external_account&quot;,<br>        &quot;audience&quot;: f&quot;//iam.googleapis.com/{google_identity_provider}&quot;,<br>        &quot;subject_token_type&quot;: &quot;urn:ietf:params:aws:token-type:aws4_request&quot;,<br>        &quot;token_url&quot;: &quot;https://sts.googleapis.com/v1/token&quot;,<br>        &quot;credential_source&quot;: {<br>            &quot;environment_id&quot;: &quot;aws1&quot;,<br>            &quot;region_url&quot;: &quot;http://169.254.169.254/latest/meta-data/placement/availability-zone&quot;,<br>            &quot;url&quot;: &quot;http://169.254.169.254/latest/meta-data/iam/security-credentials&quot;,<br>            &quot;regional_cred_verification_url&quot;: &quot;https://sts.{region}.amazonaws.com?Action=GetCallerIdentity&amp;Version=2011-06-15&quot;<br>        }<br>    }<br>    credentials = aws.Credentials.from_info(config)<br>    return credentials.with_scopes([&quot;https://www.googleapis.com/auth/cloud-platform&quot;])<br><br># Write a test object in GCS.<br>def write_object(project_id: str, credentials: aws.Credentials, bucket_name: str, object_name: str, data: str) -&gt; None:<br>    storage_client = storage.Client(<br>        project=project_id,<br>        credentials=credentials,<br>    )<br>    bucket = storage_client.bucket(bucket_name)<br>    blob = bucket.blob(object_name)<br>    blob.upload_from_string(data)<br><br>def handler(event, context) -&gt; None:<br>    google_identity_provider = os.getenv(&quot;GOOGLE_IDENTITY_PROVIDER&quot;)<br>    credentials = get_credentials(google_identity_provider)<br>    google_project_id = os.getenv(&quot;GOOGLE_PROJECT_ID&quot;)<br>    bucket_name = os.getenv(&quot;GCS_BUCKET_NAME&quot;)<br>    write_object(<br>        project_id=google_project_id,<br>        credentials=credentials,<br>        bucket_name=bucket_name,<br>        object_name=&quot;foo/bar/test.txt&quot;,<br>        data=&quot;Hello, World!&quot;<br>    )</pre><p>The config in get_credentials is generic. You can <a href="https://cloud.google.com/iam/docs/workload-identity-federation-with-other-clouds#create-cred-config">download that from your google console</a>.</p><p>2. Prepare a docker image for lambda function and upload them to a repository.</p><p>You can find an example <a href="https://github.com/brainstation-au/cross-cloud-access-examples/tree/main/aws-lambda-to-gcp">here</a>. We need the Image URI for the lambda function.</p><p>3. We need a GCS Bucket to deliver our output file.</p><pre>resource &quot;google_storage_bucket&quot; &quot;this&quot; {<br>  name                        = &quot;my-bucket-xyz&quot;<br>  location                    = &quot;AUSTRALIA-SOUTHEAST1&quot;<br>  force_destroy               = true<br>  uniform_bucket_level_access = true<br><br>  public_access_prevention = &quot;enforced&quot;<br>}</pre><p>4. Create an AWS Lambda along with a basic IAM Role.</p><pre>resource &quot;aws_iam_role&quot; &quot;this&quot; {<br>  name = local.app_name<br>  path = local.aws_iam_role_path<br>  assume_role_policy = jsonencode({<br>    Version = &quot;2012-10-17&quot;<br>    Statement = [<br>      {<br>        Action = &quot;sts:AssumeRole&quot;<br>        Effect = &quot;Allow&quot;<br>        Principal = {<br>          Service = &quot;lambda.amazonaws.com&quot;<br>        }<br>      },<br>    ]<br>  })<br>  managed_policy_arns = [<br>    &quot;arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole&quot;,<br>  ]<br>}<br><br>resource &quot;aws_lambda_function&quot; &quot;this&quot; {<br>  function_name = local.app_name<br>  role          = aws_iam_role.this.arn<br>  architectures = [&quot;arm64&quot;] # Provide the correct architecture for your image.<br>  description   = &quot;This is an example lambda function&quot;<br>  image_uri     = var.image_uri # Provide the docker image URI.<br>  environment {<br>    variables = {<br>      GOOGLE_IDENTITY_PROVIDER = google_iam_workload_identity_pool_provider.aws[&quot;111122223333&quot;].name<br>      GOOGLE_PROJECT_ID        = var.google_project_id<br>      GCS_BUCKET_NAME          = google_storage_bucket.this.name<br>    }<br>  }<br>  package_type = &quot;Image&quot;<br>  timeout      = 60<br>}</pre><p>5. Assign GCP IAM Role roles/storage.objectAdmin to Workload Identity Principal that corresponds to AWS IAM Role for the Lambda.</p><pre>data &quot;aws_caller_identity&quot; &quot;current&quot; {}<br><br>resource &quot;google_storage_bucket_iam_member&quot; &quot;this&quot; {<br>  bucket = google_storage_bucket.this.name<br>  role   = &quot;roles/storage.objectAdmin&quot;<br>  member = &quot;principalSet://iam.googleapis.com/${google_iam_workload_identity_pool.this.name}/attribute.aws_role/arn:aws:sts::${data.aws_caller_identity.current.account_id}:assumed-role/${aws_iam_role.this.name}&quot;<br>}</pre><p>Let’s test the Lambda from AWS Console and you will find an object foo/bar/test.txt in your GCS Bucket.</p><p>You can find an implementation of this solution here: <a href="https://github.com/brainstation-au/cross-cloud-access-examples">https://github.com/brainstation-au/cross-cloud-access-examples</a>. To follow the best practices, Workload Identity Pool and Providers are managed in a seperate project and this project requires their names through input variables.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5199901916ef" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Event Routing in AWS: Routing Rules]]></title>
            <link>https://medium.com/@khalid-au/event-routing-in-aws-routing-rules-45ad882fb59d?source=rss-b0d08af551a8------2</link>
            <guid isPermaLink="false">https://medium.com/p/45ad882fb59d</guid>
            <category><![CDATA[routing-rule]]></category>
            <category><![CDATA[amazon-eventbridge]]></category>
            <category><![CDATA[event-driven-architecture]]></category>
            <category><![CDATA[event-routing]]></category>
            <dc:creator><![CDATA[Khalid Saifullah]]></dc:creator>
            <pubDate>Thu, 14 Jul 2022 02:12:18 GMT</pubDate>
            <atom:updated>2022-07-14T02:12:18.247Z</atom:updated>
            <content:encoded><![CDATA[<p>Previous Blog: <a href="https://medium.com/@khalid-au/event-routing-in-aws-5ebd07a688e0">Event Routing in AWS</a></p><h3>Amazon EventBridge rules</h3><p>A rule matches incoming events and sends them to targets for processing. A single rule can send an event to multiple targets, which then run in parallel.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LNIxCCxYKosKoB-WDsno8A.jpeg" /><figcaption>Simple usecase of EventBridge</figcaption></figure><p>Each rule requires 3 information:</p><ol><li>An Event Bus ARN</li><li>One or more target ARN and attributes based on each target type</li><li>An event pattern, which effectively is a filter criteria for events</li></ol><h3>An Example Event</h3><p>In the previous blog, we talked about event router, where any type of events can land and get routed based on the content it carries. Let’s look at an example event:</p><pre>{<br>  &quot;version&quot;: &quot;0&quot;,<br>  &quot;id&quot;: &quot;c8f9c4b5-76e5-d76a-f980-7011e206042b&quot;,<br>  <strong>&quot;detail-type&quot;: &quot;UserAccountActivity&quot;,<br>  &quot;source&quot;: &quot;my-org.service-a&quot;,</strong><br>  &quot;account&quot;: &quot;123456789012&quot;,<br>  &quot;time&quot;: &quot;2022-01-11T23:36:40Z&quot;,<br>  &quot;region&quot;: &quot;ap-southeast-2&quot;,<br>  &quot;resources&quot;: [],<strong><br>  </strong>&quot;detail&quot;: {<br>    &quot;userid&quot;: &quot;28416cb8-3915-462b-be7c-728194cda0c8&quot;,<br>    &quot;email&quot;: &quot;jane.person@email.com&quot;,<br>    <strong>&quot;action&quot;: &quot;USER_SIGNED_UP&quot;</strong><br>  }<br>}</pre><h3>Event Pattern</h3><p>It’s a list of event attributes and expected values for each of them.</p><pre>{<br>  &quot;source&quot;: [<br>    &quot;my-org.service-a&quot;,<br>    &quot;my-org.service-b&quot;<br>  ],<br>}</pre><p>Any event with $.source equal to my-org.service-a <strong>or</strong> my-org.service-b would satisfy the above rule.</p><pre>{<br>  &quot;source&quot;: [<br>    &quot;my-org.service-a&quot;<br>  ],<br>  &quot;detail-type&quot;: [<br>    &quot;UserAccountActivity&quot;<br>  ],<br>  &quot;detail&quot;: {<br>    &quot;action&quot;: [<br>      &quot;USER_SIGNED_UP&quot;<br>    ]<br>  }<br>}</pre><p>Any event with $.source equal to my-org.service-a <strong>and</strong> $.detail-type equal to UserAccountActivity <strong>and</strong> $.detail.action equal to USER_SIGNED_UP would satisfy the above rule.</p><p>EventBridge ignores the fields in the event that aren’t included in the event pattern. Please have a <a href="https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-patterns.html#eb-create-pattern">read</a> for information on available match types in an event pattern.</p><h3>Conclusion</h3><p>Ability to match values in a deeply nested JSON structure and apply different filter options are very useful in an event routing context.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=45ad882fb59d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Event Routing in AWS]]></title>
            <link>https://medium.com/@khalid-au/event-routing-in-aws-5ebd07a688e0?source=rss-b0d08af551a8------2</link>
            <guid isPermaLink="false">https://medium.com/p/5ebd07a688e0</guid>
            <category><![CDATA[event-driven-architecture]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[amazon-eventbridge]]></category>
            <category><![CDATA[event-routing]]></category>
            <dc:creator><![CDATA[Khalid Saifullah]]></dc:creator>
            <pubDate>Tue, 12 Jul 2022 00:01:25 GMT</pubDate>
            <atom:updated>2022-07-14T02:17:03.093Z</atom:updated>
            <content:encoded><![CDATA[<p>Previous blog: <a href="https://medium.com/@khalid-au/event-streaming-vs-event-routing-274ba968360b">Event Streaming vs Event Routing</a></p><h3>Event and Message</h3><p>The words ‘Event’ and ‘Message’ are overloaded words, even within software architecture, design and development. In this blog, I’ll use them as synonyms, an event/message is a JSON object, carrying information about an occurrence of some kind (event) within a software system (event producer). Some other systems (event consumers) would need enough information about the event, so that they can perform subsequent actions.</p><h3>Routing Expectation</h3><p>Let’s recall a traditional network router, at a high level, it has a routing table, a list of <strong>targets</strong> and corresponding IP blocks. When a packet comes to the router with a destination IP address, the router finds the correct <strong>target</strong> and <strong>forwards</strong> the packet. We can’t find the packet in the router once it finishes the routing operation, there is no storage for packets.</p><p>For event routing, the router needs routing rules to match values of different properties in an event objects. It should check an event object agains all available rules, 0 or more rules would match, and it should <strong>forward</strong> a copy of the event to <strong>all the targets</strong> corresponding to the rules matched.</p><h3>AWS EventBridge</h3><p>AWS offers <a href="https://aws.amazon.com/eventbridge/">EventBridge</a> to solve the problem of Event Routing. It has many component to fulfil wide range of business requirements in the industry.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bTPjMxoddqVFzpZ8dQY9mw.jpeg" /><figcaption>Components of EventBridge Family (Other AWS/external components are in grey)</figcaption></figure><ul><li><strong>Event Bus:</strong> The core of EventBridge service, the router. A regional service, deployed across all availability zone.</li><li><strong>Event Rule:</strong> The routing rule, contains event patterns and a list of targets.</li><li><strong>API Destinations:</strong> Invoke HTTP API with the event object.</li><li><strong>Archives:</strong> Optional long term storage for event objects.</li><li><strong>Replays:</strong> Replay archived events on-demand.</li><li><strong>Global Endpoints:</strong> Automatic fail-over to another Event Bus in a different region.</li><li><strong>Schema Registry:</strong> A place to record and share event schemas (supports cross-account)</li><li><strong>Schema Discoverer:</strong> Analyze event objects as they lands in the Event Bus and register new schema or new version of a schema in Schema Registry.</li></ul><h3>Simple Use Case</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zYCP_iZUuCS2-LVlK0uBow.jpeg" /><figcaption>Simple EventBridge use case</figcaption></figure><p>The producers <strong>put</strong> their events to the Event Bus, Event Bus evaluates each event object against all the event rules and then <strong>forwards</strong> a copy to the <strong>targets</strong> matched. Please note, it’s a <strong>PUSH</strong> model, the targets can NOT <strong>pull</strong> events from the Event Bus. Event Bus has no storage for events, once an event object is forwarded to the right targets, that’s gone. But if a target fails to respond, it has configurable <strong>retries upto 24 hours</strong>.</p><h3>EventBridge Pricing</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gSWjQF3Yr5DyPOPQG1gcSQ.jpeg" /><figcaption>EventBridge Pricing</figcaption></figure><h3>Conclusion</h3><p>Yes, it’s a <strong>router</strong>. Hence, all producers can put their events in a single Bus. The consumer side makes the decision what to consume and put rules in the Bus.</p><p>This eliminates <strong>location coupling</strong> for the events and hence reduces a lot of upfront planning for the producers. For example, a comparison with Kafka, a producer would need to plan which type of event goes to which topic and the consumers need to find multiple topics to get all the events that they want.</p><p>More on <a href="https://aws.amazon.com/event-driven-architecture/">event-drive architecture</a> in AWS.</p><h3>Next</h3><p>I bet you are wondering how flexible and configurable the <strong>Event Rules</strong> are. I’ve addressed that in the <a href="https://medium.com/@khalid-au/event-routing-in-aws-routing-rules-45ad882fb59d">next blog</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5ebd07a688e0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Event Streaming vs Event Routing]]></title>
            <link>https://medium.com/@khalid-au/event-streaming-vs-event-routing-274ba968360b?source=rss-b0d08af551a8------2</link>
            <guid isPermaLink="false">https://medium.com/p/274ba968360b</guid>
            <category><![CDATA[amazon-eventbridge]]></category>
            <category><![CDATA[event-driven-architecture]]></category>
            <category><![CDATA[event-streaming]]></category>
            <category><![CDATA[stream-processing]]></category>
            <category><![CDATA[event-routing]]></category>
            <dc:creator><![CDATA[Khalid Saifullah]]></dc:creator>
            <pubDate>Thu, 07 Jul 2022 10:50:11 GMT</pubDate>
            <atom:updated>2022-07-13T20:57:22.683Z</atom:updated>
            <content:encoded><![CDATA[<p>I’m going to use couple of hypothetical scenarios in this discussion.</p><h3>Scenario #1</h3><p>We are building a realtime recommendation or decisioning engine for an online shopping platform (DemoShopping.com). To be able to produce a recommendation while people are searching for products, our decisioning engine need a list of at least 5 products that the user have looked in detail. To be able to feed a realtime recommendation on the user screen, we need to feed the decisioning engine with realtime data.</p><p>We capture an event whenever someone visits a product detail page. How do we collect <strong>a series of such events</strong> and feed the decisioning engine realtime? We need a streaming platform, which can feed realtime analytics engines like kSQL, Amazon KDA or Apache Flink.</p><p>The main point to take, we have to use an <strong>event streaming</strong> platform, because we need <strong>a series of events</strong> to perform analysis.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n1kByQKmYLUzeHhwc0kKqA.jpeg" /><figcaption>Realtime Recommendation Engine fed by Event Stream</figcaption></figure><h3>Scenario #2</h3><p>We have a list of microservices to deliver whole lifecycle of an online shopping experience. Unless there is a necessity for synchronous communication, we want to keep the integration asynchronous, decoupled.</p><p>We could choose a streaming platform (e.g. Kinesis Data Streams, Topics in a Kafka cluster) to exchange events among the systems.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gfBKVMBEPW-ckQrrA81l_g.jpeg" /><figcaption>Event-Driven Application with Event Streams</figcaption></figure><p>The main point to take, all these decoupled systems can process <strong>events in isolation</strong>. To produce the correct outcome they don’t need to consume <strong>a series of events</strong>.</p><p>Yes, the above statement opens up opportunities to explore options for event exchange in an event-driven application. I’m going to present one here.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DtEAV77b2GbTWrGeh3VydQ.jpeg" /><figcaption>Event-Driven Application with Event Router</figcaption></figure><p>Instead of using a number of event streams for different type of events, we can choose to use an event router that has the capability to filter events and forward a copy of an event to all the right targets.</p><p>What could be an option for such an event router? I’ve writen another <a href="https://medium.com/@khalid-au/event-routing-in-aws-5ebd07a688e0">blog</a> to discuss that in detail.</p><h3>Conclusion</h3><p>Event streaming has it’s own space (e.g. scenario 1) where that’s the only feasible option. Streaming is complex and expensive, where as routing is comperatively simple and cheaper. Hope this discussion would help you make a better decision for your solution design.</p><p><strong>Next:</strong> <a href="https://medium.com/@khalid-au/event-routing-in-aws-5ebd07a688e0">Event Routing in AWS</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=274ba968360b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>