A step by step example of how to create an s3 bucket that autodeletes when we run cdk destroy, using Typescript
Transcript
hi
i'm gonna show you how to how to deal
with undeleted
s3 buckets when using a
amazon web services cdk so first of all
to
illustrate this problem let me create a
new
a new stack using cdk
so for this we need to
do a few installs with npm
so that will be the first install
we also will need to install aws
cdk
s3
and with this we can create our stack so
what we're going to do is we're going to
use
we need to do everything in our own
folder so i'm going to create this
folder
my stack all right my stack
and then inside here i'm going to create
using the cdk
like a kind of cloud formation template
which is going to be driven by
typescript so you're going to see in a
second
where the problem is
but basically we're these are our
dependencies that we need to
to create this right if you're familiar
with cdk
like you know that you can use this with
any other language with javascript
python etc
so this is done it's looking good and
then we're gonna go to the editor
and open we can see here the files that
got created
and we can open this my stack stack
i named it my stack but here we have any
other name just as an example
i just here the code that defines your
stack goes here right so this is already
done for us
so basically uh what we're gonna do here
is i'm just gonna
i'm just gonna copy
this code here
[Music]
okay so basically
what i added here is how you normally uh
create a bucket using
cdk right so you say
s3 we need to add s3 also
okay so here we have the library to read
access to s3
thanks to this import and then this is
the id of the bucket
and this is the name of the packet the
package cannot have underscores
so this is not allowed but it can have
like normal dashes and
other things and basically none of these
parameters is necessary right
if you create it like this you could
create an
after generated name but just for the
sake of
showing a real example i'm going to show
it like this this won't be a public
bucket either so basically
what we're going to do is
we're going to create this bucket
normally and then we're gonna see what
happens when we do
destroy so first of all
uh let's run this let's change the name
of this bucket to something
more unique so i'm gonna have here like
um from zero to full stack
my best packet
and the same here
and this should be a unique packet name
um then um basically
so we have defining code what our stack
will be and here there could be lambdas
it could be api gateways
but in this example is just like an s3
bucket right
and we're going to see what's the
default behavior when we deploy this
so for this we're going to have to first
run the ctk
bootstrap this is a one-time
command that sets everything that amazon
web services needs
for this cdk and if you're running
several times you will just keep getting
this message like no changes so there's
no harm in running it several times
and after this we can then run the cdk
deploy
to deploy this infrastructure so um the
cdk will like
create the cloud formation templates and
like uh spin
any like uh servers that we need any
lambdas
any s3s so in our case this will create
this
s3 bucket also this runs by default with
the
aws credentials in the home folder
in the home folder so in this folder
aws so this file
credentials and
this this is where it uh where the
credentials will come from
so if you want if you have like a
default profile you can just run this
and if you have another profile you can
just use
the syntax with a profile flag to use
the in this case the my profile
profile um okay so after
of this let's run the cdk deploy
then we're gonna see this creating the
cloud formation change set and we're
gonna see by the end of this
we should have a new s3 bucket
so let's see if it works it's in
progress
and it looks like it's creating it one
of three tasks
let's see
it's a bit slow but um hopefully it will
get there
and in the meantime exactly now it's
finishing
three or three and it finished
correctly right so uh if we go to the
s3 that i have here the panel
uh basically i'm gonna here are some
like folders that i have for other
things
we can see the cd toolkit that was
uploaded also
and then we can see the new bucket here
so this got correctly
uploaded or created yeah so
this is cool and now we're gonna see
what's the problem right
now we're gonna run uh so this is the
the problem itself
normally we will just run cdk destroy
to remove all the resources all the
lambdas etc but
what happens when we run cdk destroy
let's take a look
first it asks for confirmation so we say
yes
now we should remove all our stack right
so in this case all our stack is just an
s3 bucket
and it says here destroyed everything
went correctly
but let's take a look at the bucket and
let's see if it was
really destroyed
and it was not destroyed right so that
is the problem and that's the problem
that we're gonna solve immediately
so this means that uh
we we need to come here and delete it by
hand so imagine if instead of one bucket
we created 200
like this would be a nightmare plus when
you delete it
and because of security concerns right
you need to type this so it's very
manual
also if there will be files inside the
bucket we need to empty the bucket first
so this is very annoying so we're gonna
find a solution for this
um yeah so let's let's go back to the
code
what we're gonna do is we're gonna
replace this s3 bucket
with a different component um and
that component works as we as we expect
so when we run destroy
it gets destroyed right so to do that
we're gonna need some new imports
and also some new npm packages
so let's start with the npm package that
we need to install
and it's this one this mobile post up to
the lead packet you can see it in the
notes also there's a link to this
but basically we need to install this so
just a second let's see which folder are
we in
inside my stack so i think actually
it should be from the outside because
that's where the node modules
is so here we install it
and now it's installing this and while
this is installing uh we can
okay it finished installing and while
this
this is done so what we need now is to
add this
import also to the code right so
we'll see here where it says
in this imports we need to import this
one and we'll import the out of delete
bucket
right and then the only thing we need to
do is to change this line
i'm just commenting down here instead of
the s3
we just do auto delete bucket
right and everything else can stay the
same so
here i have some options but you can
have like a version through here you can
have like
you have none of these options this
would be valid but the important one is
this auto delete bucket uh
accepts the same options as s3 buckets
so it's a drop-in replacement
um so let's see how this behaves and and
this should solve our problem so we're
gonna deploy this and then we're gonna
destroy it and we're gonna see
if the bucket is still there
so let me take a look okay so let's run
deploy again
ah we need to do it from within the
folder
so the my stack folder and now we can
run deploy again
and they should create a bucket the same
as the first time right
um okay there's a problem here we need
another package
so we need to install one more package
so let me find the package
um
basically is this ts
it's this ts file right so
[Music]
just a second
so it's complaining about the
this model uh lambda
are we including the ah the way it works
is this auto delete bucket
is using the the lambda module right so
this means we also need to install that
okay so no problem we can like require
that
lambda so we can add it back here
and then we can try again and let's try
again
okay and the lambda model is not
installed so we need to install it
um from mpm
okay so no problem we can go and install
the lambda module too
so it's just
we just take this string right
we go to the client folder so we're in
the
in the main uh folder we started with
and then it was like sudo and
install and here we install our module
perfect because this uh auto delete
bucket is using lambda right to
to make sure that this is created etc
so now we go to my stack to my stack
and now it should work let's see
okay still it finished it also needs the
cloud formation so let's add the cloud
formation
too let's add all of them
um
okay so we go back and rpm
and we have the cloud formation
okay and now again we go back with
my stack and deploy
okay now it says if we want to deploy
these changes
we say yes
[Music]
and this should deploy our stack which
means in this case creating the s3
bucket
but using this other library instead
so let's see by the time this is
finished we should have
again a new bucket here
still then created
okay here we say the auto bucket i think
and then this package should take care
of of destroying the bucket for us
okay it's getting there
it's creating some rolls uh lambdas
it doesn't create a lot of stuff just
enough to
here's the lambda that will take care of
removing it
okay and it's getting there and
everything went correctly right
so let's check
and here's our bucket so this worked
well
and now for for the solution so
basically
we're gonna write run uh cdk destroy
same as before
but now the auto delete bucket should
remove the bucket
so are we sure yes for sure
and then we should see the bucket here
this bucket should disappear right and
this is very practical because then you
need to be double checking everything if
you have a lot of buckets with a lot of
things inside it would be
charged still from from amazon so it's a
it's a very useful package so let's just
show that this is the case so it has
destroyed everything went well
same as before but let's see if it
actually deleted it
and it deleted it so now that folder
doesn't exist anymore so this is uh
i think this pack is very very useful
and i thought
this was useful to you