1 00:00:00,160 --> 00:00:01,227 ‫So now, let's talk about SAM, 2 00:00:01,227 --> 00:00:03,550 ‫the Serverless Application Model. 3 00:00:03,550 --> 00:00:05,450 ‫Now, this is really cool because we are going to view 4 00:00:05,450 --> 00:00:08,100 ‫how SAM regroups all the serverless development 5 00:00:08,100 --> 00:00:10,690 ‫we've seen so far into one single tool. 6 00:00:10,690 --> 00:00:12,180 ‫So SAM is this little squirrel, 7 00:00:12,180 --> 00:00:14,750 ‫and it means Serverless Application Model. 8 00:00:14,750 --> 00:00:16,740 ‫It's a framework that is going to be used to develop 9 00:00:16,740 --> 00:00:18,360 ‫and deploy serverless applications, 10 00:00:18,360 --> 00:00:21,790 ‫and all the configuration is going to be done in YAML. 11 00:00:21,790 --> 00:00:24,090 ‫Now, the idea is that from this simple YAML, 12 00:00:24,090 --> 00:00:24,923 ‫you're going to generate 13 00:00:24,923 --> 00:00:27,950 ‫very complex CloudFormation templates using the SAM CLI. 14 00:00:27,950 --> 00:00:30,720 ‫And because this is compatible with CloudFormation, 15 00:00:30,720 --> 00:00:32,170 ‫then any CloudFormation constructs work. 16 00:00:32,170 --> 00:00:34,410 ‫For example, outputs, mappings, parameter, resources, 17 00:00:34,410 --> 00:00:36,700 ‫all of these work within SAM. 18 00:00:36,700 --> 00:00:39,110 ‫Now, it's only two commands to deploy to AWS, 19 00:00:39,110 --> 00:00:41,450 ‫and you have to know them, so I will go over them, 20 00:00:41,450 --> 00:00:43,330 ‫and the exam will test you on them. 21 00:00:43,330 --> 00:00:45,680 ‫And then, we'll see in another lecture, 22 00:00:45,680 --> 00:00:47,850 ‫SAM can use CodeDeploy to deploy Lambda functions 23 00:00:47,850 --> 00:00:49,860 ‫and make sure we have gradual shift of traffic 24 00:00:49,860 --> 00:00:51,820 ‫between our Lambda function versions. 25 00:00:51,820 --> 00:00:53,770 ‫Finally, and we'll see this in this lecture, 26 00:00:53,770 --> 00:00:55,940 ‫SAM can help you run Lambda, 27 00:00:55,940 --> 00:00:57,720 ‫API Gateway, and DynamoDB locally, 28 00:00:57,720 --> 00:00:59,210 ‫so you don't need to deploy to AWS 29 00:00:59,210 --> 00:01:01,420 ‫to start testing your applications. 30 00:01:01,420 --> 00:01:03,490 ‫So, what is a recipe? 31 00:01:03,490 --> 00:01:06,860 ‫Well, SAM has a YAML template, and at the top of it, 32 00:01:06,860 --> 00:01:08,690 ‫you will find this transform header, 33 00:01:08,690 --> 00:01:12,310 ‫AWS Serverless 2016 10 13. 34 00:01:12,310 --> 00:01:14,880 ‫What it means is that this is a SAM template, 35 00:01:14,880 --> 00:01:16,500 ‫and then CloudFormation will be smart enough 36 00:01:16,500 --> 00:01:18,430 ‫to know how to transform this template 37 00:01:18,430 --> 00:01:20,290 ‫into a CloudFormation template. 38 00:01:20,290 --> 00:01:21,510 ‫So we're going to write some code then, 39 00:01:21,510 --> 00:01:24,430 ‫and there's some additional constructs provided 40 00:01:24,430 --> 00:01:26,250 ‫inside of the SAM YAML files. 41 00:01:26,250 --> 00:01:28,580 ‫So we can use an AWS Serverless Function, 42 00:01:28,580 --> 00:01:30,950 ‫which is going to be a Lambda function in the back end, 43 00:01:30,950 --> 00:01:33,190 ‫a Serverless Api, which is going to be API Gateway, 44 00:01:33,190 --> 00:01:35,850 ‫and a Serverless SimpleTable, which is a DynamoDB table. 45 00:01:35,850 --> 00:01:37,590 ‫So these are meant to help you go faster 46 00:01:37,590 --> 00:01:41,210 ‫at creating functions, databases, and API gateways. 47 00:01:41,210 --> 00:01:45,090 ‫And next, to package and deploy SAM, two commands. 48 00:01:45,090 --> 00:01:47,630 ‫So the first one is aws cloudformation package, 49 00:01:47,630 --> 00:01:50,090 ‫or short, sam package, 50 00:01:50,090 --> 00:01:52,610 ‫and then the second one is aws cloudformation deploy, 51 00:01:52,610 --> 00:01:55,440 ‫or the shorthand version is sam deploy. 52 00:01:55,440 --> 00:01:56,550 ‫So this is as simple as that. 53 00:01:56,550 --> 00:01:58,180 ‫The exam will say, how do I deploy? 54 00:01:58,180 --> 00:01:59,390 ‫The first one is sam package, 55 00:01:59,390 --> 00:02:01,610 ‫and the second one is sam deploy. 56 00:02:01,610 --> 00:02:03,880 ‫Let's have a look at these commands in deep dive. 57 00:02:03,880 --> 00:02:06,330 ‫So we have a SAM template, which is written in YAML, 58 00:02:06,330 --> 00:02:07,960 ‫and our application code, 59 00:02:07,960 --> 00:02:10,140 ‫and we're going to do a sam build 60 00:02:10,140 --> 00:02:11,770 ‫to build the application locally, 61 00:02:11,770 --> 00:02:12,730 ‫and it's going to create 62 00:02:12,730 --> 00:02:14,490 ‫a transformed CloudFormation template 63 00:02:14,490 --> 00:02:15,880 ‫and our application code. 64 00:02:15,880 --> 00:02:18,350 ‫And then, we're going to run sam package, 65 00:02:18,350 --> 00:02:20,150 ‫or aws cloudformation package, 66 00:02:20,150 --> 00:02:22,240 ‫to zip and upload our function 67 00:02:22,240 --> 00:02:24,810 ‫and have it uploaded into an S3 bucket. 68 00:02:24,810 --> 00:02:26,300 ‫So both the CloudFormation template 69 00:02:26,300 --> 00:02:29,040 ‫and our application code is going to be uploaded. 70 00:02:29,040 --> 00:02:31,340 ‫And then, using the sam deploy command, 71 00:02:31,340 --> 00:02:33,510 ‫we're going to deploy our application. 72 00:02:33,510 --> 00:02:35,470 ‫From the S3 bucket, it's going to create 73 00:02:35,470 --> 00:02:37,290 ‫and execute a ChangeSet onto CloudFormation 74 00:02:37,290 --> 00:02:38,680 ‫to start deploying our application. 75 00:02:38,680 --> 00:02:41,570 ‫And then, CloudFormation will deploy our Lambda functions, 76 00:02:41,570 --> 00:02:44,060 ‫our API gateways, and DynamoDB. 77 00:02:44,060 --> 00:02:45,770 ‫So very important to see the process. 78 00:02:45,770 --> 00:02:47,450 ‫So SAM template gets transformed 79 00:02:47,450 --> 00:02:49,400 ‫into CloudFormation with sam build, 80 00:02:49,400 --> 00:02:52,750 ‫then packaged into Amazon S3 using the sam package command, 81 00:02:52,750 --> 00:02:54,240 ‫and then deployed onto CloudFormation 82 00:02:54,240 --> 00:02:55,940 ‫using the sam deploy command. 83 00:02:55,940 --> 00:02:58,120 ‫If you really understand this diagram, 84 00:02:58,120 --> 00:03:00,520 ‫you will have most of the SAM questions figured out 85 00:03:00,520 --> 00:03:01,780 ‫at the exam. 86 00:03:01,780 --> 00:03:02,960 ‫Another question you can have is, 87 00:03:02,960 --> 00:03:04,550 ‫how can you run Lambda locally? 88 00:03:04,550 --> 00:03:06,340 ‫Well, to run Lambda functions locally, 89 00:03:06,340 --> 00:03:08,220 ‫you can use the SAM CLI 90 00:03:08,220 --> 00:03:12,560 ‫because the SAM CLI has the power to provide you 91 00:03:12,560 --> 00:03:15,420 ‫with a Lambda-like execution environment locally. 92 00:03:15,420 --> 00:03:16,340 ‫How does it do it? 93 00:03:16,340 --> 00:03:18,750 ‫Well, we use the SAM CLI and AWS Toolkits, 94 00:03:18,750 --> 00:03:20,420 ‫which are going to allow us 95 00:03:20,420 --> 00:03:23,000 ‫to step through our code and debug it. 96 00:03:23,000 --> 00:03:25,750 ‫So instead of deploying a Lambda function on the cloud 97 00:03:25,750 --> 00:03:27,020 ‫and then stepping through it 98 00:03:27,020 --> 00:03:28,410 ‫or trying to find where it failed, 99 00:03:28,410 --> 00:03:30,600 ‫you can do all the development that you're used to do 100 00:03:30,600 --> 00:03:34,389 ‫directly on your local computer without deploying to AWS. 101 00:03:34,389 --> 00:03:37,780 ‫So the SAM CLI has toolkits for various different IDEs. 102 00:03:37,780 --> 00:03:41,040 ‫So we have Cloud9, Visual Studio Code, JetBrains, 103 00:03:41,040 --> 00:03:44,040 ‫PyCharm, IntelliJ, and so on. 104 00:03:44,040 --> 00:03:45,800 ‫And both these things together allow us 105 00:03:45,800 --> 00:03:48,260 ‫to run a Lambda function locally on our computer 106 00:03:48,260 --> 00:03:51,030 ‫and then use our favorite tools to debug locally, 107 00:03:51,030 --> 00:03:55,020 ‫for example, to do a step-through of the code line by line. 108 00:03:55,020 --> 00:03:59,697 ‫And so these toolkits is what is giving SAM the power 109 00:03:59,697 --> 00:04:02,215 ‫to invoke Lambda locally, okay? 110 00:04:02,215 --> 00:04:04,320 ‫So that's it for an intro to SAM. I hope you liked it. 111 00:04:04,320 --> 00:04:06,613 ‫And in the next lecture, we'll have a play with it.