https://pulumi.com logo
Title
b

bland-pharmacist-96854

02/03/2023, 12:48 PM
What is the correct way of doing this in typescript? pseudo-code. What I want is a list of buckets accessible by bucket name, in a way of
buckets.sourceBucket.name
e

echoing-dinner-19531

02/03/2023, 1:02 PM
b

bland-pharmacist-96854

02/03/2023, 1:04 PM
no, as I want it to be accessible in this way `buckets.sourceBucket.name``
e

echoing-dinner-19531

02/03/2023, 1:08 PM
Then what you've got is correct
b

bland-pharmacist-96854

02/03/2023, 1:09 PM
But I'm not using the
bucket
object 😕 , what about if I want to create the buckets using
aws.s3.bucket()
in the for each loop?
in addition, what is called in typescript that
buckets
type? map? dict?
e

echoing-dinner-19531

02/03/2023, 1:19 PM
Ah you need to give TypeScript a few more type hints for that:
type Bucket = {
    name: string;
    bucket?: aws.s3.Bucket;
  };

let buckets : {[key: string]: Bucket } = {
    sourceBucket: { name: "emr-source" },
    outputBucket: { name: "emr-output" },
    emrLogsBucket: { name: "emr-logs" },
    emrStudioBucket: { name: "emr-studio" },
};

for (const k in buckets) {
    buckets[k].bucket = ...
}
b

bland-pharmacist-96854

02/03/2023, 1:21 PM
What about if I want to initialize the source bucket later?
e

echoing-dinner-19531

02/03/2023, 1:21 PM
That's why it's marked
?
b

bland-pharmacist-96854

02/03/2023, 1:22 PM
let buckets: { [key: string]: aws.s3.Bucket } = {
  source:,
  output:,
  emrLogs:,
  emrStudio:,
};
and If I want to do it in this way?
how can I define the objects (source, output, emrLogs, etc..) without initializing them?
e

echoing-dinner-19531

02/03/2023, 1:23 PM
I think:
let buckets: { [key: string]: aws.s3.Bucket? } = {
  source: undefined,
  output: undefined,
  emrLogs: undefined,
  emrStudio: undefined,
};
b

bland-pharmacist-96854

02/03/2023, 1:24 PM
nops
Type 'undefined' is not assignable to type 'Bucket'.ts(2322)
e

echoing-dinner-19531

02/03/2023, 1:25 PM
aws.s3.Bucket?
<- I think
?
works in that location if not then you want
aws.s3.Bucket | undefined
b

bland-pharmacist-96854

02/03/2023, 1:25 PM
yeah that | undefined did the trick
let buckets: { [key: string]: aws.s3.Bucket | undefined } = {
  emrSource: undefined,
  emrOutput: undefined,
  emrLogs: undefined,
  emrStudio: undefined,
};

Object.entries(buckets).forEach(([key, value]) => {
  value = new aws.s3.Bucket(key, { forceDestroy: true });
});
this works, thanks
do you think that is OK this code?
or is there any better way of doing that? (creating a list of buckets)
e

echoing-dinner-19531

02/03/2023, 1:28 PM
I think you need to fix the
value =
That will assign the variable value, while you probably want
buckets[key] =
But otherwise yeh that seems reasonable
b

bland-pharmacist-96854

02/03/2023, 1:36 PM
done
thanks!