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.yamlfile 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.pulumifolder) are listed in your.gitignoreto 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 codeBelow is an example Pulumi program written in TypeScript. This could be placed in an
index.tsfile inside theinfradirectory 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 infraflag tells Pulumi to change to theinfradirectory where the Pulumi project is located.