How to import an existing S3 object with Pulumi?

711 views Asked by At

I am trying to import an existing S3 object into Pulumi. This is my current attempt (using Python):

bucket = s3.Bucket('test-bucket',
               bucket='test-bucket')
file = s3.BucketObject('file.txt',
                   bucket=bucket,
                   key='temp/file.txt',
                   opts=ResourceOptions(import_='temp/file.txt'))

With that config, I am getting the following error:

Diagnostics:
  aws:s3:BucketObject (file.txt):
    error: Preview failed: refreshing urn:pulumi:dev::quickstart::aws:s3/bucketObject:BucketObject::file.txt: 1 error occurred:
        * InvalidParameter: 2 validation error(s) found.
    - minimum field size of 1, HeadObjectInput.Bucket.
    - minimum field size of 1, HeadObjectInput.Key.

I could not find any mention of a HeadObjectInput in the reference documentation.

What parameters should I pass to BucketObject() so that the S3 object is imported in Pulumi?

My final goal here is to use Pulumi to delete an existing object from an S3 bucket. Is there any other way for achieving this?

4

There are 4 answers

0
Mitch G On BEST ANSWER

Currently, the Pulumi aws provider is built on the terraform aws provider. So, I tried importing an S3 bucket and S3 bucket object in terraform. Although the bucket imports in terraform (and Pulumi), terraform throws an error:

Error: resource aws_s3_bucket_object doesn't support import

Therefore, the Pulumi provider (at this time) does not support it either.

2
Mitch G On

If you are indeed trying to import an existing bucket and/or object for Pulumi to manage as part of the stack, this page may help: https://www.pulumi.com/docs/guides/adopting/import/

If you just want to get information about an existing bucket and/or object, then there are get functions available: https://www.pulumi.com/docs/reference/pkg/aws/s3/#functions

0
Sam On

According to Pulumi documentation, S3 Bucket import is now supported:

$ pulumi import aws:s3/bucket:Bucket bucket <bucket-name>
0
YJ K. On

Pulumi supports bulk import operation from a JSON file. I have written some code to read EC2 and S3 resources from AWS, and output JSON files that can be utilized for pulumi import -f.

https://github.com/yjiq150/pulumi-aws-resource-scraper