Amazon Web Services (AWS) - Associate an Instance Profile with an EC2 Instance
by
Jeremy Canfield |
Updated: January 26 2024
| Amazon Web Services (AWS) articles
This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI.
The aws iam list-instance-profiles command can be used to list the Instance Profiles you have created. Something like this should be returned.
~]$ aws iam list-instance-profiles
{
"InstanceProfiles": [
{
"Path": "/",
"InstanceProfileName": "my-instance-profile",
"InstanceProfileId": "AIPA2MITL76GNDM4CZFIM",
"Arn": "arn:aws:iam::123456789012:instance-profile/my-instance-profile",
"CreateDate": "2024-01-26T02:20:03+00:00",
"Roles": [
{
"Path": "/",
"RoleName": "test",
"RoleId": "AROA2MITL76GF46NSCA6E",
"Arn": "arn:aws:iam::123456789012:role/my-role",
"CreateDate": "2024-01-26T02:13:17+00:00",
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::123456789012:user/johndoe"
},
"Action": "sts:AssumeRole",
"Condition": {}
}
]
}
}
]
}
]
}
The aws ec2 associate-iam-instance-profile command can be used to associate the instance profile with your EC2 instances.
~]$ aws ec2 associate-iam-instance-profile --instance-id i-abc123def456gh789 --iam-instance-profile Name=my-instance-profile
{
"IamInstanceProfileAssociation": {
"AssociationId": "iip-assoc-04a03786337a32660",
"InstanceId": "i-abc123def456gh789",
"IamInstanceProfile": {
"Arn": "arn:aws:iam::123456789012:instance-profile/my-instance-profile",
"Id": "AIPA2MITL76GNDM4CZFIM"
},
"State": "associated"
}
}
Did you find this article helpful?
If so, consider buying me a coffee over at