ec2.instances.set-role

Give the instance an IAM role it gets short-lived credentials for

PUT /ec2/api/instances/{id}/roleIAM ec2:AssociateInstanceRoleChanges state

Also needs iam:PassRole on the role.

Request syntax

PUT /ec2/api/instances/{id}/role
{
  "iamRoleName": "string"
}

Path parameters

  • idRequired

    string

    Constraints: /^i-[0-9a-f]{6,32}$/

Request body

  • iamRoleNameRequired

    string

    IAM role the machine acts as (needs iam:PassRole on it): it gets 60-minute credentials for it through its instance identity (`ews-instance credentials`), with no key in user data

    Constraints: 1-64 chars, /^[\w+=,.@-]+$/

Response

200: `data` is `{ instanceId, iamRoleName }` with the role it now has.

A JSON response is wrapped as { "success": true, "data": … }; the CLI prints the whole body with --json.

Errors

  • 400The request does not pass the schema above. The body is { "error": …, "issues": […] }, one issue per field.
  • 401No credentials, or an access key that is unknown, revoked or expired.
  • 403The caller's policies do not allow ec2:AssociateInstanceRole on this resource. The body names the action and the resource, and the refusal is recorded in the audit log.
  • 404The resource does not exist in your organization.

Examples

The required parameters only, with example values. Replace id and the other placeholders with your own.

dcloud ec2 instances set-role id --iam-role-name example

Generated from the API description, the same one the API checks requests against and the CLI is built from. MCP tool: dcloud_ec2_instances_set-role.