Terrain¶
Umfassende Terraform-Befehle und Workflows für Infrastruktur als Code (IaC), einschließlich Ressourcenmanagement, staatliche Operationen und Multi-Cloud-Einsätze.
Installation und Inbetriebnahme
| Command | Description |
|---|---|
| INLINE_CODE_16 | Show Terraform version |
| INLINE_CODE_17 | Show help information |
| INLINE_CODE_18 | Show help for specific command |
| _ | |
| Kern-Workflow |
Basisoperationen¶
| Command | Description |
|---|---|
| INLINE_CODE_19 | Initialize working directory |
| INLINE_CODE_20 | Create execution plan |
| INLINE_CODE_21 | Apply changes |
| INLINE_CODE_22 | Destroy infrastructure |
| INLINE_CODE_23 | Validate configuration |
| INLINE_CODE_24 | Format configuration files |
| _ | |
| ### Advanced Planning | |
| Command | Description |
| --------- | ------------- |
| INLINE_CODE_25 | Save plan to file |
| INLINE_CODE_26 | Apply saved plan |
| INLINE_CODE_27 | Plan specific resource |
| INLINE_CODE_28 | Plan with variables |
| INLINE_CODE_29 | Plan with variable file |
Staatliche Verwaltung
State Operations¶
| Command | Description |
|---|---|
| INLINE_CODE_30 | List resources in state |
| INLINE_CODE_31 | Show resource details |
| INLINE_CODE_32 | Move resource in state |
| INLINE_CODE_33 | Remove resource from state |
| INLINE_CODE_34 | Download remote state |
| INLINE_CODE_35 | Upload state to remote |
State Backup and Recovery_¶
| Command | Description |
|---|---|
| INLINE_CODE_36 | Create state backup |
| INLINE_CODE_37 | Force unlock state |
| INLINE_CODE_38 | Update state with real resources |
Arbeitsräume
| Command | Description |
|---|---|
| INLINE_CODE_39 | List workspaces |
| INLINE_CODE_40 | Create new workspace |
| INLINE_CODE_41 | Switch to workspace |
| INLINE_CODE_42 | Delete workspace |
| INLINE_CODE_43 | Show current workspace |
| _ | |
| Import und Output |
Import Resources¶
| Command | Description |
|---|---|
| INLINE_CODE_44 | Import existing resource |
| INLINE_CODE_45 | Import AWS instance |
Outputs¶
| Command | Description |
|---|---|
| INLINE_CODE_46 | Show all outputs |
| INLINE_CODE_47 | Show specific output |
| INLINE_CODE_48 | Show outputs in JSON |
Beispiele für die Konfiguration
Basic AWS EC2 instance¶
provider "aws" \\\\{
region = "us-west-2"
\\\\}
resource "aws_instance" "example" \\\\{
ami = "ami-0c55b159cbfafe1d0"
instance_type = "t2.micro"
tags = \\\\{
Name = "example-instance"
\\\\}
\\\\}
output "instance_ip" \\\\{
value = aws_instance.example.public_ip
\\\\}
```_
* Variablen
```hcl
variable "instance_type" \\\\{
description = "EC2 instance type"
type = string
default = "t2.micro"
\\\\}
variable "environment" \\\\{
description = "Environment name"
type = string
\\\\}
```_
### Datenquellen
```hcl
data "aws_ami" "ubuntu" \\\\{
most_recent = true
owners = ["099720109477"] # Canonical
filter \\\\{
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
\\\\}
\\\\}
```_
### Module
```hcl
module "vpc" \\\\{
source = "terraform-aws-modules/vpc/aws"
name = "my-vpc"
cidr = "10.0.0.0/16"
azs = ["us-west-2a", "us-west-2b"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24"]
enable_nat_gateway = true
enable_vpn_gateway = true
\\\\}
```_
Remote State Configuration
### S3 Backend
```hcl
terraform \\\\{
backend "s3" \\\\{
bucket = "my-terraform-state"
key = "state/terraform.tfstate"
region = "us-west-2"
\\\\}
\\\\}
```_
### Azure Backend
```hcl
terraform \\\\{
backend "azurerm" \\\\{
resource_group_name = "tfstate"
storage_account_name = "tfstate"
container_name = "tfstate"
key = "prod.terraform.tfstate"
\\\\}
\\\\}
```_
### Google Cloud Backend
```hcl
terraform \\\\{
backend "gcs" \\\\{
bucket = "tf-state-bucket"
prefix = "terraform/state"
\\\\}
\\\\}
```_
Konfiguration des Anbieters
### AWS Provider
```hcl
provider "aws" \\\\{
region = "us-west-2"
profile = "default"
default_tags \\\\{
tags = \\\\{
Environment = "production"
Project = "my-project"
\\\\}
\\\\}
\\\\}
```_
### Azure Provider
```hcl
provider "azurerm" \\\\{
features \\\\{\\\\}
subscription_id = "00000000-0000-0000-0000-000000000000"
tenant_id = "00000000-0000-0000-0000-000000000000"
\\\\}
```_
### Google Cloud Provider
```hcl
provider "google" \\\\{
project = "my-project-id"
region = "us-central1"
zone = "us-central1-c"
\\\\}
```_
Erweiterte Eigenschaften
### Bedingte Ressourcen
```hcl
resource "aws_instance" "example" \\\\{
count = var.create_instance ? 1 : 0
ami = data.aws_ami.ubuntu.id
instance_type = var.instance_type
\\\\}
```_
Für jeden
```hcl
resource "aws_instance" "example" \\\\{
for_each = toset(var.instance_names)
ami = data.aws_ami.ubuntu.id
instance_type = var.instance_type
tags = \\\\{
Name = each.key
\\\\}
\\\\}
```_
### Dynamische Blöcke
```hcl
resource "aws_security_group" "example" \\\\{
name = "example"
dynamic "ingress" \\\\{
for_each = var.ingress_rules
content \\\\{
from_port = ingress.value.from_port
to_port = ingress.value.to_port
protocol = ingress.value.protocol
cidr_blocks = ingress.value.cidr_blocks
\\\\}
\\\\}
\\\\}
```_
Prüfung und Validierung
### Terraform Validate
|Command|Description|
|---------|-------------|
|__INLINE_CODE_49__|Validate syntax|
|__INLINE_CODE_50__|Validate with JSON output|
### Terraform Plananalyse
|Command|Description|
|---------|-------------|
|__INLINE_CODE_51__|Plan with detailed exit codes|
|__INLINE_CODE_52__|Show current state|
|__INLINE_CODE_53__|Show state in JSON|
_
### Tools von Drittanbietern
|Command|Description|
|---------|-------------|
|__INLINE_CODE_54__|Terraform linter|
|__INLINE_CODE_55__|Generate documentation|
|__INLINE_CODE_56__|Security scanning|
|__INLINE_CODE_57__|Security analysis|
Debugging und Fehlerbehebung
### Logging
|Command|Description|
|---------|-------------|
|__INLINE_CODE_58__|Enable debug logging|
|__INLINE_CODE_59__|Enable trace logging|
|__INLINE_CODE_60__|Log to file|
### Gemeinsame Themen
|Command|Description|
|---------|-------------|
|__INLINE_CODE_61__|Sync state with reality|
|__INLINE_CODE_62__|Mark resource for recreation|
|__INLINE_CODE_63__|Remove taint from resource|
_
oder Best Practices
### Dateiorganisation
Version Constraints¶
```hcl terraform \\{ required_version = ">= 1.0"
required_providers \\{ aws = \\{ source = "hashicorp/aws" version = "~> 5.0" \\} \\} \\} ```_
Resource Naming¶
```hcl resource "aws_instance" "web_server" \\{ # Use descriptive names ami = data.aws_ami.ubuntu.id instance_type = var.instance_type
tags = \\{ Name = "\(\\\\{var.project_name\\\\}-web-\)\\{var.environment\\}" Environment = var.environment Project = var.project_name \\} \\} ```_
Security Best Practices¶
ANHANG State Security: Verwenden Sie Remote-Zustand mit Verschlüsselung 2. Secrets Management*: Verwenden Sie externe geheime Speicher 3. **Access Control*: Umsetzung richtiger IAM-Politiken 4. Code Review: Alle Infrastrukturänderungen überprüfen 5. Scanning: Verwenden Sie Sicherheits-Scan-Tools
Leistungsoptimierung¶
ANHANG Parallelismus*: Verwenden -parallelism Flagge für große Bereitstellungen
2. **Targeting*: Verwendung -target für spezifische Ressourcen
3. **State Splitting: Große Staaten in kleinere aufgeteilt
4. Module Design: Design wiederverwendbare Module