๐Ÿ”งDevOps

Pulumi

An IaC tool that defines infrastructure in programming languages. Supports TypeScript and Python.

Detailed Explanation

Pulumi is an Infrastructure as Code (IaC) platform developed by a company founded in 2017, and it defines cloud infrastructure using general-purpose programming languages such as TypeScript, Python, Go, C#, and Java. Because you can use familiar languages instead of a domain-specific language (DSL) like HCL, it is developer-friendly.

The key advantage is that all the features of a programming language (conditionals, loops, functions, classes, package management) can be used as-is in infrastructure code. IDE autocompletion, type checking, and refactoring tools can also be used as they are.

Pulumi supports more than 150 cloud providers, including AWS, Azure, GCP, and Kubernetes. State is stored in Pulumi Cloud (SaaS), S3, Azure Blob, and so on, and Terraform state files can also be migrated.

In practice, it is useful for abstracting complex infrastructure patterns into reusable components and for verifying infrastructure code with unit tests. Compared to Terraform, it has the advantages of a lower learning curve (using existing languages) and easier testing.

Code Example

// TypeScript๋กœ AWS ์ธํ”„๋ผ ์ •์˜
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as awsx from "@pulumi/awsx";

// ์„ค์ • ๊ฐ’ ๋กœ๋“œ
const config = new pulumi.Config();
const instanceType = config.get("instanceType") || "t3.micro";
const desiredCapacity = config.getNumber("desiredCapacity") || 2;

// VPC ์ƒ์„ฑ (์ž๋™์œผ๋กœ ์„œ๋ธŒ๋„ท, NAT ๊ฒŒ์ดํŠธ์›จ์ด ๋“ฑ ๊ตฌ์„ฑ)
const vpc = new awsx.ec2.Vpc("main-vpc", {
    cidrBlock: "10.0.0.0/16",
    numberOfAvailabilityZones: 2,
    natGateways: {
        strategy: "Single",
    },
    tags: {
        Name: "production-vpc",
        Environment: "production",
    },
});

// ECS ํด๋Ÿฌ์Šคํ„ฐ
const cluster = new aws.ecs.Cluster("app-cluster", {
    settings: [{
        name: "containerInsights",
        value: "enabled",
    }],
});

// Application Load Balancer
const alb = new awsx.lb.ApplicationLoadBalancer("app-alb", {
    subnetIds: vpc.publicSubnetIds,
    securityGroups: [],
});

// ECS Fargate ์„œ๋น„์Šค
const service = new awsx.ecs.FargateService("app-service", {
    cluster: cluster.arn,
    desiredCount: desiredCapacity,
    taskDefinitionArgs: {
        container: {
            name: "app",
            image: "nginx:latest",
            cpu: 256,
            memory: 512,
            essential: true,
            portMappings: [{
                containerPort: 80,
                targetGroup: alb.defaultTargetGroup,
            }],
            environment: [
                { name: "NODE_ENV", value: "production" },
            ],
            logConfiguration: {
                logDriver: "awslogs",
                options: {
                    "awslogs-group": "/ecs/app",
                    "awslogs-region": aws.getRegion().then(r => r.name),
                    "awslogs-stream-prefix": "ecs",
                },
            },
        },
    },
    networkConfiguration: {
        subnets: vpc.privateSubnetIds,
        securityGroups: [],
        assignPublicIp: false,
    },
});

// RDS PostgreSQL
const dbSubnetGroup = new aws.rds.SubnetGroup("db-subnet", {
    subnetIds: vpc.privateSubnetIds,
});

const db = new aws.rds.Instance("app-db", {
    engine: "postgres",
    engineVersion: "15.4",
    instanceClass: "db.t3.micro",
    allocatedStorage: 20,
    dbName: "appdb",
    username: "admin",
    password: config.requireSecret("dbPassword"),
    dbSubnetGroupName: dbSubnetGroup.name,
    skipFinalSnapshot: true,
    multiAz: true,
});

// ์ถœ๋ ฅ๊ฐ’ ๋‚ด๋ณด๋‚ด๊ธฐ
export const vpcId = vpc.vpcId;
export const albDns = alb.loadBalancer.dnsName;
export const dbEndpoint = db.endpoint;

// ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ์˜ˆ์‹œ (Jest)
/*
import * as pulumi from "@pulumi/pulumi";
import "jest";

pulumi.runtime.setMocks({
    newResource: (args) => ({ id: `${args.name}-id`, state: args.inputs }),
    call: (args) => args.inputs,
});

describe("Infrastructure", () => {
    let infra: typeof import("./index");

    beforeAll(async () => {
        infra = await import("./index");
    });

    test("VPC should have correct CIDR", async () => {
        const cidr = await infra.vpcCidr;
        expect(cidr).toBe("10.0.0.0/16");
    });
});
*/

How It's Said in Practice

Senior: "There's a limit to building repeated patterns with Terraform HCL. Shall we consider migrating to Pulumi?"

Junior: "Sounds good. If we write it in TypeScript, we get type checking too, and since everyone on our team is familiar with TS, the learning curve should be low. Writing test code will also get easier."

Interviewer: "What is the difference between Pulumi and Terraform?"

Candidate: "Terraform uses a declarative DSL called HCL, while Pulumi uses general-purpose languages such as TypeScript and Python. Pulumi makes conditionals and loops natural and unit testing easy, whereas Terraform has a larger and more mature ecosystem."

Take a mock interview on this topic โ†’ WIA SOOM AI Interview ยท Start for free

Reviewer: "This DB password is in plain text. You should encrypt it using config.requireSecret()."

Developer: "Right, I'll set it with pulumi config set --secret dbPassword and change the code to fetch it with requireSecret()."

Cautions

Learn More