Home > Software design >  Attach AWS IAM Profile to Azure VM
Attach AWS IAM Profile to Azure VM

Time:09-28

Is there a way where to attach an AWS IAM profile to an Azure VM.

I'm trying to develop a common infrastructure for Azure and AWS and i want to use resources which are in AWS from an Azure VM.

I know this can do this by exporting AWS creds to Azure VM but is there a way where I can attach an already existing AWS IAM profile to the Azure VM (if not directly may be through an interface or a service?) and access the resources (which how is I'm doing from an ec2 instance currently) ?

CodePudding user response:

Sadly you can't do this. IAM instance profiles are only valid and usable from ec2 instances. You can't use instance profiles from outside of aws.

As you mentioned, you have to explicitly provide aws credentials to your azure vm. For example by creating .aws/ folder with aws profile.

CodePudding user response:

You should be able to achieve what you are looking for by using the same IAM role for both the EC2 instance profile and a managed identity assigned to your Azure VM.

From my limited understanding of AWS, the instance profile identifies your EC2 instance, so you cant use it directly.

To use the same role for both EC2 and Azure VM, here is how I would try this out:

First, familiarize yourself with how you can assign an Azure managed identity a role in AWS. I wrote this blog post recently to show how Managed identities can be granted access to AWS resources: https://blog.identitydigest.com/azuread-access-aws/

Now rather than create a new role as mentioned in the blog, you can reuse the role in your EC2 instance, by adding a trust relationship for the managed identity to AssumeRoleWithWebIdentity.

So the trust relationship for your existing role used in your EC2 profile will look something as follows: (please note, I have not tried multiple statements in a role but expect this to work based on the AWS documentation)

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "ec2.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    },
    {
      "Effect": "Allow",
      "Principal": {
        "Federated": "arn:aws:iam::<your account>:oidc-provider/sts.windows.net/<your azure ad tenant>"
      },
      "Action": "sts:AssumeRoleWithWebIdentity",
      "Condition": {
        "StringEquals": {
          "sts.windows.net/<your tenant>:aud": "app audience or managed identity client_id",
          "sts.windows.net/<your tenant>:sub": "in case you want to also include sub"
        }
      }
    }
  ]
}

Now if you assign the managed identity to the VM, it should be able to access the same resources as your EC2 instance.

  • Related