1 00:00:00,398 --> 00:00:03,038 ‫So to initialize our project, we could run 2 00:00:03,038 --> 00:00:06,349 ‫sam init, and I'll just have the help function here. 3 00:00:06,349 --> 00:00:10,200 ‫And sam init basically generates a SAM project for you 4 00:00:10,200 --> 00:00:13,817 ‫and you can specify the run time you want being python 3.6, 5 00:00:13,817 --> 00:00:17,217 ‫but you could also say 2.7, node js, and so on. 6 00:00:17,217 --> 00:00:19,233 ‫Sam init creates a lot of files for you 7 00:00:19,233 --> 00:00:22,305 ‫and I don't want to overwhelm you, so how about we just go 8 00:00:22,305 --> 00:00:24,785 ‫one by one, we don't use sam init, we're just going 9 00:00:24,785 --> 00:00:27,294 ‫to create the code one file at a time 10 00:00:27,294 --> 00:00:29,883 ‫so it makes a lot of sense for us. 11 00:00:29,883 --> 00:00:32,414 ‫So just to make things easy, I'm going to have 12 00:00:32,414 --> 00:00:35,723 ‫a source folder and the source folder will contain 13 00:00:35,723 --> 00:00:37,785 ‫our application so I'll call it 14 00:00:37,785 --> 00:00:40,652 ‫a new file and I'll make a app.py file, 15 00:00:40,652 --> 00:00:42,745 ‫I'll just write my application in Python. 16 00:00:42,745 --> 00:00:44,569 ‫That's going to be our source folder. 17 00:00:44,569 --> 00:00:47,094 ‫We're also going to need to have a template file 18 00:00:47,094 --> 00:00:51,465 ‫so we'll have template.yaml and this is going to be 19 00:00:51,465 --> 00:00:55,163 ‫our SAM file so SAM file right here. 20 00:00:55,163 --> 00:00:58,155 ‫And finally we're going to have to write 21 00:00:58,155 --> 00:01:00,745 ‫our commands somewhere so I'll just say them, 22 00:01:00,745 --> 00:01:03,734 ‫I'll have a commands.sh file. 23 00:01:03,734 --> 00:01:06,323 ‫So now we need to go ahead and fill the app.py, 24 00:01:06,323 --> 00:01:09,014 ‫sample.yaml...uh, template.yaml, and so on. 25 00:01:09,014 --> 00:01:11,625 ‫So this, let's just go straight to 26 00:01:11,625 --> 00:01:13,915 ‫the server application model example apps 27 00:01:13,915 --> 00:01:15,403 ‫because that will be the easiest way 28 00:01:15,403 --> 00:01:17,814 ‫for us to make sense of everything. 29 00:01:17,814 --> 00:01:20,665 ‫And let's find a hello world python three. 30 00:01:20,665 --> 00:01:23,894 ‫That sounds great, and here we have a lambda_function.py 31 00:01:23,894 --> 00:01:28,894 ‫and a template.yaml, so let's open these two in two tabs 32 00:01:29,083 --> 00:01:32,812 ‫and let's first look at the lambda_function.py. 33 00:01:32,812 --> 00:01:35,129 ‫So this is a very easy lambda function, imports 34 00:01:35,129 --> 00:01:38,105 ‫to json package and returns some events. 35 00:01:38,105 --> 00:01:40,825 ‫So let's just go and import that function 36 00:01:40,825 --> 00:01:44,515 ‫and I'll just have this into my source, app.py. 37 00:01:44,515 --> 00:01:46,233 ‫So here this is the basic lambda function 38 00:01:46,233 --> 00:01:48,995 ‫that we have from before and I'm actually going to remove 39 00:01:48,995 --> 00:01:51,994 ‫a lot of stuff as well from here and I'm just going 40 00:01:51,994 --> 00:01:56,077 ‫to say return "Hello World," and that's about it. 41 00:01:57,065 --> 00:01:59,864 ‫Okay, a very simple lambda function. 42 00:01:59,864 --> 00:02:01,686 ‫Now, let's do the more fun stuff, 43 00:02:01,686 --> 00:02:05,766 ‫which is to take care of this template.yaml file. 44 00:02:05,766 --> 00:02:09,235 ‫So template.yaml file, let's go back and here it is, 45 00:02:09,235 --> 00:02:11,715 ‫it is going to be the yaml file that will 46 00:02:11,715 --> 00:02:14,297 ‫describe how your function should be. 47 00:02:14,297 --> 00:02:16,275 ‫So let me copy this entire thing 48 00:02:16,275 --> 00:02:19,029 ‫and we'll analyze the lines one by one. 49 00:02:19,029 --> 00:02:22,073 ‫I'll copy the yaml file right here and here we go. 50 00:02:22,073 --> 00:02:23,578 ‫The first thing, the first line 51 00:02:23,578 --> 00:02:25,817 ‫is the AWStemplateformatversion. 52 00:02:25,817 --> 00:02:27,935 ‫It says 2010, so this is just indicating 53 00:02:27,935 --> 00:02:30,126 ‫this is a cut formation template. 54 00:02:30,126 --> 00:02:32,937 ‫The second thing is a transform, and a transform 55 00:02:32,937 --> 00:02:37,246 ‫is indicative that we are using a sam template, 56 00:02:37,246 --> 00:02:41,897 ‫because the transform says "AWS Serverless 2016-10-31." 57 00:02:41,897 --> 00:02:44,508 ‫So this is what you would see at the exam. 58 00:02:44,508 --> 00:02:45,518 ‫Any time you would see a transform 59 00:02:45,518 --> 00:02:48,169 ‫saying this, that means it's a sam template. 60 00:02:48,169 --> 00:02:50,030 ‫Now we can have description, and that's our 61 00:02:50,030 --> 00:02:53,087 ‫starter AWS lambda function, we could have some parameters, 62 00:02:53,087 --> 00:02:54,190 ‫but I'm actually going to remove 63 00:02:54,190 --> 00:02:56,460 ‫these parameters to make it very simple. 64 00:02:56,460 --> 00:02:58,192 ‫And so we have the first resource which 65 00:02:58,192 --> 00:03:01,072 ‫is the helloworldpython3, the type of it 66 00:03:01,072 --> 00:03:05,792 ‫is a serverless function, and this is a sam-specific type 67 00:03:05,792 --> 00:03:08,883 ‫of resource, and we can look at properties. 68 00:03:08,883 --> 00:03:11,043 ‫Properties is that our lambda function 69 00:03:11,043 --> 00:03:13,392 ‫is going to have a handler. 70 00:03:13,392 --> 00:03:14,630 ‫Now the handler should be 71 00:03:14,630 --> 00:03:17,443 ‫the file name dot the function name. 72 00:03:17,443 --> 00:03:21,621 ‫Turns out that our file name is app.py, not lambda_function. 73 00:03:21,621 --> 00:03:23,991 ‫So I'll just change this to app. 74 00:03:23,991 --> 00:03:27,334 ‫And within our app, the py, the first function we define, 75 00:03:27,334 --> 00:03:31,043 ‫is lambda_handler, so I'll keep app.lambda_handler. 76 00:03:31,043 --> 00:03:35,203 ‫The runtime is python3.6, and the code is not 77 00:03:35,203 --> 00:03:37,781 ‫in this directory, it is in the source directory, 78 00:03:37,781 --> 00:03:40,803 ‫so I'll just have source as the CodeUri. 79 00:03:40,803 --> 00:03:44,771 ‫CodeUri indicates locally where the code is. 80 00:03:44,771 --> 00:03:47,875 ‫For me it is in the source directory. 81 00:03:47,875 --> 00:03:51,123 ‫Now the description is that it's our starter AWS function. 82 00:03:51,123 --> 00:03:52,403 ‫We can define the memory size straight 83 00:03:52,403 --> 00:03:55,519 ‫from our parameters, so can we for the timeouts. 84 00:03:55,519 --> 00:03:57,377 ‫And for the policies, I need to remove 85 00:03:57,377 --> 00:03:59,618 ‫this because it is not useful right now. 86 00:03:59,618 --> 00:04:03,026 ‫So here is our most simple file 87 00:04:03,026 --> 00:04:05,948 ‫for sam, it looks pretty simple. 88 00:04:05,948 --> 00:04:08,748 ‫We have a template, an app.py, and then we need 89 00:04:08,748 --> 00:04:12,612 ‫to deploy it, which I will do in the next lecture.