Error "error: Duplicate resource URN" when writing same name file to two S3 buckets

3.3k views Asked by At

I'm trying to create two separate buckets in a stack, but when I try and write a file to the second bucket with the same name as any file in the first I get an error error: Duplicate resource URN 'urn:pulumi:dev::quickstart::aws:s3/bucketObject:BucketObject::index.html'; try giving it a unique name

The code below demonstrates the problem.

Obviously there's no clash as far as AWS is concerned, but is there a different 'name' I should be defining to prevent Pulumi creating what appears to be a duplicate key?

using Pulumi;
using Pulumi.Aws.S3;

class MyStack : Stack
{
    public MyStack()
    {
        // Create an AWS resource (S3 Bucket)
        var bucket1 = new Bucket("my-bucket");

        var bucketObject1 = new BucketObject("index.html", new BucketObjectArgs {
            Bucket = bucket1.BucketName,
            Content = "HTML in 1"
        });

        var bucket2 = new Bucket("my-bucket2");

        var bucketObject2 = new BucketObject("index.html", new BucketObjectArgs {
            Bucket = bucket2.BucketName,
            Content = "HTML in 2"
        });
    }
}
1

There are 1 answers

2
Mikhail Shilkov On

You should give unique logical names to all resources, including two BucketObject's. Logical names are used by Pulumi to identify resources in a stack.

You can still give the same physical name to both files:

var bucketObject1 = new BucketObject("b1-index.html", new BucketObjectArgs {
    Key = "index.html",
    Bucket = bucket1.BucketName,
    Content = "HTML in 1"
});

var bucketObject2 = new BucketObject("b2-index.html", new BucketObjectArgs {
    Key = "index.html",
    Bucket = bucket2.BucketName,
    Content = "HTML in 2"
});