# AWS CLI — Amazon-Web-Services-Dienste steuern

> Praxis-Guide zur AWS CLI — Dienste wie S3, EC2, IAM und Lambda im Terminal steuern, mit Profilen, Regionen und JSON-, Text- oder Table-Ausgabe.

Source: https://www.jpkc.com/db/cheatsheets/cloud-iac/aws/

<!-- PROSE:intro -->
Die AWS CLI ist die offizielle Kommandozeilen-Schnittstelle von Amazon Web Services und steuert nahezu jeden Dienst – von S3 und EC2 über IAM bis Lambda und ECS – mit einem einheitlichen `aws`-Befehl. Statt dich durch die Web-Konsole zu klicken, automatisierst du Infrastruktur reproduzierbar in Skripten und CI-Pipelines. Mit Profilen wechselst du zwischen Accounts, mit `--region` steuerst du das Rechenzentrum, und über `--output` und `--query` formst du die Ausgabe als JSON, Text oder Tabelle. Dieser Guide zeigt dir die Befehle, die du im Alltag am häufigsten brauchst.
<!-- PROSE:intro:end -->

## Konfiguration & Authentifizierung

`aws configure` — Richtet Zugangsdaten und Standard-Region interaktiv ein. Access-Keys landen im Klartext unter `~/.aws/credentials` – niemals ins Git committen; bevorzuge IAM-Rollen, temporäre Credentials oder SSO (`aws sso login`).

```bash
aws configure
```

`aws configure list` — Zeigt die aktuellen Konfigurationswerte und ihre Quellen.

```bash
aws configure list
```

`aws configure --profile <name>` — Konfiguriert ein benanntes Profil.

```bash
aws configure --profile production
```

`aws sts get-caller-identity` — Zeigt den aktuellen IAM-Benutzer bzw. die IAM-Rolle (prüft die Zugangsdaten).

```bash
aws sts get-caller-identity
```

`export AWS_PROFILE=<name>` — Wechselt für die aktuelle Sitzung zu einem benannten Profil.

```bash
export AWS_PROFILE=production
```

## S3 — Speicher

`aws s3 ls` — Listet alle S3-Buckets auf.

```bash
aws s3 ls
```

`aws s3 ls s3://<bucket>/<prefix>` — Listet Objekte in einem Bucket bzw. unter einem Präfix.

```bash
aws s3 ls s3://my-bucket/uploads/
```

`aws s3 cp <file> s3://<bucket>/<key>` — Lädt eine Datei nach S3 hoch.

```bash
aws s3 cp backup.tar.gz s3://my-bucket/backups/
```

`aws s3 cp s3://<bucket>/<key> <file>` — Lädt eine Datei aus S3 herunter.

```bash
aws s3 cp s3://my-bucket/backups/backup.tar.gz ./backup.tar.gz
```

`aws s3 sync <dir> s3://<bucket>/<prefix>` — Gleicht ein lokales Verzeichnis nach S3 ab (wie rsync). Vorsicht: `--delete` löscht im Ziel alles, was lokal fehlt – vorab mit `--dryrun` testen.

```bash
aws s3 sync ./dist/ s3://my-website-bucket/ --delete
```

`aws s3 rm s3://<bucket>/<key>` — Löscht ein Objekt aus S3 (irreversibel).

```bash
aws s3 rm s3://my-bucket/old-file.txt
```

`aws s3 rm s3://<bucket>/<prefix> --recursive` — Löscht alle Objekte unter einem Präfix. Destruktiv und ohne Nachfrage – vorher mit `--dryrun` prüfen, was getroffen wird.

```bash
aws s3 rm s3://my-bucket/tmp/ --recursive
```

`aws s3 mb s3://<bucket>` — Erstellt einen neuen S3-Bucket.

```bash
aws s3 mb s3://my-new-bucket --region eu-west-1
```

## EC2 — Rechenleistung

`aws ec2 describe-instances` — Listet alle EC2-Instanzen auf.

```bash
aws ec2 describe-instances --query 'Reservations[].Instances[].{ID:InstanceId,State:State.Name,Type:InstanceType}' --output table
```

`aws ec2 start-instances --instance-ids <id>` — Startet eine EC2-Instanz.

```bash
aws ec2 start-instances --instance-ids i-1234567890abcdef0
```

`aws ec2 stop-instances --instance-ids <id>` — Stoppt eine EC2-Instanz.

```bash
aws ec2 stop-instances --instance-ids i-1234567890abcdef0
```

`aws ec2 reboot-instances --instance-ids <id>` — Startet eine EC2-Instanz neu.

```bash
aws ec2 reboot-instances --instance-ids i-1234567890abcdef0
```

`aws ec2 describe-security-groups` — Listet alle Security-Groups auf.

```bash
aws ec2 describe-security-groups --output table
```

## IAM — Identität

`aws iam list-users` — Listet alle IAM-Benutzer auf.

```bash
aws iam list-users --output table
```

`aws iam list-roles` — Listet alle IAM-Rollen auf.

```bash
aws iam list-roles --query 'Roles[].RoleName'
```

`aws iam create-user --user-name <name>` — Legt einen neuen IAM-Benutzer an.

```bash
aws iam create-user --user-name deploy-bot
```

`aws iam attach-user-policy --user-name <user> --policy-arn <arn>` — Hängt eine Policy an einen Benutzer. Folge dem Least-Privilege-Prinzip statt pauschaler `*FullAccess`-Policies.

```bash
aws iam attach-user-policy --user-name deploy-bot --policy-arn arn:aws:iam::aws:policy/AmazonS3FullAccess
```

`aws iam create-access-key --user-name <user>` — Erzeugt Access-Keys für einen Benutzer. Der Secret Key wird nur einmal angezeigt – sicher verwahren, nie ins Git oder in Klartext-Dateien; temporäre Credentials oder IAM-Rollen sind die bessere Wahl.

```bash
aws iam create-access-key --user-name deploy-bot
```

## Lambda & ECS

`aws lambda list-functions` — Listet alle Lambda-Funktionen auf.

```bash
aws lambda list-functions --query 'Functions[].FunctionName'
```

`aws lambda invoke --function-name <name> <output>` — Ruft eine Lambda-Funktion auf und schreibt die Antwort in eine Datei.

```bash
aws lambda invoke --function-name my-function response.json
```

`aws ecs list-clusters` — Listet alle ECS-Cluster auf.

```bash
aws ecs list-clusters
```

`aws ecs list-services --cluster <name>` — Listet die Services in einem ECS-Cluster auf.

```bash
aws ecs list-services --cluster production
```

`aws ecs update-service --cluster <cluster> --service <service> --force-new-deployment` — Erzwingt ein erneutes Deployment eines ECS-Service.

```bash
aws ecs update-service --cluster production --service web --force-new-deployment
```

## Ausgabe & häufige Optionen

`--output <format>` — Setzt das Ausgabeformat (json, table, text, yaml).

```bash
aws ec2 describe-instances --output table
```

`--query '<jmespath>'` — Filtert die Ausgabe mit JMESPath-Ausdrücken.

```bash
aws s3api list-buckets --query 'Buckets[].Name'
```

`--region <region>` — Überschreibt die Standard-Region.

```bash
aws s3 ls --region eu-central-1
```

`--profile <name>` — Nutzt ein bestimmtes benanntes Profil.

```bash
aws s3 ls --profile production
```

`--no-cli-pager` — Deaktiviert den Ausgabe-Pager (praktisch in Skripten).

```bash
aws ec2 describe-instances --no-cli-pager
```

`aws help` — Zeigt die allgemeine Hilfe oder die Hilfe zu einem bestimmten Dienst.

```bash
aws s3 help
```

<!-- PROSE:outro -->
## Fazit

Die AWS CLI bündelt das gesamte AWS-Universum in einem einzigen Befehl und macht Cloud-Infrastruktur skriptbar, reproduzierbar und versionierbar. Behandle deine Zugangsdaten wie Produktiv-Passwörter: setze auf IAM-Rollen, SSO oder temporäre Credentials statt langlebiger Access-Keys, und folge konsequent dem Least-Privilege-Prinzip. Bei destruktiven Operationen wie `s3 rm --recursive`, `s3 rb --force` oder `ec2 terminate-instances` lohnt sich vorab ein `--dryrun` oder ein prüfender Blick, denn gelöschte Ressourcen sind unwiederbringlich.

## Weiterführende Links

- [AWS CLI – offizielle Referenz](https://docs.aws.amazon.com/cli/) – vollständige Dokumentation zu Installation, Konfiguration und allen Service-Befehlen (englisch)
- [AWS CLI Command Reference](https://awscli.amazonaws.com/v2/documentation/api/latest/index.html) – durchsuchbare Referenz aller Unterbefehle und Optionen (englisch)
- [Sicherheit der AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/security.html) – Best Practices zu Credentials, Profilen und IAM (englisch)
<!-- PROSE:outro:end -->

## Verwandte Kommandos

- [ansible](https://www.jpkc.com/db/cheatsheets/cloud-iac/ansible/) – agentenlose Konfigurationsverwaltung und Provisionierung
- [terraform](https://www.jpkc.com/db/cheatsheets/cloud-iac/terraform/) – deklaratives Infrastructure as Code für AWS und andere Provider

