1 00:00:00,150 --> 00:00:01,320 ‫So just as a quick reminder 2 00:00:01,320 --> 00:00:03,690 ‫we're going to use the CDK to create an S3 bucket, 3 00:00:03,690 --> 00:00:06,420 ‫a Lambda function that will invoke Amazon Recognition 4 00:00:06,420 --> 00:00:09,510 ‫and DynamoDB tables to save the results of our action 5 00:00:09,510 --> 00:00:11,400 ‫all using the Amazon CDK. 6 00:00:11,400 --> 00:00:13,050 ‫So let's go. 7 00:00:13,050 --> 00:00:15,210 ‫Okay, so let's practice using the CDK. 8 00:00:15,210 --> 00:00:17,220 ‫For this I'm going to use CloudTrail 9 00:00:17,220 --> 00:00:20,850 ‫to be able to run commands against my AWS environment. 10 00:00:20,850 --> 00:00:23,640 ‫So click on CloudTrail and CloudTrail is getting ready. 11 00:00:23,640 --> 00:00:26,130 ‫And in the meantime you can go under CDK 12 00:00:26,130 --> 00:00:28,950 ‫and open the steps.sh to get started 13 00:00:28,950 --> 00:00:30,480 ‫and run some commands. 14 00:00:30,480 --> 00:00:33,720 ‫So first of all, what we have to do is to initialize 15 00:00:33,720 --> 00:00:36,480 ‫and install the CDK on our environments. 16 00:00:36,480 --> 00:00:38,640 ‫So for this, we do a sudo npm install 17 00:00:38,640 --> 00:00:42,450 ‫and we install the aws-cdk-lib. 18 00:00:42,450 --> 00:00:44,373 ‫So let's paste this and press Enter. 19 00:00:46,110 --> 00:00:49,890 ‫And now we have installed the CDK lib on our computer. 20 00:00:49,890 --> 00:00:51,840 ‫So that means that if I type cdk now 21 00:00:51,840 --> 00:00:53,520 ‫we're going to get some commands out of it 22 00:00:53,520 --> 00:00:54,450 ‫as you can see. 23 00:00:54,450 --> 00:00:55,590 ‫Right now we get some error 24 00:00:55,590 --> 00:00:58,050 ‫because we have to specified which CDK command we want, 25 00:00:58,050 --> 00:00:59,760 ‫but it's working. 26 00:00:59,760 --> 00:01:03,090 ‫So next we're going to create a directory called cdk-app 27 00:01:03,090 --> 00:01:06,000 ‫and then we're going to change directory to go in it. 28 00:01:06,000 --> 00:01:08,070 ‫And this is very important to do so. 29 00:01:08,070 --> 00:01:11,370 ‫Next we're going to initialize our CDK application. 30 00:01:11,370 --> 00:01:14,070 ‫So for this the command is cdk init. 31 00:01:14,070 --> 00:01:17,040 ‫Then we specify app and then we specify a language. 32 00:01:17,040 --> 00:01:20,670 ‫This is what we will write the infrastructure in. 33 00:01:20,670 --> 00:01:21,600 ‫So we have JavaScript 34 00:01:21,600 --> 00:01:23,820 ‫but we can also have for example, Python. 35 00:01:23,820 --> 00:01:25,820 ‫But we chose JavaScript in this example. 36 00:01:31,324 --> 00:01:33,300 ‫Okay, now everything is done. 37 00:01:33,300 --> 00:01:36,960 ‫So we're going to verify that it has been successfully 38 00:01:36,960 --> 00:01:41,280 ‫initialized by doing cdk ls, which should return one thing, 39 00:01:41,280 --> 00:01:44,910 ‫it will return the CdkAppStack. 40 00:01:44,910 --> 00:01:46,380 ‫Alright, that's perfect. 41 00:01:46,380 --> 00:01:48,720 ‫So next what we have to do is to actually 42 00:01:48,720 --> 00:01:51,360 ‫copy the content of the cdk-app-stack 43 00:01:51,360 --> 00:01:53,190 ‫into the lib directory. 44 00:01:53,190 --> 00:01:56,880 ‫So if you do ls lib, you'll see there's a cdk-app-stack. 45 00:01:56,880 --> 00:01:59,310 ‫So let's go into this lib directory. 46 00:01:59,310 --> 00:02:02,340 ‫We're going to remove the cdk-app-stack, 47 00:02:02,340 --> 00:02:07,340 ‫and then we touch the cdk-app-stack to create a new file 48 00:02:07,530 --> 00:02:10,470 ‫and we'll use nano to edit this file. 49 00:02:10,470 --> 00:02:12,810 ‫So if you see nano command not found. 50 00:02:12,810 --> 00:02:17,730 ‫Just do sudo yum install nano. 51 00:02:17,730 --> 00:02:19,463 ‫Nano is a text editor. Yes. 52 00:02:19,463 --> 00:02:21,390 ‫Nano is a text editor, which will allow us 53 00:02:21,390 --> 00:02:22,500 ‫to edit some files. 54 00:02:22,500 --> 00:02:24,720 ‫So let's clear this and we'll do again, 55 00:02:24,720 --> 00:02:27,840 ‫nano cdk-app-strack.js. 56 00:02:27,840 --> 00:02:32,400 ‫And now we're going to paste the content of lib, this file. 57 00:02:32,400 --> 00:02:35,220 ‫So let's copy-paste it. 58 00:02:35,220 --> 00:02:37,440 ‫Yes, please paste multiline. 59 00:02:37,440 --> 00:02:38,273 ‫Exit. 60 00:02:39,750 --> 00:02:42,930 ‫And Save, so Control + X and Y. 61 00:02:42,930 --> 00:02:46,590 ‫Okay, just make sure you're good by doing a cat. 62 00:02:46,590 --> 00:02:48,450 ‫And cat allows you to see that, yes, 63 00:02:48,450 --> 00:02:51,930 ‫you have indeed copied all the content into this file. 64 00:02:51,930 --> 00:02:55,410 ‫Okay, so now let's do a little look into what this file is 65 00:02:55,410 --> 00:02:58,170 ‫because this is the core of the CDK. 66 00:02:58,170 --> 00:03:01,020 ‫So at the top, we require the CDK library 67 00:03:01,020 --> 00:03:03,810 ‫and then we require some specific constructs from CDK 68 00:03:03,810 --> 00:03:07,350 ‫such as S3, IAM, Lambda, DynamoDB, 69 00:03:07,350 --> 00:03:09,690 ‫as well as more. 70 00:03:09,690 --> 00:03:12,450 ‫So we scroll down and then we see that thanks to code 71 00:03:12,450 --> 00:03:15,570 ‫we start deploying and defining our infrastructure. 72 00:03:15,570 --> 00:03:18,000 ‫For example, here we define an S3 buckets 73 00:03:18,000 --> 00:03:19,320 ‫and we say this is an S3 bucket, 74 00:03:19,320 --> 00:03:22,260 ‫and by the way, the removal policy is destroy this. 75 00:03:22,260 --> 00:03:24,690 ‫So that means that please destroy this bucket 76 00:03:24,690 --> 00:03:26,580 ‫when the stack is destroyed. 77 00:03:26,580 --> 00:03:29,070 ‫And we have an output that's defined out of it 78 00:03:29,070 --> 00:03:30,720 ‫called CfnOutputs. 79 00:03:30,720 --> 00:03:33,750 ‫And the name is buckets and the value is the bucket name. 80 00:03:33,750 --> 00:03:36,960 ‫And so this is going to be converted into CloudFormation 81 00:03:36,960 --> 00:03:39,330 ‫but this is programmed as code. 82 00:03:39,330 --> 00:03:42,690 ‫This is because the CDK knows what to expect 83 00:03:42,690 --> 00:03:43,523 ‫to create an S3 bucket, 84 00:03:43,523 --> 00:03:46,620 ‫and so therefore you can define different parameters. 85 00:03:46,620 --> 00:03:49,140 ‫And this allows you to be a lot more versatile, 86 00:03:49,140 --> 00:03:52,080 ‫a lot more agile when creating your stacks 87 00:03:52,080 --> 00:03:54,090 ‫because you just write code, you don't write CloudFormation. 88 00:03:54,090 --> 00:03:56,340 ‫So if you know a little bit of JavaScript 89 00:03:56,340 --> 00:03:57,900 ‫or you know a little bit of Python, 90 00:03:57,900 --> 00:04:00,030 ‫you may prefer this method. 91 00:04:00,030 --> 00:04:02,970 ‫So then we go ahead, we create a role for Lambda. 92 00:04:02,970 --> 00:04:05,130 ‫So we call this one role 93 00:04:05,130 --> 00:04:07,050 ‫and then we define how it's working to work. 94 00:04:07,050 --> 00:04:09,900 ‫And then there's a function called addToPolicy 95 00:04:09,900 --> 00:04:12,900 ‫where we can add a policy statement into a role. 96 00:04:12,900 --> 00:04:15,030 ‫So we've done this in two steps, in two lines of code, 97 00:04:15,030 --> 00:04:16,680 ‫one and two. 98 00:04:16,680 --> 00:04:19,890 ‫And this really shows the power of mixing programmation 99 00:04:19,890 --> 00:04:23,850 ‫and then at the end, compiling into CloudFormation. 100 00:04:23,850 --> 00:04:26,820 ‫So we have a DynamicDB table yet again we created, 101 00:04:26,820 --> 00:04:30,270 ‫we defined the partition key, the removal policy 102 00:04:30,270 --> 00:04:32,970 ‫as well as that becomes an output. 103 00:04:32,970 --> 00:04:34,620 ‫Then we have a Lambda function. 104 00:04:34,620 --> 00:04:36,720 ‫We define the runtime, the role, 105 00:04:36,720 --> 00:04:39,510 ‫some environment variables that are linked directly 106 00:04:39,510 --> 00:04:41,880 ‫to the table we have defined from before. 107 00:04:41,880 --> 00:04:44,820 ‫And the buckets we're defining right after, 108 00:04:44,820 --> 00:04:47,040 ‫or right before right here. 109 00:04:47,040 --> 00:04:48,270 ‫So it really shows again the power 110 00:04:48,270 --> 00:04:50,520 ‫of using programming languages. 111 00:04:50,520 --> 00:04:52,740 ‫We can add event sources to say that the Lambda function 112 00:04:52,740 --> 00:04:55,830 ‫is going to be triggered from Amazon S3. 113 00:04:55,830 --> 00:04:58,560 ‫And then we have some very good shorthand 114 00:04:58,560 --> 00:05:01,080 ‫such as we can grant read-write access 115 00:05:01,080 --> 00:05:04,170 ‫to the Lamnda function on the bucket and the table 116 00:05:04,170 --> 00:05:06,900 ‫by just having one line and using this function 117 00:05:06,900 --> 00:05:10,770 ‫instead of figuring out what is the correct IAM policy. 118 00:05:10,770 --> 00:05:14,010 ‫So that's really the power of using the CDK right here. 119 00:05:14,010 --> 00:05:14,910 ‫Okay. 120 00:05:14,910 --> 00:05:17,370 ‫So once we're done, we need to actually set up 121 00:05:17,370 --> 00:05:19,470 ‫the lamb function that will be invoked. 122 00:05:19,470 --> 00:05:21,660 ‫Okay, so to do so from the lib directory, 123 00:05:21,660 --> 00:05:22,710 ‫we go one level up. 124 00:05:22,710 --> 00:05:26,370 ‫So now we're back into our top-level directory. 125 00:05:26,370 --> 00:05:30,600 ‫We're going to create a Lambda directory and go in it. 126 00:05:30,600 --> 00:05:34,740 ‫And then we're going to create a file called index.py in it 127 00:05:34,740 --> 00:05:36,840 ‫by doing touch index.py. 128 00:05:36,840 --> 00:05:40,380 ‫Then we're going to edit it, so nano index.py 129 00:05:40,380 --> 00:05:44,760 ‫and we copy the content of Lambda index.py. 130 00:05:44,760 --> 00:05:47,730 ‫So let's take everything from here and paste it 131 00:05:47,730 --> 00:05:49,023 ‫and then paste it. 132 00:05:50,370 --> 00:05:54,150 ‫And then Control + X, Y Enter. Okay. 133 00:05:54,150 --> 00:05:58,020 ‫So now our index.py file is properly set up as you can see. 134 00:05:58,020 --> 00:05:59,400 ‫This is perfect. 135 00:05:59,400 --> 00:06:00,600 ‫We can have a look at what it does. 136 00:06:00,600 --> 00:06:03,570 ‫So what it does that it detects labels in images 137 00:06:03,570 --> 00:06:05,730 ‫using Amazon Recognition. 138 00:06:05,730 --> 00:06:09,510 ‫So it's going to actually look at the image coming from S3 139 00:06:09,510 --> 00:06:12,090 ‫and then run it through recognition. 140 00:06:12,090 --> 00:06:14,340 ‫And then finally write the data, 141 00:06:14,340 --> 00:06:16,950 ‫the target data into DynamoDB. 142 00:06:16,950 --> 00:06:19,950 ‫Okay, so this function is now created. 143 00:06:19,950 --> 00:06:22,800 ‫Now we need to bootstrap the CDK application, 144 00:06:22,800 --> 00:06:24,270 ‫bootstrap CDK overall. 145 00:06:24,270 --> 00:06:25,230 ‫So this is something you have to do 146 00:06:25,230 --> 00:06:27,243 ‫once per account per region, 147 00:06:28,440 --> 00:06:30,960 ‫and of course you should be running this command 148 00:06:30,960 --> 00:06:33,060 ‫from a directory one level up 149 00:06:33,060 --> 00:06:36,660 ‫because there is a cdk.json file in it 150 00:06:36,660 --> 00:06:38,700 ‫which contains some important information 151 00:06:38,700 --> 00:06:40,590 ‫around how we want to be set up. 152 00:06:40,590 --> 00:06:44,043 ‫Let's go back, we run the CDK bootstrap again. 153 00:06:45,570 --> 00:06:47,853 ‫Paste this. Press Enter. 154 00:06:48,780 --> 00:06:51,870 ‫And this is going to create all the necessary stuff 155 00:06:51,870 --> 00:06:54,580 ‫in your accounts to run the CDK. 156 00:06:54,580 --> 00:06:56,460 ‫So it actually goes into CloudFormation. 157 00:06:56,460 --> 00:06:59,550 ‫So if you open the CloudFormation service, 158 00:06:59,550 --> 00:07:02,943 ‫so CloudFormation in here. 159 00:07:05,760 --> 00:07:08,010 ‫As you see right now, there's a stack being created 160 00:07:08,010 --> 00:07:09,870 ‫called the CDK Toolkit. 161 00:07:09,870 --> 00:07:12,150 ‫And this is what's necessary for CDK to work. 162 00:07:12,150 --> 00:07:14,580 ‫So if you look at resources in here, 163 00:07:14,580 --> 00:07:18,060 ‫you can see that there is SSM parameter, IAM roles, 164 00:07:18,060 --> 00:07:20,790 ‫ECR repository, IAM role, IAM roll, 165 00:07:20,790 --> 00:07:22,470 ‫and then an S3 bucket. 166 00:07:22,470 --> 00:07:25,290 ‫A bit of necessary stuff for the CDK to work. 167 00:07:25,290 --> 00:07:27,810 ‫This is something you have to do once per region 168 00:07:27,810 --> 00:07:28,893 ‫and per account. 169 00:07:30,240 --> 00:07:32,730 ‫Next, we're going to run cdk synth 170 00:07:32,730 --> 00:07:35,220 ‫to get the target CloudFormation template 171 00:07:35,220 --> 00:07:37,560 ‫that is going to be generated out of our stack. 172 00:07:37,560 --> 00:07:39,570 ‫So by running cdk synth, 173 00:07:39,570 --> 00:07:41,370 ‫what's going to happen is that we're actually going to get 174 00:07:41,370 --> 00:07:43,320 ‫a whole CloudFormation template, 175 00:07:43,320 --> 00:07:46,620 ‫as you can see in here, an entirely a big one. 176 00:07:46,620 --> 00:07:48,570 ‫And that contains everything we need. 177 00:07:48,570 --> 00:07:50,550 ‫So it has an S3 bucket 178 00:07:50,550 --> 00:07:53,700 ‫it has S3 bucket notifications being set up. 179 00:07:53,700 --> 00:07:56,490 ‫It also has a Lambda function and so on, 180 00:07:56,490 --> 00:07:58,890 ‫and IAM role and SSM parameters. 181 00:07:58,890 --> 00:08:01,680 ‫So the idea here is that this 182 00:08:01,680 --> 00:08:03,720 ‫is what's going to be deployed to CloudFormation, 183 00:08:03,720 --> 00:08:05,400 ‫and we can preview it, 184 00:08:05,400 --> 00:08:07,440 ‫but we don't need to necessarily do it. 185 00:08:07,440 --> 00:08:10,290 ‫And finally, we need to deploy the CDK stack. 186 00:08:10,290 --> 00:08:12,150 ‫So we'll run CDK deploy. 187 00:08:12,150 --> 00:08:13,740 ‫And this is actually going to generate 188 00:08:13,740 --> 00:08:15,240 ‫this CloudFormation template 189 00:08:15,240 --> 00:08:17,280 ‫and deploy it into CloudFormation. 190 00:08:17,280 --> 00:08:18,990 ‫So you say, yes, here are all the things 191 00:08:18,990 --> 00:08:20,970 ‫that are going to be created. 192 00:08:20,970 --> 00:08:23,310 ‫Everything looks good, let's say yes. 193 00:08:23,310 --> 00:08:24,810 ‫And press Enter. 194 00:08:24,810 --> 00:08:26,460 ‫And now is going to actually deploy 195 00:08:26,460 --> 00:08:28,410 ‫a new CloudFormation template 196 00:08:28,410 --> 00:08:30,090 ‫that is going to be representing our stack. 197 00:08:30,090 --> 00:08:32,220 ‫So if I go in here, 198 00:08:32,220 --> 00:08:36,510 ‫we have the CdkAppStack, actually yes, being created. 199 00:08:36,510 --> 00:08:39,753 ‫And if we have a look at our resources, 200 00:08:41,220 --> 00:08:45,060 ‫there's going to events and resources and refresh. 201 00:08:45,060 --> 00:08:47,220 ‫We can see everything that is being created 202 00:08:47,220 --> 00:08:49,650 ‫such as the IAM role, the S3 bucket, 203 00:08:49,650 --> 00:08:51,930 ‫DynamoDB table, and so on. 204 00:08:51,930 --> 00:08:54,660 ‫So let's wait until everything is created. 205 00:08:54,660 --> 00:08:57,180 ‫Okay, so my stack is fully created 206 00:08:57,180 --> 00:08:58,740 ‫and if we refresh this page, 207 00:08:58,740 --> 00:09:01,860 ‫we see we have 11 resources being created. 208 00:09:01,860 --> 00:09:05,220 ‫So what's important to us is going to be the S3 bucket 209 00:09:05,220 --> 00:09:07,890 ‫and the DynamoDB table. 210 00:09:07,890 --> 00:09:09,660 ‫So let's go into the S3 bucket right here, 211 00:09:09,660 --> 00:09:13,950 ‫open it in a new tab, and then the DynamoDB table as well. 212 00:09:13,950 --> 00:09:16,200 ‫So the idea is that whenever we upload a file 213 00:09:16,200 --> 00:09:19,860 ‫into this S3 bucket, the Lambda function that get deployed 214 00:09:19,860 --> 00:09:22,530 ‫will be run and then we will have some data 215 00:09:22,530 --> 00:09:24,720 ‫in our DynamoDB table. 216 00:09:24,720 --> 00:09:28,230 ‫So for this right now, if you go into explore table items, 217 00:09:28,230 --> 00:09:32,700 ‫we don't have any items, but let's do a quick experiment 218 00:09:32,700 --> 00:09:34,770 ‫and see if everything is working. 219 00:09:34,770 --> 00:09:36,720 ‫So we go into these buckets 220 00:09:36,720 --> 00:09:39,180 ‫and I'm going to upload the file. 221 00:09:39,180 --> 00:09:41,850 ‫Add files, and then underneath my code, 222 00:09:41,850 --> 00:09:45,720 ‫I'm going to choose the cdk, images 223 00:09:45,720 --> 00:09:47,790 ‫and then we'll choose penguins. 224 00:09:47,790 --> 00:09:50,523 ‫So let's upload this file in our buckets. 225 00:09:51,750 --> 00:09:52,950 ‫And we're done. 226 00:09:52,950 --> 00:09:54,540 ‫So this file is right here. 227 00:09:54,540 --> 00:09:55,800 ‫I'm going to open it right here 228 00:09:55,800 --> 00:09:57,600 ‫to show you what it looks like. 229 00:09:57,600 --> 00:10:00,003 ‫Let's go, Preference, Allow. 230 00:10:01,050 --> 00:10:04,080 ‫Okay, so this is just a man with some penguins. 231 00:10:04,080 --> 00:10:06,720 ‫And so this is sent into our S3 buckets. 232 00:10:06,720 --> 00:10:08,250 ‫And then what's going to happen is 233 00:10:08,250 --> 00:10:09,810 ‫that our Lambda function is being run. 234 00:10:09,810 --> 00:10:12,930 ‫It's going to be triggered because S3 sent a notification 235 00:10:12,930 --> 00:10:14,550 ‫to our Lambda function 236 00:10:14,550 --> 00:10:17,040 ‫and then recognition is going to be invoked. 237 00:10:17,040 --> 00:10:19,050 ‫And finally, if I refresh this, 238 00:10:19,050 --> 00:10:22,050 ‫you see that now there's one item return in my table. 239 00:10:22,050 --> 00:10:24,210 ‫So the image is penguins.jpeg 240 00:10:24,210 --> 00:10:25,290 ‫and then we can have a look 241 00:10:25,290 --> 00:10:27,990 ‫at the fact that a penguin, a mobile phone, animal, 242 00:10:27,990 --> 00:10:31,470 ‫bird, person, man, adult, male, glove and shoe 243 00:10:31,470 --> 00:10:34,020 ‫all get detected in my images. 244 00:10:34,020 --> 00:10:35,460 ‫And you can play a bit more. 245 00:10:35,460 --> 00:10:37,470 ‫You can, for example, go back to your buckets 246 00:10:37,470 --> 00:10:39,240 ‫and upload two more files. 247 00:10:39,240 --> 00:10:41,860 ‫So we could upload kid_and_pigeons and swans 248 00:10:43,680 --> 00:10:44,733 ‫and upload them. 249 00:10:46,620 --> 00:10:47,760 ‫Okay, we good? 250 00:10:47,760 --> 00:10:48,750 ‫So they've been uploaded 251 00:10:48,750 --> 00:10:50,580 ‫and very quickly 252 00:10:50,580 --> 00:10:51,780 ‫I go back into the DynamoDB. 253 00:10:51,780 --> 00:10:53,910 ‫And now I see that more of my images 254 00:10:53,910 --> 00:10:55,890 ‫have had item detected in them. 255 00:10:55,890 --> 00:10:58,650 ‫And you can have a look at this table on your own. 256 00:10:58,650 --> 00:10:59,483 ‫So that's pretty cool. 257 00:10:59,483 --> 00:11:02,820 ‫We've seen how to deploy a full stack using the CDK. 258 00:11:02,820 --> 00:11:04,200 ‫Now to clean up everything, 259 00:11:04,200 --> 00:11:07,470 ‫what we have to do is that we have to empty our S3 buckets. 260 00:11:07,470 --> 00:11:09,120 ‫So let's take all these files and yes, 261 00:11:09,120 --> 00:11:12,690 ‫we say yes, I want to permanently delete them. 262 00:11:12,690 --> 00:11:14,700 ‫So that's step one. 263 00:11:14,700 --> 00:11:16,680 ‫And then step two, to clean everything up 264 00:11:16,680 --> 00:11:18,870 ‫we're going to do cdk destroy. 265 00:11:18,870 --> 00:11:22,410 ‫So back into CloudShell, do cdk destroy 266 00:11:22,410 --> 00:11:25,320 ‫and is going to destroy everything for you. 267 00:11:25,320 --> 00:11:27,750 ‫Yes, and you're good to go. 268 00:11:27,750 --> 00:11:29,520 ‫All right, that's it for this lecture. 269 00:11:29,520 --> 00:11:32,583 ‫I hope you liked it, and I will see you in the next lecture.