https://pulumi.com logo
#general
Title
# general
d

damp-pillow-67781

06/25/2019, 8:38 PM
Hey I got a question. I need to get the content of a file from S3 in pulumi, the file is a
.json
or a
.yaml
file, but when calling
object.body()
it shows undefined. I wonder is this the proper way to get the content or any other suggestions on how to get that? The code I have now:
Copy code
const latestFile =  aws.s3.getBucketObject({
        bucket: bucket,
        key: `${key}/pod-settings_latest.json`,
    });
    console.log(`latest file: ${latestFile.body}`);
which have the output as
undefined
.
w

white-balloon-205

06/25/2019, 8:52 PM
I believe the
body
property is only populated when the
content-type
is
test/*
or
application/json
. See note at https://pulumi.io/reference/pkg/nodejs/pulumi/aws/s3/#getBucketObject.
d

damp-pillow-67781

06/25/2019, 8:52 PM
I saw that.
so there's no way to get the file other then text and json?
p

proud-alarm-92546

06/25/2019, 8:53 PM
I've had a few cases now where I was tempted to just use the aws sdk for stuff that I need to consume (vs create)
w

white-balloon-205

06/25/2019, 8:53 PM
You could use the
aws
JS SDK directly.
p

proud-alarm-92546

06/25/2019, 8:53 PM
no idea how that would play with plan/apply tho
d

damp-pillow-67781

06/25/2019, 8:53 PM
Hmm, is there an example of how to do that?
w

white-balloon-205

06/25/2019, 8:54 PM
By default, it would run during every plan and apply - which in fact
getBucketObject
will as well.
d

damp-pillow-67781

06/25/2019, 8:54 PM
Basically we just have a bunch of configuration files in yaml format stored in S3, and we need to get these values in pulumi, as an input for other objects.
w

white-balloon-205

06/25/2019, 8:55 PM
The most significant "tricky" think about using the AWS SDK directly is credentials - it will not automatically pick up the same credentials as you used to configure pulumi-aws - so you may need to do some additional work to make sure the SDK is configured with the same credentials.
@damp-pillow-67781 Yes - that sounds like a reasonable use case for using the AWS SDK.
Copy code
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as awssdk from "aws-sdk";

const bucket = new aws.s3.Bucket("my-bucket");
const obj = new aws.s3.BucketObject("o", {
    bucket: bucket,
    key: "foo.json",
    content: '{"foo": "bar"}',
    contentType: "application/json",
})

export const contents = pulumi.all([bucket.id, obj.key]).apply(async ([bucketId, key]) => {
    // const o = await aws.s3.getBucketObject({
    //     bucket: bucketId,
    //     key: key,
    // });
    const s3 = new awssdk.S3();
    const o = await s3.getObject({
        Bucket:  bucketId, 
        Key: key,
    }).promise();
    console.log(JSON.stringify(o));
    return (o.Body as Buffer).toString();
});
d

damp-pillow-67781

06/25/2019, 9:14 PM
Oh cool, looking into that, thanks!
2 Views