# Getting started with Compute

Launch an instance that only you can reach over SSH, log in, and delete everything again, in about ten minutes.

Replace _web-01_ with a name of your own, _203.0.113.4_ with your own public address, and the ids
with the ones each step answers.

## Prerequisites

- Permission for `ec2:RunInstances`, `ec2:CreateSecurityGroup`, `ec2:DescribeInstances`,
  `ec2:TerminateInstances` and `ec2:DeleteSecurityGroup`, and for the CLI's separate rule step
  `ec2:AuthorizeSecurityGroupIngress`; the managed policy `Ec2FullAccess` grants them all.
- In the console, also `ec2:DescribeSecurityGroups` to open **Security groups** in **Clean up**
  (without it the page says "Access denied") and the group's page (without it the page is not
  found); `Ec2FullAccess` grants it too.
- A key pair. The console can make one while you launch; for the CLI and API, create or import one
  first (see [Manage key pairs](manage-key-pairs.md)).

You pay for every started hour the instance runs, until you terminate it in **Clean up**.

## Step 1: Let only your address in

**Console**
You do this in step 2: with no security group added, the form offers **Allow SSH from
_address_ only**, which creates the group `ssh-from-my-ip` (SSH, port 22, from your IPv4 address)
when you choose **Create instance**.

**CLI**
```sh
dcloud ec2 security-groups create --name ssh-only --description "SSH from my address"
dcloud ec2 security-groups authorize-ingress sg-uuid --protocol tcp --from-port 22 --to-port 22 --cidr-ipv4 203.0.113.4/32
```

**API**
```http
POST /ec2/api/security-groups
{
  "name": "ssh-only",
  "rules": [
    { "direction": "inbound", "protocol": "tcp", "fromPort": 22, "toPort": 22, "cidrIpv4": "203.0.113.4" }
  ]
}
```

A single address is stored as `/32`.

## Step 2: Launch the instance

**Console**
1. Open **Compute**, then choose **Create instance**.
2. Under **Basics**, for **Name**, enter web-01. Keep **Region** `de1`.
3. Under **Size**, keep the preselected **Instance type** (the cheapest shared type in stock), or
   choose another one that is in stock.
4. Under **Image**, keep the preselected **Boot image** (Ubuntu 24.04).
5. Under **Network**, in the banner **Every port of this machine will be open to the internet.**,
   choose **Allow SSH from _address_ only**.
6. Under **Security**, for **Login key pair**, choose your key pair, or create one from the picker.
7. Choose **Create instance**.

The instance page opens with the state **Pending**, then **Running**.

**CLI**
```sh
dcloud ec2 instances run --region-id de1 --server-type performance-2 \
  --image-id 114690387 --name web-01 \
  --ssh-key-pair-id key-pair-uuid --security-group-ids sg-uuid
dcloud ec2 instances wait i-3f9a2c07
```

`wait` returns once the instance is running.

**API**
```http
POST /ec2/api/instances
{
  "serverType": "performance-2",
  "imageId": 114690387,
  "regionId": "de1",
  "name": "web-01",
  "sshKeyPairId": "3f0c2a8e-0000-4000-8000-000000000001",
  "securityGroupIds": ["3f0c2a8e-0000-4000-8000-000000000002"]
}
```

The answer is `201` with the instance in state `pending`. Image ids come from
`GET /ec2/api/catalog/images`.

## Step 3: Log in

The instance's **Overview** shows the SSH command under **Connect** once the instance has a
public address:

```sh
ssh -i ~/.ssh/my-key root@192.0.2.10
```

The default user on the platform images is `root`; point `-i` at the private half of your key
pair. To log in from the browser instead, open the instance's **Connect** tab.

## Clean up

**Console**
1. On the instance page, choose the **Danger zone** tab, then **Terminate instance**.
2. Enter the instance name and choose **Terminate**. The state turns **Terminated** once the
   machine is deleted.
3. Open **Security groups**, open `ssh-from-my-ip`, choose the **Danger zone** tab, then **Delete
   security group**.
4. Enter the group's name and choose **Delete security group**. While the instance is not yet
   terminated, deleting is refused: the group is still attached.

**CLI**
```sh
dcloud ec2 instances terminate i-3f9a2c07
dcloud ec2 instances wait i-3f9a2c07 --state terminated
dcloud ec2 security-groups delete sg-uuid
```

**API**
```http
DELETE /ec2/api/instances/i-3f9a2c07
```

Then, once the instance is `terminated`, `DELETE /ec2/api/security-groups/{id}`.

## Next steps

- [Launch an instance](launch-an-instance.md): every launch option.
- [Create and attach a volume](create-and-attach-a-volume.md)
- [Use an elastic IP](use-an-elastic-ip.md)
