colocate pulumi code and app code
TypeScriptTo colocate Pulumi code with your application code, you would include the Pulumi project files within the same repository as your application. Typically, you would create a
Pulumi.yaml
file along with the necessary Pulumi stack files in a directory within your app's repository, and make sure that Pulumi's state files (usually found in a.pulumi
folder) are listed in your.gitignore
to prevent them from being checked into version control.Here's how you could structure your repository:
/my-application // Your application's root directory |-- /app // Application source code |-- /infra // Pulumi infrastructure code |-- index.ts // Your Pulumi program |-- Pulumi.yaml // Pulumi project file |-- .gitignore // To exclude state files from version control |-- package.json // Node.js package file for app and Pulumi code
Below is an example Pulumi program written in TypeScript. This could be placed in an
index.ts
file inside theinfra
directory mentioned above. This example shows a simple Pulumi stack that creates an AWS S3 bucket:import * as aws from "@pulumi/aws"; const bucket = new aws.s3.Bucket("myBucket", { website: { indexDocument: "index.html", }, }); // Export the name of the bucket export const bucketName = bucket.id;
In your
package.json
, you would include the Pulumi AWS package as a development dependency, and you could include scripts to run Pulumi commands, like so:{ "name": "my-application", "scripts": { "pulumi-up": "pulumi up --cwd infra", "pulumi-preview": "pulumi preview --cwd infra", "pulumi-destroy": "pulumi destroy --cwd infra" }, "devDependencies": { "@pulumi/aws": "^4.0.0", "pulumi": "^3.0.0", "typescript": "^4.0.0" } }
This way, you can run Pulumi commands from the root of your application's repository, and the
--cwd infra
flag tells Pulumi to change to theinfra
directory where the Pulumi project is located.