1 00:00:00,000 --> 00:00:01,556 ‫The first thing I have to do is to go to 2 00:00:01,556 --> 00:00:06,112 ‫commands and I will basically create an s3 bucket. 3 00:00:06,112 --> 00:00:10,374 ‫For this we know the command line now is aws s3 make bucket 4 00:00:10,374 --> 00:00:14,197 ‫and I will just say stephane-code-sam. 5 00:00:14,197 --> 00:00:15,592 ‫Should be a unique name, obviously, 6 00:00:15,592 --> 00:00:18,481 ‫you need to change the s3 bucket to a name you have. 7 00:00:18,481 --> 00:00:20,889 ‫I have a terminal right here so I'll just go ahead 8 00:00:20,889 --> 00:00:25,070 ‫and paste this aws s3 make bucket and now we have made 9 00:00:25,070 --> 00:00:27,741 ‫my bucket stephane-code-sam. 10 00:00:27,741 --> 00:00:32,089 ‫If I go to my s3 and refresh now we see there is 11 00:00:32,089 --> 00:00:34,820 ‫stephane-code-sam as a bucket that was created 12 00:00:34,820 --> 00:00:36,245 ‫and now it's empty. 13 00:00:36,245 --> 00:00:38,404 ‫So we need to start uploading code and so 14 00:00:38,404 --> 00:00:39,498 ‫the first thing you want to do is 15 00:00:39,498 --> 00:00:42,456 ‫package our cloudformation and so 16 00:00:42,456 --> 00:00:46,026 ‫for this we will do aws cloudformation package 17 00:00:46,026 --> 00:00:48,905 ‫and we will have to specify the right parameters. 18 00:00:48,905 --> 00:00:52,080 ‫So let's do this function, cloudformation package, 19 00:00:52,080 --> 00:00:55,709 ‫press enter and we'll do help to get some information 20 00:00:55,709 --> 00:00:57,954 ‫around what we should be doing. 21 00:00:57,954 --> 00:01:01,947 ‫If you scroll down, we see that we have to specify 22 00:01:01,947 --> 00:01:04,478 ‫the s3 bucket we're in so we'll say 23 00:01:04,478 --> 00:01:07,561 ‫s3 bucket is stephane-code-sam, okay. 24 00:01:09,041 --> 00:01:12,595 ‫Then you need to specify the template file we have. 25 00:01:12,595 --> 00:01:17,535 ‫The template file we have is going to be template.yaml . 26 00:01:17,535 --> 00:01:19,543 ‫Then the output template file, 27 00:01:19,543 --> 00:01:21,048 ‫because we generate something, 28 00:01:21,048 --> 00:01:23,625 ‫is going to go under a gen directory. 29 00:01:23,625 --> 00:01:25,256 ‫I'm just going to have a gen directory 30 00:01:25,256 --> 00:01:28,305 ‫because we generate code, so I'll say gen 31 00:01:28,305 --> 00:01:31,972 ‫and here I will say template-generated.yaml. 32 00:01:34,408 --> 00:01:38,795 ‫This is going to be out transformed cloudformation template. 33 00:01:38,795 --> 00:01:41,265 ‫This looks about right, it looks like I have everything 34 00:01:41,265 --> 00:01:42,720 ‫I need for this function. 35 00:01:42,720 --> 00:01:47,664 ‫Let me go ahead and quit this and run our command line. 36 00:01:47,664 --> 00:01:51,151 ‫Press enter and it's successfully completed. 37 00:01:51,151 --> 00:01:55,568 ‫What it did, is that it actually uploaded code to s3. 38 00:01:57,158 --> 00:01:59,337 ‫It says successfully packaged artifacts and 39 00:01:59,337 --> 00:02:01,638 ‫wrote output template to the file 40 00:02:01,638 --> 00:02:04,169 ‫gen/template-generated file. 41 00:02:04,169 --> 00:02:06,555 ‫It says I need to execute the following command 42 00:02:06,555 --> 00:02:08,014 ‫to now deploy my code. 43 00:02:08,014 --> 00:02:12,931 ‫By the way, we could have run sam and package, the command. 44 00:02:14,108 --> 00:02:15,898 ‫It would have been exactly the same. 45 00:02:15,898 --> 00:02:19,299 ‫If sam is just a shorthand for aws cloudformation 46 00:02:19,299 --> 00:02:21,307 ‫when you use the package commands. 47 00:02:21,307 --> 00:02:23,816 ‫Okay, so we have the first thing done. 48 00:02:23,816 --> 00:02:26,810 ‫Let's look at this template-generated file just so 49 00:02:26,810 --> 00:02:28,529 ‫we can have a good look at it. 50 00:02:28,529 --> 00:02:32,279 ‫It looks just the same except now the CodeUri 51 00:02:33,774 --> 00:02:35,862 ‫is now referencing s3. 52 00:02:35,862 --> 00:02:38,747 ‫It turns out that my code was uploaded into s3 and 53 00:02:38,747 --> 00:02:43,568 ‫this CodeUri right here was automatically updated 54 00:02:43,568 --> 00:02:45,648 ‫to reference that s3 bucket and 55 00:02:45,648 --> 00:02:47,139 ‫that file that was updated. 56 00:02:47,139 --> 00:02:49,222 ‫This was the little nice thing done 57 00:02:49,222 --> 00:02:51,141 ‫by cloudformation package. 58 00:02:51,141 --> 00:02:52,550 ‫Obviously, you can do way more 59 00:02:52,550 --> 00:02:54,625 ‫but this is just a little taster. 60 00:02:54,625 --> 00:02:57,119 ‫Now let's go ahead and deploy and before that maybe 61 00:02:57,119 --> 00:02:58,834 ‫we should check in our bucket. 62 00:02:58,834 --> 00:03:01,643 ‫Indeed, yes, a file was uploaded. 63 00:03:01,643 --> 00:03:04,446 ‫Now we are going to deploy our template. 64 00:03:04,446 --> 00:03:07,511 ‫It says you should run this command, 65 00:03:07,511 --> 00:03:11,748 ‫aws cloudformation deploy, and then the template file is, 66 00:03:11,748 --> 00:03:13,125 ‫and you'll need the whole path. 67 00:03:13,125 --> 00:03:16,773 ‫I can just use gen/template-generated and the stack name 68 00:03:16,773 --> 00:03:19,356 ‫is going to be hello-world-sam. 69 00:03:23,672 --> 00:03:25,318 ‫That sounds about right. 70 00:03:25,318 --> 00:03:29,064 ‫I'm going to go ahead and run this command. 71 00:03:29,064 --> 00:03:32,109 ‫This will, basically, create a changeset. 72 00:03:32,109 --> 00:03:34,350 ‫It's waiting right now for the changeset to be created. 73 00:03:34,350 --> 00:03:38,724 ‫When it's ready it's going to do the changesets. 74 00:03:38,724 --> 00:03:43,175 ‫Here it says the changeset's generation has failed. 75 00:03:43,175 --> 00:03:46,709 ‫The reason is that it requires a CAPABILITY_IAM. 76 00:03:46,709 --> 00:03:48,253 ‫This is the annoying thing when you 77 00:03:48,253 --> 00:03:49,959 ‫do run this package command. 78 00:03:49,959 --> 00:03:52,277 ‫It doesn't necessarily give you the right answer 79 00:03:52,277 --> 00:03:53,429 ‫and the right commands. 80 00:03:53,429 --> 00:03:57,922 ‫You actually need to add a capability, flagged right here. 81 00:03:57,922 --> 00:04:01,038 ‫Here I am going to add capabilities and then I will 82 00:04:01,038 --> 00:04:03,979 ‫just copy this CAPABILITY_IAM because we are 83 00:04:03,979 --> 00:04:06,287 ‫generating an IAM role. 84 00:04:06,287 --> 00:04:08,844 ‫I will run this command again and now it's waiting 85 00:04:08,844 --> 00:04:10,500 ‫for the changeset to be created. 86 00:04:10,500 --> 00:04:13,758 ‫This time everything should be created just fine. 87 00:04:13,758 --> 00:04:16,946 ‫Now the stack is in create/update state. 88 00:04:16,946 --> 00:04:19,697 ‫Let's go to our cloudformation. 89 00:04:19,697 --> 00:04:24,364 ‫Let's go here and we can go to services, cloudformation, 90 00:04:27,355 --> 00:04:28,917 ‫and let's make sure I'm in the right region. 91 00:04:28,917 --> 00:04:30,149 ‫I'm in the Paris region. 92 00:04:30,149 --> 00:04:32,564 ‫This is where I configured my CLI to work. 93 00:04:32,564 --> 00:04:35,259 ‫Here, as we can see, the hello-world-sam 94 00:04:35,259 --> 00:04:37,630 ‫is in create in progress. 95 00:04:37,630 --> 00:04:39,831 ‫Automatically this cloudformation stack 96 00:04:39,831 --> 00:04:41,776 ‫was being created for me. 97 00:04:41,776 --> 00:04:44,992 ‫Now I just need to wait for the create to happen. 98 00:04:44,992 --> 00:04:48,227 ‫The create function has completed, as we can see. 99 00:04:48,227 --> 00:04:51,226 ‫If you look at the resources that were created, 100 00:04:51,226 --> 00:04:54,705 ‫as we can see, my helloworldpython Lambda function 101 00:04:54,705 --> 00:04:56,415 ‫was created, but also a 102 00:04:56,415 --> 00:05:00,134 ‫helloworldpython3 IAM role was created. 103 00:05:00,134 --> 00:05:02,564 ‫We had both a Lambda function and an IAM role 104 00:05:02,564 --> 00:05:03,854 ‫that was created for us. 105 00:05:03,854 --> 00:05:05,125 ‫We could look at the events. 106 00:05:05,125 --> 00:05:06,538 ‫I don't know where I'm getting this error. 107 00:05:06,538 --> 00:05:09,313 ‫We could look at the events to see what happened over time. 108 00:05:09,313 --> 00:05:11,230 ‫Now if we go to Lambda. 109 00:05:12,071 --> 00:05:14,904 ‫We'll just type Lambda right here. 110 00:05:16,260 --> 00:05:18,245 ‫We should be able to see our function 111 00:05:18,245 --> 00:05:20,603 ‫that was deployed here, this hello-world-sam 112 00:05:20,603 --> 00:05:21,825 ‫and it is managed by sam 113 00:05:21,825 --> 00:05:24,385 ‫and we get a generated name by a cloudformation. 114 00:05:24,385 --> 00:05:27,348 ‫If we look at the function code, as we expect, 115 00:05:27,348 --> 00:05:29,911 ‫it is the code we had in our local environment 116 00:05:29,911 --> 00:05:31,816 ‫which is returning as Hello World. 117 00:05:31,816 --> 00:05:34,823 ‫We could, obviously, go ahead and test that function. 118 00:05:34,823 --> 00:05:39,156 ‫Click on create, testevent, and now click on create, 119 00:05:40,805 --> 00:05:42,649 ‫and now click on test and our function 120 00:05:42,649 --> 00:05:44,795 ‫is really invoked and says Hello World. 121 00:05:44,795 --> 00:05:47,124 ‫This was very basic but overall, 122 00:05:47,124 --> 00:05:51,234 ‫we've just shown how we could write code on our computer. 123 00:05:51,234 --> 00:05:53,404 ‫How with two command lines only, 124 00:05:53,404 --> 00:05:55,698 ‫upload this directly to cloudformation, 125 00:05:55,698 --> 00:05:58,387 ‫and cloudformation to create the Lambda function for us. 126 00:05:58,387 --> 00:06:00,729 ‫Obviously, sam is way more powerful, so the next lecture 127 00:06:00,729 --> 00:06:02,693 ‫we're going to see how we can update this app 128 00:06:02,693 --> 00:06:03,908 ‫to do more things for us. 129 00:06:03,908 --> 00:06:05,273 ‫See you in the next lecture.