#Lab: Infrastructure with Pulumi
Write infrastructure as code using real programming languages.
#๐ฏ Objectives
- Install and configure Pulumi
- Create infrastructure with TypeScript
- Compare with Terraform approach
#๐ Prerequisites
- Node.js 18+ installed
- AWS credentials configured
#โฑ๏ธ Duration: 30 minutes
#Task 1: Install Pulumi (5 min)
bash
1# Install Pulumi CLI
2curl -fsSL https://get.pulumi.com | sh
3
4# Verify installation
5pulumi version
6
7# Login (local state for learning)
8pulumi login --local#Task 2: Create New Project (5 min)
diagram
mkdir ~/pulumi-lab && cd ~/pulumi-lab # Create new project pulumi new aws-typescript --name my-infra --yes # Project structure: # โโโ Pulumi.yaml # Project config # โโโ Pulumi.dev.yaml # Stack config # โโโ index.ts # Infrastructure code # โโโ package.json # Dependencies # โโโ tsconfig.json # TypeScript config
#Task 3: Write Infrastructure (10 min)
#index.ts
typescript
1import * as pulumi from "@pulumi/pulumi";
2import * as aws from "@pulumi/aws";
3
4// Configuration
5const config = new pulumi.Config();
6const instanceType = config.get("instanceType") || "t3.micro";
7
8// Create VPC
9const vpc = new aws.ec2.Vpc("main", {
10 cidrBlock: "10.0.0.0/16",
11 enableDnsHostnames: true,
12 tags: { Name: "pulumi-vpc" },
13});
14
15// Create subnet
16const subnet = new aws.ec2.Subnet("public", {
17 vpcId: vpc.id,
18 cidrBlock: "10.0.1.0/24",
19 mapPublicIpOnLaunch: true,
20 tags: { Name: "pulumi-subnet" },
21});
22
23// Create security group
24const securityGroup = new aws.ec2.SecurityGroup("web-sg", {
25 vpcId: vpc.id,
26 ingress: [{
27 protocol: "tcp",
28 fromPort: 80,
29 toPort: 80,
30 cidrBlocks: ["0.0.0.0/0"],
31 }],
32 egress: [{
33 protocol: "-1",
34 fromPort: 0,
35 toPort: 0,
36 cidrBlocks: ["0.0.0.0/0"],
37 }],
38});
39
40// Get AMI
41const ami = aws.ec2.getAmi({
42 mostRecent: true,
43 owners: ["amazon"],
44 filters: [{ name: "name", values: ["amzn2-ami-hvm-*-x86_64-gp2"] }],
45});
46
47// Create EC2 instance
48const server = new aws.ec2.Instance("web-server", {
49 ami: ami.then(a => a.id),
50 instanceType: instanceType,
51 subnetId: subnet.id,
52 vpcSecurityGroupIds: [securityGroup.id],
53 tags: { Name: "pulumi-web-server" },
54});
55
56// Exports
57export const vpcId = vpc.id;
58export const publicIp = server.publicIp;#Task 4: Deploy and Manage (10 min)
bash
1# Install dependencies
2npm install
3
4# Preview changes
5pulumi preview
6
7# Deploy
8pulumi up
9
10# View outputs
11pulumi stack output
12
13# View resources
14pulumi stack
15
16# Destroy when done
17pulumi destroy#Pulumi vs Terraform Comparison
| Feature | Pulumi | Terraform |
|---|---|---|
| Language | TypeScript, Python, Go, etc. | HCL |
| Type Safety | Yes | No |
| IDE Support | Full | Limited |
| Testing | Native unit tests | Limited |
| State | Pulumi Cloud or S3 | Terraform Cloud or S3 |
#โ Success Criteria
- Pulumi installed and configured
- Project created with TypeScript
- pulumi preview shows resources
- Infrastructure deployed (optional)
#๐งน Cleanup
bash
pulumi destroy -y
cd ~ && rm -rf pulumi-lab