Search code examples
pythonamazon-web-servicesamazon-s3infrastructure-as-codepulumi

Read local state/checkpoint values Pulumi


TL;DR. I would like to ready output from previous stack in newer one with local Pulumi stack saving. For example to create a AWS Fargate ECS cluster in previously created VPC/Subnets. How to do that in Python?

I've created dev Pulumi stack, applied code:

$ mkdir pulumi-infra-az
$ pulumi login --local
$ pulumi stack init dev

And got such Outputs:

...
Outputs:
    pulumi-private-subnet-ids: [
        [0]: "subnet-0dcbaabe273db8feb"
        [1]: "subnet-08c63207611c6bae2"
        [2]: "subnet-00fa346a71a323551"
    ]
    pulumi-public-subnet-ids : [
        [0]: "subnet-02c50846690f2cd70"
        [1]: "subnet-06282506863db7ac1"
        [2]: "subnet-0cfae8a4f5e4fc03c"
    ]
    pulumi-vpc-id            : "vpc-0767f0d49e3a59d42"

Resources:
    ~ 3 updated
    22 unchanged

Duration: 10s

Permalink: file:///root/.pulumi/stacks/dev.json
...

As you can see here I am using local stack placement /root/.pulumi/stacks/dev.json. So far so good. Now in other dir I would like to create fargate cluster description:

$ mkdir pulumi-ecs-fargate
$ pulumi stack init dev-ecs # by the way can I use the same `dev` stack name here?

And here I need to read previously created pulumi-private-subnet-ids, pulumi-public-subnet-ids, pulumi-vpc-id output values? How to do that correct?

I've found only https://app.pulumi.com backend examples:
https://www.pulumi.com/docs/intro/concepts/organizing-stacks-projects/#inter-stack-dependencies
https://www.pulumi.com/docs/intro/concepts/programming-model/#stack-references
https://www.pulumi.com/docs/tutorials/aws/aws-py-stackreference/

Could anybody provide local or AWS s3 example how to read output in other stack/dir?


Solution

  • Ok, reading local state outputs is also possible. Login and create first stack:

    $ pulumi logout
    $ pulumi login --local
    
    $ mkdrir pulumi-infra-az
    $ cd pulumi-infra-az
    
    $ pulumi stack init pulumi-infra-az-dev
    

    Apply it:

    $ pulumi up
    
    ...
    Outputs:
        pulumi-private-subnet-ids: [
            [0]: "subnet-0e8eb4cd276720a51"
            [1]: "subnet-0447d96727f6fdf62"
            [2]: "subnet-02e0e1d44183f7733"
        ]
        pulumi-public-subnet-ids : [
            [0]: "subnet-00b1c052633b93f73"
            [1]: "subnet-0333dd2abc409acb7"
            [2]: "subnet-006e949371228f8bd"
        ]
        pulumi-vpc-id            : "vpc-0e59fc2d7df06bac0"
    
    Resources:
        + 25 created
    ...
    

    Now create new dir and stack:

    $ mkdir pulumi-ecs-fargate
    $ cd pulumi-ecs-fargate
    
    $ pulumi stack init pulumi-ecs-fargate-dev
    

    Stacks name should be diffent. And than read outputs in new one:

    ...
    # Reading local state
    infra = pulumi.StackReference(f"pulumi-infra-az-dev")
    
    # Read back the default VPC and public subnets, which we will use.
    pulumi_vpc = infra.get_output("pulumi-vpc-id")
    pulumi_private_subnets = infra.get_output("pulumi-private-subnet-ids")
    pulumi_public_subnets = infra.get_output("pulumi-public-subnet-ids")
    ...
    

    Very sad that Pulumi doesn't have good manuals/examples.