Instance profile management for providing AWS credentials to Amazon EC2 instances and other AWS compute services through IAM roles.
Creates a new instance profile that can contain IAM roles for EC2 instances and other AWS services.
/**
* Creates a new instance profile
* @param InstanceProfileName - The name of the instance profile to create
* @param Path - The path to the instance profile (default: /)
* @param Tags - List of tags to attach to the instance profile
*/
interface CreateInstanceProfileCommandInput {
InstanceProfileName: string;
Path?: string;
Tags?: Tag[];
}
interface CreateInstanceProfileCommandOutput {
InstanceProfile: InstanceProfile;
}Usage Example:
import { IAMClient, CreateInstanceProfileCommand } from "@aws-sdk/client-iam";
const client = new IAMClient({ region: "us-east-1" });
const command = new CreateInstanceProfileCommand({
InstanceProfileName: "EC2-S3-Access-Profile",
Path: "/ec2/",
Tags: [
{ Key: "Environment", Value: "Production" },
{ Key: "Service", Value: "WebApp" }
]
});
const result = await client.send(command);
console.log("Instance Profile created:", result.InstanceProfile.Arn);Retrieves information about the specified instance profile, including the roles contained in it.
/**
* Retrieves information about the specified instance profile
* @param InstanceProfileName - The name of the instance profile to get information about
*/
interface GetInstanceProfileCommandInput {
InstanceProfileName: string;
}
interface GetInstanceProfileCommandOutput {
InstanceProfile: InstanceProfile;
}Lists the instance profiles that have the specified path prefix.
/**
* Lists the instance profiles that have the specified path prefix
* @param PathPrefix - The path prefix for filtering the results (optional)
* @param Marker - Use this parameter only when paginating results
* @param MaxItems - Maximum number of items to return
*/
interface ListInstanceProfilesCommandInput {
PathPrefix?: string;
Marker?: string;
MaxItems?: number;
}
interface ListInstanceProfilesCommandOutput {
InstanceProfiles: InstanceProfile[];
IsTruncated?: boolean;
Marker?: string;
}Lists the instance profiles that contain the specified role.
/**
* Lists the instance profiles that contain the specified role
* @param RoleName - The name of the role to list instance profiles for
* @param Marker - Use this parameter only when paginating results
* @param MaxItems - Maximum number of items to return
*/
interface ListInstanceProfilesForRoleCommandInput {
RoleName: string;
Marker?: string;
MaxItems?: number;
}
interface ListInstanceProfilesForRoleCommandOutput {
InstanceProfiles: InstanceProfile[];
IsTruncated?: boolean;
Marker?: string;
}Adds the specified IAM role to the specified instance profile. An instance profile can contain only one role.
/**
* Adds the specified IAM role to the specified instance profile
* @param InstanceProfileName - The name of the instance profile to update
* @param RoleName - The name of the role to add
*/
interface AddRoleToInstanceProfileCommandInput {
InstanceProfileName: string;
RoleName: string;
}Usage Example:
import { IAMClient, AddRoleToInstanceProfileCommand } from "@aws-sdk/client-iam";
const command = new AddRoleToInstanceProfileCommand({
InstanceProfileName: "EC2-S3-Access-Profile",
RoleName: "EC2-S3-Read-Role"
});
await client.send(command);
console.log("Role added to instance profile successfully");Removes the specified IAM role from the specified instance profile.
/**
* Removes the specified IAM role from the specified instance profile
* @param InstanceProfileName - The name of the instance profile to update
* @param RoleName - The name of the role to remove
*/
interface RemoveRoleFromInstanceProfileCommandInput {
InstanceProfileName: string;
RoleName: string;
}Deletes the specified instance profile. The instance profile must not have any policies attached or contain any roles.
/**
* Deletes the specified instance profile
* @param InstanceProfileName - The name of the instance profile to delete
*/
interface DeleteInstanceProfileCommandInput {
InstanceProfileName: string;
}Tag and untag instance profiles for organization and access control.
/**
* Adds one or more tags to an IAM instance profile
* @param InstanceProfileName - The name of the instance profile to tag
* @param Tags - List of tags to attach to the instance profile
*/
interface TagInstanceProfileCommandInput {
InstanceProfileName: string;
Tags: Tag[];
}
/**
* Removes the specified tags from the IAM instance profile
* @param InstanceProfileName - The name of the instance profile to untag
* @param TagKeys - List of tag keys to remove
*/
interface UntagInstanceProfileCommandInput {
InstanceProfileName: string;
TagKeys: string[];
}
/**
* Lists the tags attached to the specified IAM instance profile
* @param InstanceProfileName - The name of the instance profile
* @param Marker - Use this parameter only when paginating results
* @param MaxItems - Maximum number of items to return
*/
interface ListInstanceProfileTagsCommandInput {
InstanceProfileName: string;
Marker?: string;
MaxItems?: number;
}
interface ListInstanceProfileTagsCommandOutput {
Tags: Tag[];
IsTruncated?: boolean;
Marker?: string;
}interface InstanceProfile {
Path: string;
InstanceProfileName: string;
InstanceProfileId: string;
Arn: string;
CreateDate: Date;
Roles: Role[];
Tags?: Tag[];
}Instance profiles are the standard way to provide AWS credentials to EC2 instances:
// 1. Create an IAM role with necessary permissions
const createRoleCommand = new CreateRoleCommand({
RoleName: "EC2-S3-Access-Role",
AssumeRolePolicyDocument: JSON.stringify({
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": { "Service": "ec2.amazonaws.com" },
"Action": "sts:AssumeRole"
}]
})
});
// 2. Attach policies to the role
const attachPolicyCommand = new AttachRolePolicyCommand({
RoleName: "EC2-S3-Access-Role",
PolicyArn: "arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess"
});
// 3. Create instance profile
const createProfileCommand = new CreateInstanceProfileCommand({
InstanceProfileName: "EC2-S3-Access-Profile"
});
// 4. Add role to instance profile
const addRoleCommand = new AddRoleToInstanceProfileCommand({
InstanceProfileName: "EC2-S3-Access-Profile",
RoleName: "EC2-S3-Access-Role"
});Instance profiles work with various AWS compute services:
Security:
Organization:
Management:
Roles: Instance profiles contain exactly one IAM role that defines the permissions Policies: Permissions are granted through policies attached to the role within the instance profile EC2 Instances: Instances reference instance profiles to obtain temporary credentials Service-Linked Roles: Some AWS services create instance profiles automatically