Instead of uploading the files manually through the AWS console, a better solution would be to automatically upload the asset
directory (or any other directory we'd like to upload) whenever we deploy our stack. After all, with CDK we'd like to reduce the manual work to minimum and focus on building an architecture and solving our problems.
In this lesson we're going to learn how to use the aws-s3-deployment
construct in order to upload an assets
directory to an s3 bucket automatically whenever we deploy the CDK stack.
Instructor: [0:00] I've deleted all the files from our S3 bucket because instead of uploading the [inaudible] for the console, we are going to upload whenever is inside of the assets directory. In this case, the egghead logo whenever we deploy our stack to AWS.
[0:12] In order to do that, we are going to use the AWS S3 deployment construct. First, open up the terminal, and run npm install --save @aws-cdk/aws-s3-deployment. I'm going to hit Enter.
[0:23] Next, close the terminal, go over here, and import * as S3-deployment from '@aws-cdk/aws-s3-deployment'. We're going to create a new S3 bucket deployment. I'm going to do New. S3-deployment is going to be a bucket deployment. The first argument is going to be this. As usual, I'm going to call it deploy-logo because I will be using that in order to deploy the egghead logo.
[0:43] Next up the props. In order to create a bucket deployment two props are necessary. The first one is the destination bucket. That is the bucket that we are going to upload the farthest to. The second one is sources. This is an array of sources from which to deploy the contents of this bucket.
[0:58] I'm going to set the destination bucket to our logo bucket. The source is propped to be an array of S3-deployment, source, asset, and I'm going to set it to /assets directory. That's it. Now we can deploy our stack.
[1:12] Open up the terminal and run cdk deploy. Once again, we have some potentially sensitive changes to approve because this bucket deployment is going to be able to modify the contents of our S3 bucket, but this is exactly what we want. I'm going to hit Yes.
[1:24] After successful deployment, let's go back to S3 Console. Hit Refresh and we can see the egghead logo that was deployed automatically for us when we deployed out stack to AWS.