GitHub Copilot for Pulumi Infrastructure as Code: Suggestion Patterns
🔍 WiseChecker

GitHub Copilot for Pulumi Infrastructure as Code: Suggestion Patterns

GitHub Copilot can generate Pulumi code for cloud infrastructure, but its suggestions often need careful guidance. Without proper context, Copilot may propose incorrect resource configurations or miss required properties. This article explains the specific patterns Copilot follows when generating Pulumi programs in TypeScript, Python, and C#. You will learn how to prompt Copilot for accurate infrastructure code and how to avoid common pitfalls.

Key Takeaways: Pulumi Copilot Suggestion Patterns

  • Inline comments with resource names and cloud provider: Tell Copilot the exact Azure, AWS, or GCP resource type to generate correct imports and constructors.
  • Explicit property values in adjacent code: Provide sample property values like region, size, or SKU so Copilot infers the correct schema.
  • Structured output variables and stack references: Define output names and stack references before the resource block to guide Copilot toward valid cross-resource references.

ADVERTISEMENT

How Copilot Generates Pulumi Code

Copilot uses the surrounding code context and language server data to predict the next lines. For Pulumi, this means it reads existing import statements, variable names, and any comments you write. Copilot does not have a special Pulumi training dataset. It relies on patterns seen across millions of public GitHub repositories. When you write a comment like // Create an Azure resource group, Copilot searches for code that follows that pattern and suggests the Pulumi SDK call.

Resource Constructor Pattern

The most common suggestion is the resource constructor. Copilot typically generates:

const resourceGroup = new azure.core.ResourceGroup("example", {
    location: "West US",
});

Copilot infers the class name from the comment. If you write // Storage account, it may suggest new azure.storage.Account. The pattern works best when you include the cloud provider prefix in the comment.

Property Completion Pattern

After you type the opening brace of a resource constructor, Copilot suggests property names and values. It learns from the resource type you imported. For AWS S3 buckets, it suggests bucket, acl, and tags. For Azure VMs, it suggests vmSize, osDisk, and networkInterfaces. Copilot does not always include required properties, so you must verify against the official Pulumi documentation.

Cross-Resource Reference Pattern

When you define two resources, Copilot may suggest passing the output of one as an input to another. For example, after creating a resource group, Copilot might suggest:

const containerGroup = new azure.containerservice.Group("example", {
    resourceGroupName: resourceGroup.name,
    location: resourceGroup.location,
});

This pattern only appears if you have already defined the source resource variable in the same scope. If you rename the variable, Copilot may not update the reference.

Steps to Improve Copilot Suggestions for Pulumi

  1. Write explicit comments before each resource block
    Include the cloud provider and resource type in the comment. For example: // AWS EC2 instance using Pulumi. This tells Copilot which SDK to import and which class to instantiate.
  2. Add a sample property value after the comment
    Write a line like const region = "us-east-1"; directly above the resource block. Copilot uses this as a hint for the location property.
  3. Import the Pulumi SDK explicitly at the top of the file
    Write import as azure from "@pulumi/azure"; or the equivalent for your language. Without the import, Copilot may suggest non-existent class names.
  4. Define output variables before the resource block
    Write export const bucketName = bucket.id; after the resource creation. Copilot uses this to suggest the correct output property.
  5. Use consistent property ordering
    Always put the required properties first, then optional ones. Copilot learns the order from your previous resource blocks and repeats it.
  6. Review all suggested properties against the Pulumi schema
    Copilot may suggest a property that does not exist for a given resource type. Open the Pulumi documentation in your browser and verify the property name.

ADVERTISEMENT

If Copilot Still Suggests Incorrect Pulumi Code

Copilot Suggests a Non-Existent Resource Class

This happens when the comment is too vague or when the import is missing. Fix by writing the exact class name from the Pulumi documentation in the comment. For example, // azure.compute.VirtualMachine instead of // Azure VM. Then let Copilot autocomplete the constructor.

Copilot Omits Required Properties

Copilot often leaves out properties like resourceGroupName or sku because it predicts optional properties first. After Copilot suggests a block, manually add the missing required properties. A quick way is to type the property name and let Copilot suggest the value.

Copilot Suggests Incorrect Output References

If you rename a resource variable after Copilot generated the code, the output references may still point to the old name. Delete the output line and let Copilot regenerate it. The new suggestion will use the updated variable name.

Copilot Does Not Suggest Any Pulumi Code

This usually means the file language is not recognized or the Pulumi SDK is not imported. Check that the file extension is correct for your language. Also ensure the Pulumi NuGet package or npm module is installed in your project. Copilot only suggests code when it detects the SDK import.

Copilot Pro vs GitHub Copilot Free: Differences for Pulumi

Item Copilot Pro GitHub Copilot Free
Context window size Up to 4,000 tokens of file context Up to 2,000 tokens of file context
Multi-file awareness Suggests across open files in the workspace Only the current file and adjacent files
Inline chat support Available in VS Code, JetBrains, and Neovim Limited to VS Code
Custom instructions Supports organization-level policies No custom instruction support
Pulumi suggestion quality Better cross-resource references due to larger context More isolated suggestions, often missing references

For large Pulumi projects with many interdependent resources, Copilot Pro provides more accurate suggestions because it can see the entire file and related files. Copilot Free works well for small single-file stacks.

To get the most out of Copilot for Pulumi, always start with a clear comment that names the exact resource type and cloud provider. Add sample property values in the lines above the resource block. Define output variables in advance. After Copilot generates a block, verify each property against the official Pulumi schema. Use Copilot Pro for projects with more than five resources to benefit from the larger context window. Test your generated code with pulumi preview before deploying.

ADVERTISEMENT