Lambdas and API Gateways are not the only type of AWS resource we can deploy to AWS cloud using CDK. In fact - nearly every resource that is deployable (is that a word?) with CloudFormation, can also be deployed with AWS CDK.
One of the most popular services on AWS is Amazon S3, which stands for Simple Storage Service.
As the name suggests - it's nothing but simple (in my humble opinion), luckily creating a new S3 bucket and deploying it to the cloud using CDK is relatively straightforward.
Files on S3 are stored in things called "buckets", which are unlimited in size and each bucket has a globally unique name.
In this quick lesson we're going to create and deploy a brand new S3 bucket to AWS with CDK
Instructor: [0:00] Of course, Lambdas and API gateways are not the only type of AWS resource that we can deploy to AWS clouds using CDK. If we want to build a web app, we are going to have static files such as HTML, images, JavaScript, and so on, and we have to host them somewhere.
[0:13] A perfect solution for that in AWS is Amazon S3. Amazon S3 stands for Amazon Simple Storage Service, which is honestly anything but simple. It is an object storage built to store and retrieve any amount of data from anywhere. This is exactly what we need in order to host our static files.
[0:28] S3 is keeping your files in things that are called buckets. We are going to use CDK in order to create and deploy an S3 bucket to AWS. To do that, first open up the terminal and run npm install --save @aws-cdk/aws-s3.
[0:42] Once this is done, let's go ahead and import S3 from '@aws-cdk/aws-s3'. Next up, we are going to create a new bucket. For now, I would like this bucket to store the egghead logo. I'm going to do const logo-bucket = newS3.bucket. Again, we have to pass this as the first argument. I'm going to call it logo-bucket.
[1:01] In order to create a bucket, we don't have to pass any props whatsoever. I'm just going to pass in an empty object.
[1:06] Next, open up the terminal and run cdk diff to see what we are about to deploy. We are going to see that the only difference between our stack and the new stack is the logo-bucket.
[1:15] One fact I remember though, is that S3 buckets have global unique names. What that means is that if anyone is going to create a bucket, which is going to be literally called logo-bucket, you are not able to create a bucket with the exact same name. What is going to happen is that CDK is going to generate a random name for us. Let's go ahead and deploy that, so run cdk deploy.
[1:34] Now that our stack has been successfully deployed, let's go to CloudFormation. Click on todo-app-stack. Next up, click on the Resources, search for S3. We can see our bucket over here. If we click on that, we can see our empty S3 bucket.
[1:45] The name of the bucket is the todo-app-stack. That is the name of our stack. Next, logo-bucket. That is the name of the bucket that we have defined in our code over here, followed by some random characters, because as we mentioned, the S3 bucket names have to be globally unique inside of AWS cloud.