generating-infrastructure-as-code

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Infrastructure As Code Generator

基础设施即代码生成工具

This skill provides automated assistance for infrastructure as code generator tasks.
此技能可为基础设施即代码生成任务提供自动化辅助。

Overview

概述

Generates production-ready IaC (Terraform/CloudFormation/Pulumi/etc.) with modular structure, variables, outputs, and deployment guidance for common cloud stacks.
生成具备模块化结构、变量、输出项的可用于生产环境的IaC(Terraform/CloudFormation/Pulumi等),并为常见云栈提供部署指导。

Prerequisites

前提条件

Before using this skill, ensure:
  • Target cloud provider CLI is installed (aws-cli, gcloud, az)
  • IaC tool is installed (Terraform, Pulumi, AWS CDK)
  • Cloud credentials are configured locally
  • Understanding of target infrastructure architecture
  • Version control system for IaC storage
使用此技能前,请确保:
  • 已安装目标云服务商的CLI工具(aws-cli、gcloud、az)
  • 已安装IaC工具(Terraform、Pulumi、AWS CDK)
  • 已在本地配置云服务商凭证
  • 了解目标基础设施架构
  • 有用于存储IaC的版本控制系统

Instructions

使用步骤

  1. Identify Platform: Determine IaC tool (Terraform, CloudFormation, Pulumi, ARM, CDK)
  2. Define Resources: Specify cloud resources needed (compute, network, storage, database)
  3. Establish Structure: Create modular file structure for maintainability
  4. Generate Code: Write IaC configurations with proper syntax and formatting
  5. Add Variables: Define input variables for environment-specific values
  6. Configure Outputs: Specify outputs for resource references and integrations
  7. Implement State: Set up remote state storage for team collaboration
  8. Document Usage: Add README with deployment instructions and prerequisites
  1. 确定平台:选定IaC工具(Terraform、CloudFormation、Pulumi、ARM、CDK)
  2. 定义资源:指定所需的云资源(计算、网络、存储、数据库)
  3. 搭建结构:创建模块化的文件结构以提升可维护性
  4. 生成代码:编写符合语法规范和格式要求的IaC配置
  5. 添加变量:定义用于环境特定值的输入变量
  6. 配置输出:指定用于资源引用和集成的输出项
  7. 设置状态管理:配置远程状态存储以支持团队协作
  8. 编写使用文档:添加包含部署说明和前提条件的README文件

Output

输出内容

Generates infrastructure as code files:
Terraform Example:
hcl
undefined
生成基础设施即代码文件:
Terraform示例:
hcl
undefined

{baseDir}/terraform/main.tf

{baseDir}/terraform/main.tf

Overview

概述

This skill provides automated assistance for the described functionality.
此技能可为上述功能提供自动化辅助。

Examples

示例

Example usage patterns will be demonstrated in context. terraform { required_version = ">= 1.0" required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } } }
resource "aws_vpc" "main" { cidr_block = var.vpc_cidr enable_dns_hostnames = true
tags = { Name = "${var.project}-vpc" Environment = var.environment } }

**CloudFormation Example:**
```yaml
将在上下文中演示示例使用模式。 terraform { required_version = ">= 1.0" required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } } }
resource "aws_vpc" "main" { cidr_block = var.vpc_cidr enable_dns_hostnames = true
tags = { Name = "${var.project}-vpc" Environment = var.environment } }

**CloudFormation示例:**
```yaml

{baseDir}/cloudformation/template.yaml

{baseDir}/cloudformation/template.yaml

AWSTemplateFormatVersion: '2010-09-09' Description: Production VPC infrastructure
Parameters: VpcCidr: Type: String Default: 10.0.0.0/16
Resources: VPC: Type: AWS::EC2::VPC Properties: CidrBlock: !Ref VpcCidr EnableDnsHostnames: true

**Pulumi Example:**
```typescript
// {baseDir}/pulumi/index.ts
import * as aws from "@pulumi/aws";

const vpc = new aws.ec2.Vpc("main", {
    cidrBlock: "10.0.0.0/16",
    enableDnsHostnames: true,
    tags: {
        Name: "production-vpc"
    }
});

export const vpcId = vpc.id;
AWSTemplateFormatVersion: '2010-09-09' Description: Production VPC infrastructure
Parameters: VpcCidr: Type: String Default: 10.0.0.0/16
Resources: VPC: Type: AWS::EC2::VPC Properties: CidrBlock: !Ref VpcCidr EnableDnsHostnames: true

**Pulumi示例:**
```typescript
// {baseDir}/pulumi/index.ts
import * as aws from "@pulumi/aws";

const vpc = new aws.ec2.Vpc("main", {
    cidrBlock: "10.0.0.0/16",
    enableDnsHostnames: true,
    tags: {
        Name: "production-vpc"
    }
});

export const vpcId = vpc.id;

Error Handling

错误处理

Common issues and solutions:
Syntax Errors
  • Error: "Invalid resource syntax in configuration"
  • Solution: Validate syntax with
    terraform validate
    or respective tool linter
Provider Authentication
  • Error: "Unable to authenticate with cloud provider"
  • Solution: Configure credentials via environment variables or CLI login
Resource Conflicts
  • Error: "Resource already exists"
  • Solution: Import existing resources or use data sources instead of creating new ones
State Lock Issues
  • Error: "Error acquiring state lock"
  • Solution: Ensure no other process is running, or force unlock if safe
Dependency Errors
  • Error: "Resource depends on resource that does not exist"
  • Solution: Check resource references and ensure proper dependency ordering
常见问题及解决方案:
语法错误
  • 错误信息:"配置中的资源语法无效"
  • 解决方案:使用
    terraform validate
    或对应工具的语法检查器验证语法
服务商认证问题
  • 错误信息:"无法与云服务商完成认证"
  • 解决方案:通过环境变量或CLI登录配置凭证
资源冲突
  • 错误信息:"资源已存在"
  • 解决方案:导入现有资源或使用数据源而非创建新资源
状态锁定问题
  • 错误信息:"获取状态锁失败"
  • 解决方案:确保没有其他进程在运行,或在安全前提下强制解锁
依赖错误
  • 错误信息:"资源依赖的资源不存在"
  • 解决方案:检查资源引用并确保依赖顺序正确

Examples

示例场景

  • "Generate Terraform for a VPC + private subnets + NAT + EKS cluster on AWS."
  • "Create a minimal CloudFormation template for an S3 bucket with encryption and public access blocked."
  • "为AWS上的VPC + 私有子网 + NAT + EKS集群生成Terraform代码。"
  • "创建一个最小化的CloudFormation模板,用于配置带加密且禁止公共访问的S3存储桶。"

Resources

参考资源