connecting to VM instance having no external IP

Viewed 533

I am trying to connect to a google cloud VM instance having no external IP address via cloud shell and cloud SDK.

Google document says that we can connect it using IAP Connecting through IAP: refer using IAP

a) Grant the roles/iap.tunnelResourceAccessor role to the user that wants to connect to the VM.

b) Connect to the VM using below command

gcloud compute ssh instance-name --zone zone

OR Using IAP for TCP forwarding: refer using TCP forwarding we can also connect by setting a ingress firewall rule for IP '35.235.240.0/20' with port TCP:22 and select a IAM role Select Cloud IAP > IAP-Secured Tunnel User

what's the difference between these two different approach and what's the difference in these two separate IAM roles

roles/iap.tunnelResourceAccessor
IAP-secured Tunnel User

I am new to cloud so please bear with my basic knowledge.

2 Answers

It's exactly the same thing. Look at this page

IAP-Secured Tunnel User (roles/iap.tunnelResourceAccessor)

You have the display name of the role: IAP-Secured Tunnel User that you see in the GUI, and you have the technical name of the role roles/iap.tunnelResourceAccessor that you have to use in the script and CLI

The link mentioned in the question ("refer using IAP") actually points to the Connecting to instances that do not have external IP addresses > Connecting through a bastion host. Connection through a bastion host is another method apart from access via IAP.

As described in the document Connecting to instances that do not have external IP addresses > Connecting through IAP,

IAP's TCP forwarding feature wraps an SSH connection inside HTTPS. IAP's TCP forwarding feature then sends it to the remote instance.

Therefore both parts of the question (before OR and after OR) belong to the same access method: Connect using Identity-Aware Proxy for TCP forwarding. Hence the answer to the first question is "no difference" because all of that describes how the IAP TCP forwarding works and those are the steps to set it up and use:

1. Create a firewall rule that:

  • applies to all VM instances that you want to be accessible by using IAP;
  • allows ingress traffic from the IP range 35.235.240.0/20 (this range contains all IP addresses that IAP uses for TCP forwarding);
  • allows connections to all ports that you want to be accessible by using IAP TCP forwarding, for example, port 22 for SSH.

2. Grant permissions to use IAP:

  • Use GCP Console or gcloud to add a role IAP-Secured Tunnel User (roles/iap.tunnelResourceAccessor) to users.
    Note: Users with Owner access to a project always have permission to use IAP for TCP forwarding.

3. Connect to the target VM with one of the following tools:

  • GCP Console: use the SSH button in the Cloud Console;
  • gcloud compute ssh INSTANCE_NAME

There's an important explanation of how IAP TCP forwarding is invoked for accessing a VM instance without Public IP. See Identity-Aware Proxy > Doc > Using IAP for TCP forwarding:

NOTE. If the instance doesn't have a Public IP address, the connection automatically uses IAP TCP tunneling. If the instance does have a public IP address, the connection uses the public IP address instead of IAP TCP tunneling.
You can use the --tunnel-through-iap flag so that gcloud compute ssh always uses IAP TCP tunneling.

As already noted by guillaume blaquiere, roles/iap.tunnelResourceAccessor and IAP-secured Tunnel User are not the different IAM roles, but the Role Name and the Role Title of the same Role. There is one more resource that represents this in a convenient form:
Cloud IAM > Doc > Understanding roles > Predefined roles > Cloud IAP roles

Related