pulumi-basics

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Pulumi Basics

Pulumi 基础

Infrastructure-as-code using real programming languages with Pulumi.
使用Pulumi通过真实编程语言实现基础设施即代码。

Project Structure

项目结构

my-infrastructure/
├── Pulumi.yaml       # Project file
├── Pulumi.dev.yaml   # Stack config
├── index.ts          # Main program
└── package.json
my-infrastructure/
├── Pulumi.yaml       # Project file
├── Pulumi.dev.yaml   # Stack config
├── index.ts          # Main program
└── package.json

Pulumi.yaml

Pulumi.yaml

yaml
name: my-infrastructure
user-invocable: false
runtime: nodejs
description: My infrastructure project
yaml
name: my-infrastructure
user-invocable: false
runtime: nodejs
description: My infrastructure project

TypeScript Example

TypeScript 示例

typescript
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

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

// Create subnet
const subnet = new aws.ec2.Subnet("public", {
    vpcId: vpc.id,
    cidrBlock: "10.0.1.0/24",
    availabilityZone: "us-east-1a",
});

// Export outputs
export const vpcId = vpc.id;
export const subnetId = subnet.id;
typescript
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

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

// Create subnet
const subnet = new aws.ec2.Subnet("public", {
    vpcId: vpc.id,
    cidrBlock: "10.0.1.0/24",
    availabilityZone: "us-east-1a",
});

// Export outputs
export const vpcId = vpc.id;
export const subnetId = subnet.id;

Common Commands

常用命令

bash
undefined
bash
undefined

Create new project

Create new project

pulumi new aws-typescript
pulumi new aws-typescript

Preview changes

Preview changes

pulumi preview
pulumi preview

Apply changes

Apply changes

pulumi up
pulumi up

Destroy resources

Destroy resources

pulumi destroy
pulumi destroy

View stack outputs

View stack outputs

pulumi stack output
undefined
pulumi stack output
undefined

Configuration

配置操作

bash
undefined
bash
undefined

Set config

Set config

pulumi config set aws:region us-east-1
pulumi config set aws:region us-east-1

Set secret

Set secret

pulumi config set --secret dbPassword mySecret123
pulumi config set --secret dbPassword mySecret123

Get config

Get config

pulumi config get aws:region
undefined
pulumi config get aws:region
undefined

Best Practices

最佳实践

Use Stack References

使用栈引用

typescript
const infraStack = new pulumi.StackReference("org/infra/prod");
const vpcId = infraStack.getOutput("vpcId");
typescript
const infraStack = new pulumi.StackReference("org/infra/prod");
const vpcId = infraStack.getOutput("vpcId");

Component Resources

组件资源

typescript
class MyApp extends pulumi.ComponentResource {
    constructor(name: string, args: MyAppArgs, opts?: pulumi.ComponentResourceOptions) {
        super("custom:app:MyApp", name, {}, opts);
        
        // Create resources
    }
}
typescript
class MyApp extends pulumi.ComponentResource {
    constructor(name: string, args: MyAppArgs, opts?: pulumi.ComponentResourceOptions) {
        super("custom:app:MyApp", name, {}, opts);
        
        // Create resources
    }
}