Featured image of post Pulumi in a Nutshell

Pulumi in a Nutshell

Pulumi in a Nutshell

Pulumi is an Infrastructure as Code (IaC) tool that lets you define cloud infrastructure using real programming languages instead of domain-specific ones.

This means you can finally write your infrastructure like you write your applications.

If you’ve ever wished for loops and conditionals in Terraform, Pulumi is your new best friend.


A Brief History of Pulumi

Pulumi was founded in 2017 by some smart folks who decided that writing infrastructure in JSON, YAML, or HCL was painful.

Istead of creating Yet Another Configuration Language™, they made Pulumi, which lets you use Python, TypeScript, JavaScript, Go, and .NET to provision cloud resources.

Since its launch, Pulumi has gained traction as an alternative to Terraform, especially among developers who prefer using real programming languages to manage infrastructure.


Pulumi vs. Other IaC Tools

FeaturePulumiTerraformCloudFormationAnsible
Multi-Cloud✅ Yes✅ Yes❌ No (AWS-only)✅ Yes
LanguagePython, JS, Go, .NETHCLYAML/JSONYAML
State ManagementManaged or self-hostedSelf-managedAWS-managedNo state
Imperative or DeclarativeImperativeDeclarativeDeclarativeImperative
Best ForDevOps & developersDevOps teamsAWS-heavy shopsConfig management

If you love programming, use Pulumi. If you want multi-cloud support but don’t mind HCL, use Terraform. If you only use AWS, CloudFormation is fine. If you just need to install software, Ansible is your go-to.


Common Pulumi Code Examples

1. Deploy an AWS S3 Bucket

1
2
3
4
5
import pulumi
import pulumi_aws as aws

bucket = aws.s3.Bucket("my-bucket")
pulumi.export("bucket_name", bucket.id)

2. Create an EC2 Instance

1
2
3
4
instance = aws.ec2.Instance("my-instance",
    ami="ami-12345678",
    instance_type="t2.micro")
pulumi.export("instance_id", instance.id)

3. Provision an RDS Database

1
2
3
4
5
db = aws.rds.Instance("my-db",
    engine="mysql",
    instance_class="db.t3.micro",
    allocated_storage=20)
pulumi.export("db_endpoint", db.endpoint)

4. Deploy a Kubernetes Cluster

1
2
3
import pulumi_kubernetes as k8s
cluster = k8s.core.v1.Namespace("my-namespace")
pulumi.export("namespace_name", cluster.metadata.name)

5. Create an IAM Role

1
2
3
4
5
6
7
8
role = aws.iam.Role("my-role",
    assume_role_policy="""
    {
      "Version": "2012-10-17",
      "Statement": [{"Effect": "Allow", "Principal": {"Service": "ec2.amazonaws.com"}, "Action": "sts:AssumeRole"}]
    }
    """)
pulumi.export("role_name", role.name)

6. Set Up a Load Balancer

1
2
3
4
lb = aws.lb.LoadBalancer("my-lb",
    internal=False,
    load_balancer_type="application")
pulumi.export("lb_dns", lb.dns_name)

7. Define a CloudWatch Alarm

1
2
3
4
5
alarm = aws.cloudwatch.MetricAlarm("my-alarm",
    comparison_operator="GreaterThanThreshold",
    threshold=80,
    evaluation_periods=2)
pulumi.export("alarm_arn", alarm.arn)

8. Use Pulumi Variables

1
2
3
4
5
6
import pulumi
from pulumi import Config

config = Config()
instance_type = config.get("instance_type") or "t2.micro"
pulumi.export("instance_type", instance_type)

9. Deploy a Static Website on S3

1
2
3
bucket = aws.s3.Bucket("website",
    website={"index_document": "index.html"})
pulumi.export("website_url", bucket.website_endpoint)

10. Automate Secret Management with AWS Secrets Manager

1
2
secret = aws.secretsmanager.Secret("my-secret")
pulumi.export("secret_id", secret.id)

Key Ideas Table

ConceptExplanation
PulumiIaC tool that uses real programming languages
Multi-cloud supportWorks with AWS, Azure, GCP, Kubernetes, and more
State ManagementCan be managed by Pulumi or self-hosted
ComparisonCompetes with Terraform, CloudFormation, and Ansible
Example UsesProvisioning VMs, Load Balancers, Kubernetes, and IAM roles