Check out a free preview of the full Introducing DevOps for Developers course:
The "Generating Buckets with Pulumi" Lesson is part of the full, Introducing DevOps for Developers course featured in this preview video. Here's what you'd learn in this lesson:

Erik walks through how to generate buckets by creating a DevOps bucket resource. Properly scoping the bucket ensures isolation of resources.

Get Unlimited Access Now

Transcript from the "Generating Buckets with Pulumi" Lesson

>> Cool, okay. So now, we've got this, right? I kinda wanna show you at least what it's gonna look like if we use it, right? So we're gonna say, export, I think that's it. I just feel like I just blanked on TypeScript. Yeah, I believe that's what we need, export.

[00:00:21] And then what we're gonna do is, we're actually going to go to index.ts and we're gonna use it now, okay? So what we wanna do is, we just wanna say FmBucket like this, and then import it. There it is. And then what do we do? Yes, I did, okay, cool.

[00:00:56] And again, because this is a class and we're not working with a functional language [LAUGH] and I have to do new. Okay, so we're gonna say new FmBucket, and then if we do this, look at that Name:example, and then Product:devops-course, right? Now, it's saying that it's expecting two options, and that's because we forgot to make this optional.

[00:01:30] So we're just gonna make that optional, and then there we go. So now, what have we done? We've created a bucket, [LAUGH], yeah. However, we've created a bucket, but now I can do these two as much as I want. And as long as I simply change the name, right, then I will always get the same type of bucket that I expect every time, right?

[00:01:54] So let's do that, let's say devops-course, right? Get rid of this, get rid of this. Let's go back to bucket. I don't know why, that's right, I see, because it wants to do import the star thing. So here's what we're gonna do instead, we're gonna say s3. Pulumi kind of exports their package a little weird.

[00:02:21] So they like to do the star export, where you just deconstruct whatever you need off of it. So I'm just gonna do that basically, so I don't deal with import issues. Okay, like that, there we go. Pulumi, and then we'll go here, and we'll just do the same thing here as well, let's cut these out.

[00:02:46] And we'll say that this is a ComponentResource, and this is a CustomResourceOptions, cool? Cool, okay. So now, what I'm gonna do is, I'm gonna say secrets-- pulumi up. And hope that it doesn't break. Yay, I didn't break it. So what do we got? Well, we got a stack just like we did before cuz we haven't created our stack yet.

[00:03:11] We now have a, look at that, right underneath it, our own FmBucket resource. And look at the naming convention, devops-course-example. So that means that this component is now named devops-course-example. And inside of that, we have an aws:s3:Bucket, which we're just using as example. Again, you can kinda see why I don't do the duplication there because you'll just see devops-course-example, devops-course-example, devops-course-example, over and over, and over again.

[00:03:39] But the only thing we really care about is just the Name:example, right? There's a problem here, though. There's a problem here and it's around how Pulumi group resources. Right now, if you pay attention to this spot here, you're gonna notice that it basically says, okay, pulumi:Stack owns all of these, right?

[00:04:04] But we technically want our bucket and our PublicAccessBlock to be here, underneath this. The reason for that is because if another bucket is created with the exact same name, it will collide, we won't allow it to. But we wanna have that high level of composability. Well, if we wanna maybe give a name the same bucket, but maybe for a different environment, right, that's where that product and thing comes into play, where we have more flexibility around how we name stuff.

[00:04:34] So what we wanna do, is we really wanna wrap all of our custom resources in our own components, right? So to do that, we're gonna go back to our bucket, and we're gonna add something to that ops thing that I told you about before, right? And as I said, Pulumi adds options to the end of every resource, right?

[00:04:54] So here, we're gonna create a new object, just like that. And in here, we're gonna say p, and you'll see parent. Parent tells Pulumi which resource this component lives in, which component this resource lives in, yeah, one of those two. But basically, what it'll do is it'll parent the resource to the component, so that when you go to create it, it'll always be inside of that component.

[00:05:23] So all we have to do is just save this, and that's the beauty of JavaScript, it's scoping it, right? Now, here's a trick, which is if you don't remember to always do it, you will see something like this. Cool, so our bucket is where it needs to be, but our PublicAccessBlock is not.

[00:05:42] This is again something you wanna make sure you look for essentially because if you create it, you have to then deal with state to remove it and readding it or things like that. Again, ideally what we want is we want all of these resources to be under the same main component so that they're very easily move aroundable and stuff.

[00:06:04] So we're just gonna go back here and we're just gonna fix that too, oops. And we're gonna say, parent this. There we go. And so now, if we go back, You guys should be able to do Pulumi up actually without needing credentials. Yeah, that's it, I don't think it actually fails until you try applying.

[00:06:32] So you should be able to do all these commands I'm doing with me, and there we go. Now, we've got much nicer isolation of our resources where we have an FmBucket which is creating a s3:Bucket and s3:PublicAccessBlock. And on top of that, we now have the naming conventions and everything that we want.