Run a command on an instance
Run one shell command on an instance through Connect and get its exit code and output, without an open port 22 or an SSH key, for example to deploy from CI.
Run Command works like a terminal without the terminal. Deutsche Cloud logs in to the machine's own
sshd through its Deutsche Cloud Connect tunnel with a certificate valid for 2 minutes, runs the command
with exec (no terminal), sends it your stdin, and keeps the exit code and the last 256 KiB of
stdout and stderr. The command runs on its own: your request returns as soon as it has started, and
the result is there to read when it ends. The machine's sshd log names every command:
sshd[2331]: Accepted publickey for root from 127.0.0.1 port 55166 ssh2: ED25519-CERT SHA256:… ID ews:cmd-4f1c9a0b7d2e6385:deployer (serial …)Prerequisites
- Permission for
ec2:RunCommandon the instance. It is its own action:ec2:StartSession(the terminal) does not include it. No managed policy has it exceptAdministratorAccess: notEc2FullAccess, notPowerUserAccess, notDeveloperAccess. Attach a policy that names it (see Allow it on some instances only). - An instance whose Connect state is connected (
dcloud ec2 instances connect-status). Any other state is refused with409and the state's message, for example for an instance launched withconnect: false. See Instance access. - The command runs as
rootor as the image's default user, the same users a terminal may log in as.PermitRootLogin noin your sshd configuration stops commands as root.
Run a command
dcloud ec2 instances run-command i-3f9a2c07 --command 'systemctl restart app && systemctl is-active app'The CLI sends the command, waits for it to end, writes its stdout and stderr to its own stdout and stderr as they arrive, and exits with the command's exit code. Options:
| Option | Meaning |
|---|---|
--user <name> |
root (the default) or the image's default user |
--timeout-seconds <n> |
1 to 3600, default 600; then the command is stopped and ends as timed_out |
--stdin-file <path> or - |
The command's standard input, at most 1 MiB |
--json |
Print the final record as JSON instead of the output |
Ctrl-C cancels the command on the machine and waits for it to end; a second Ctrl-C leaves at once.
While it waits, the CLI reads the command's state every 1 to 3 seconds. A read that gets no answer,
429 or a 5xx is tried again with a growing pause, for up to 2 minutes in a row. The command runs
on regardless, because it does not depend on the CLI.
Exit codes:
| Exit code | Meaning |
|---|---|
| The command's | It ended with that code (a command killed by a signal: 128 plus its number) |
| 124 | It ran longer than --timeout-seconds and was stopped |
| 130 | It was cancelled |
| 255 | The outcome is unknown: the connection to the machine broke before the command ended, the machine did not confirm that it started, or the API did not answer for 2 minutes while the CLI waited. stderr names the command id and the get-command call that looks it up |
| 2, 3, 4, 5 or 1 | It never started: wrong usage, not signed in, forbidden, not found, or another error, with the message on stderr |
Once the command has started, any other error also puts its id on stderr, so a script can look the command up instead of running it again.
POST /ec2/api/instances/i-3f9a2c07/commands
{
"command": "systemctl restart app && systemctl is-active app",
"user": "root",
"timeoutSeconds": 600
}stdin (optional) is the command's standard input as base64, at most 1 MiB decoded.
The answer is 202 with data.commandId. Then read it until status is no longer running:
GET /ec2/api/instances/i-3f9a2c07/commands/cmd-4f1c9a0b7d2e6385{
"commandId": "cmd-4f1c9a0b7d2e6385",
"status": "succeeded",
"exitCode": 0,
"stdout": "active\n",
"stderr": "",
"stdoutTruncated": false,
"stderrTruncated": false,
"stdoutBytes": 7,
"stderrBytes": 0,
"startedAt": "2026-11-25T09:12:03.411Z",
"finishedAt": "2026-11-25T09:12:05.027Z"
}stdoutBytes and stderrBytes count all the output so far, in bytes of its UTF-8 text, and stdout
and stderr are the last part of it. To print output as it arrives, remember how many bytes you
have printed: the text you got starts at stdoutBytes minus its own length in bytes, so print it
from your count on. When that start lies past your count, the bytes in between were dropped.
DELETE on the same path cancels it. GET /ec2/api/instances/{id}/commands lists the last
commands without their output: every command with ec2:DescribeCommands, only the ones you started
with ec2:RunCommand alone.
Statuses
status |
Meaning |
|---|---|
running |
Started, not ended yet |
succeeded |
Exit code 0 |
failed |
Any other exit code, in exitCode |
timed_out |
Ran past timeoutSeconds: it got TERM, and KILL 5 seconds later |
cancelled |
Cancelled with DELETE or Ctrl-C: TERM, then KILL 5 seconds later |
lost |
The connection to the machine broke (machine down, tunnel gone) before the command ended; error says why |
A command the machine did not start is answered with an error (409, 502 or 503) and recorded
as lost with error starting not started:. Such a command never ran, so sending it again is
safe. When the machine took the command but did not confirm it in time, the request still answers
202, and the command is lost with error starting may have started:: its outcome is unknown,
and the CLI exits 255. A command runs at most once, even when the request that started it got no
answer.
TERM and KILL reach the command only through OpenSSH 7.9 or later on the machine. An older sshd
ignores them: the status still becomes timed_out or cancelled and the connection is closed, but
the command may go on running on the machine. timed_out and cancelled say that Deutsche Cloud
stopped the command and stopped waiting for it, not that it ended there. ssh -V on the
machine shows the OpenSSH version.
Commands that start a background process
A command ends when its output closes, not when its shell exits. A process started in the background
that keeps stdout or stderr open, such as nohup ./app &, keeps the command running until
timeoutSeconds, and it ends as timed_out. Then the connection is closed, and the background
process gets SIGPIPE the next time it writes, which usually ends it as well.
To leave something running, start it as a systemd service (systemctl restart app), or send its
output elsewhere and close stdin:
nohup ./app >/var/log/app.log 2>&1 </dev/null &stdin, secrets and what is recorded
- stdin is passed through and nothing else. It goes from your request to the machine and is
never stored, never written to the audit log and never logged. Pass secrets this way, for example
--stdin-file .envwith the commandcat > /etc/app/env. - The command is recorded. The audit log keeps
ec2:RunCommandwith the principal (for a CI role:role/<name>/<session>), the command's id, the user, the command text (its first 1000 characters) and the size of stdin, then a second entry when it ends, with the status and exit code. Never put a secret in the command. - Output is kept for 7 days: the last 256 KiB of stdout and of stderr, as UTF-8. See Who may read and cancel a command.
- An AI agent confirms every command. Through the MCP server,
ec2:RunCommandcounts as destructive: the agent first gets a plan and a confirmation token, and the plan shows only the size of stdin. - At most 10 commands run at once on one instance.
Who may read and cancel a command
| You hold | Read and list | Cancel |
|---|---|---|
ec2:RunCommand |
The commands you started | The commands you started |
ec2:DescribeCommands |
Every command on the instance | None |
ec2:RunCommand and ec2:DescribeCommands |
Every command on the instance | Every command on the instance |
"You" is the principal that started the command: the same user, whether through the console, the
CLI or an access key, and for a role the same role session (role/<name>/<session>), not every
session of the role. An explicit Deny of ec2:DescribeCommands stops reading your own commands
too, while ec2:RunCommand still cancels them.
ec2:DescribeCommands is a sensitive read: command output can hold anything the machine printed,
so it is in no ReadOnly policy. The audit log records every read with ec2:DescribeCommands. A read
of your own command with ec2:RunCommand is recorded once while it runs and on every read after it
ended, not on every poll.
Allow it on some instances only
ec2:RunCommand is in no managed policy except AdministratorAccess, so a principal gets it only
from a policy that names it. It is checked on the instance's resource name, so that policy can name
the instances a role may run commands on:
{
"Version": "2024-01-01",
"Statement": [
{
"Effect": "Allow",
"Action": "ec2:RunCommand",
"Resource": "rn:ews:de1:org-EXAMPLE1234:ec2:instance/i-3f9a2c07"
}
]
}To allow every instance in a project, use "Resource": "rn:ews:de1:org-EXAMPLE1234:ec2:instance/*"
with a condition on ews:ResourceProject. Conditions on an instance's tags are not supported.
Deploy from GitHub Actions with OIDC
The job signs in with its own OIDC token as a role that may run commands on the instance, so no access key and no SSH key is stored in GitHub, and the instance keeps port 22 closed. Set up the identity provider and the role's trust policy as in Deploy from GitHub Actions without a stored key, and give the role the policy above.
permissions:
id-token: write # lets the job request an OIDC token
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
env:
DCLOUD_API_URL: https://console.example.com
DCLOUD_ROLE_RN: rn:ews::org-EXAMPLE1234:iam:role/role-id
INSTANCE_ID: i-3f9a2c07
steps:
- uses: actions/checkout@v4
- name: Sign in (OIDC)
run: |
AUD=$(jq -rn --arg a "$DCLOUD_API_URL" '$a|@uri')
OIDC_TOKEN=$(curl -sS --fail-with-body -H "Authorization: bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
"$ACTIONS_ID_TOKEN_REQUEST_URL&audience=$AUD" | jq -r .value)
CREDS=$(curl -sS --fail-with-body -X POST "$DCLOUD_API_URL/iam/api/sts/assume-role-with-web-identity" \
-H 'content-type: application/json' \
-d "$(jq -n --arg role "$DCLOUD_ROLE_RN" --arg token "$OIDC_TOKEN" '{roleRn: $role, webIdentityToken: $token}')")
KEY="$(jq -r .data.accessKeyId <<<"$CREDS"):$(jq -r .data.secretKey <<<"$CREDS")"
echo "::add-mask::$KEY"
echo "DCLOUD_ACCESS_KEY=$KEY" >> "$GITHUB_ENV"
- name: Install the CLI
run: |
curl -fsSL "$DCLOUD_API_URL/api/cli" -o /usr/local/bin/dcloud
chmod +x /usr/local/bin/dcloud
- name: Deploy
run: |
dcloud ec2 instances run-command "$INSTANCE_ID" --timeout-seconds 900 \
--stdin-file deploy/app.env \
--command 'install -m 600 /dev/stdin /etc/app/app.env && cd /opt/app && git pull --ff-only && systemctl restart app'The step fails when the command fails, with the command's exit code, and its output is in the
job's log. On Deutsche Cloud's own runners, uses: dcloud/configure-credentials@v1 with
role-to-assume replaces the sign-in step (see
Deploy with an OIDC token).