1 00:00:06,640 --> 00:00:08,200 - Okay, so now I wanna run through 2 00:00:08,200 --> 00:00:11,300 an example of a simple module, 3 00:00:11,300 --> 00:00:13,980 just to kinda give you the idea of 4 00:00:13,980 --> 00:00:15,160 how you can create a module, 5 00:00:15,160 --> 00:00:18,060 how you can use a module on your own. 6 00:00:18,060 --> 00:00:19,470 Modules can be very very simple. 7 00:00:19,470 --> 00:00:20,700 Sometimes they're only a few lines, 8 00:00:20,700 --> 00:00:22,370 sometimes they're very very complicated 9 00:00:22,370 --> 00:00:25,810 with tons and tons of sub-modules. 10 00:00:25,810 --> 00:00:29,050 This one is relatively simple. 11 00:00:29,050 --> 00:00:31,560 This is one that I have actually published 12 00:00:31,560 --> 00:00:34,580 on the public terraform repo, 13 00:00:34,580 --> 00:00:36,390 so you're welcomed to go check that out. 14 00:00:36,390 --> 00:00:38,840 Or it'll be in the source code for this class. 15 00:00:38,840 --> 00:00:42,660 It's called terraform-aws-lambda-python-archive 16 00:00:42,660 --> 00:00:44,700 and the purpose of this is to 17 00:00:44,700 --> 00:00:48,940 package up a lambda function into a zip file 18 00:00:48,940 --> 00:00:51,170 as required by lambda. 19 00:00:51,170 --> 00:00:54,090 But one of the drawbacks that you find in, 20 00:00:54,090 --> 00:00:57,140 if you're creating these zip files in the standard way, 21 00:00:57,140 --> 00:01:00,500 is that they will have a different hash each time, 22 00:01:00,500 --> 00:01:02,380 even if the code hasn't changed. 23 00:01:02,380 --> 00:01:05,280 So I created this module to kinda work around that. 24 00:01:05,280 --> 00:01:06,970 But we're just gonna take a look at this 25 00:01:06,970 --> 00:01:10,993 to sorta understand the structure of a module, 26 00:01:11,940 --> 00:01:15,640 how to call a module, and what to look for 27 00:01:15,640 --> 00:01:17,170 when you're creating a module. 28 00:01:17,170 --> 00:01:20,040 So you can see that I've named this directory 29 00:01:21,610 --> 00:01:26,080 using the convention that's required for the repository. 30 00:01:26,080 --> 00:01:28,317 Terraform is always required. 31 00:01:28,317 --> 00:01:29,520 AWS is the provider. 32 00:01:29,520 --> 00:01:33,300 And then the name of the module is lambda-python-archive. 33 00:01:33,300 --> 00:01:36,880 Okay, and you can see our layout, we've got examples. 34 00:01:36,880 --> 00:01:38,961 I've added scripts because this one 35 00:01:38,961 --> 00:01:40,900 calls out to some external scripts 36 00:01:40,900 --> 00:01:42,290 and we're not gonna talk too much 37 00:01:42,290 --> 00:01:43,280 about that in this section, 38 00:01:43,280 --> 00:01:47,140 but we've got some more on that in another lesson. 39 00:01:47,140 --> 00:01:50,200 And then we have our main, our outputs, 40 00:01:50,200 --> 00:01:53,270 our variables, and our README. 41 00:01:53,270 --> 00:01:54,680 Okay, so this is a README. 42 00:01:54,680 --> 00:01:58,980 It gives a explanation of what this does, 43 00:01:58,980 --> 00:02:00,060 and you can read this. 44 00:02:00,060 --> 00:02:03,640 This will be in the module repository as well. 45 00:02:03,640 --> 00:02:06,730 But this gives a little bit of information 46 00:02:06,730 --> 00:02:11,140 about how to use this module, why it was created. 47 00:02:11,140 --> 00:02:13,320 It gives an example of using it. 48 00:02:13,320 --> 00:02:16,410 So this is all just, you don't have to do this. 49 00:02:16,410 --> 00:02:17,370 This is by convention. 50 00:02:17,370 --> 00:02:19,560 But this is for completeness. 51 00:02:19,560 --> 00:02:22,590 I highly encourage you to document 52 00:02:22,590 --> 00:02:24,830 the thinking behind the module, how it works, 53 00:02:24,830 --> 00:02:28,270 what the inputs are, what the outputs are, how to use it. 54 00:02:28,270 --> 00:02:29,610 This is just a cut and paste 55 00:02:29,610 --> 00:02:31,960 from the actual example directory. 56 00:02:31,960 --> 00:02:34,750 So you can see that this is how the module itself is used. 57 00:02:34,750 --> 00:02:36,780 And it's in context a little bit 58 00:02:36,780 --> 00:02:39,150 of how it would actually be used 59 00:02:39,150 --> 00:02:40,950 to deploy a lambda function. 60 00:02:40,950 --> 00:02:42,630 So we can look at the variables 61 00:02:42,630 --> 00:02:45,870 and the outputs constitute the interface for our module. 62 00:02:45,870 --> 00:02:49,150 If you've done any kind of object oriented programming, 63 00:02:49,150 --> 00:02:51,680 you're familiar with the concept of the interface. 64 00:02:51,680 --> 00:02:55,790 So the variables are the inputs to this. 65 00:02:55,790 --> 00:02:58,580 So we take a source directory, 66 00:02:58,580 --> 00:03:00,340 and we give it a description, 67 00:03:00,340 --> 00:03:04,820 which is the path to the root of the Python source. 68 00:03:04,820 --> 00:03:08,010 And we give it an output path. 69 00:03:08,010 --> 00:03:10,270 And this is the path that terraform 70 00:03:10,270 --> 00:03:11,890 is going to create our zip file. 71 00:03:11,890 --> 00:03:14,500 Okay, so we need to know that before we start. 72 00:03:14,500 --> 00:03:16,640 And you can see that there's no default for any of these. 73 00:03:16,640 --> 00:03:19,180 So this means that this is a, 74 00:03:19,180 --> 00:03:20,840 these are both required variables. 75 00:03:20,840 --> 00:03:22,440 So these must be provided. 76 00:03:22,440 --> 00:03:24,380 If they're not provided, it's an error. 77 00:03:24,380 --> 00:03:26,870 And then the output is, 78 00:03:26,870 --> 00:03:29,950 this is the zip file that was created. 79 00:03:29,950 --> 00:03:34,100 It should be the same as the input that we gave it. 80 00:03:34,100 --> 00:03:35,870 And then this is the hash. 81 00:03:35,870 --> 00:03:40,870 Okay, so this is just a Base64 SHA hash of the file. 82 00:03:43,520 --> 00:03:48,330 This is required when you upload the zip file to lambda, 83 00:03:48,330 --> 00:03:51,310 it expects that, so that's why that's there. 84 00:03:51,310 --> 00:03:53,620 And then the main file just simply calls 85 00:03:53,620 --> 00:03:56,310 an external program to create this. 86 00:03:56,310 --> 00:04:00,320 Again, terraform does have methods for creating zip files. 87 00:04:00,320 --> 00:04:03,880 It actually has a zip, it has an archive file resource type. 88 00:04:03,880 --> 00:04:06,140 But we had to do some special magic in this Python 89 00:04:06,140 --> 00:04:08,440 so that we didn't, so that the hash doesn't change. 90 00:04:08,440 --> 00:04:11,610 We basically had to strip out some of the file metadata 91 00:04:11,610 --> 00:04:14,090 and the binary Python files 92 00:04:14,090 --> 00:04:17,910 in order to make those hashes stable. 93 00:04:17,910 --> 00:04:21,392 And then we can just look at the example. 94 00:04:21,392 --> 00:04:23,270 This is an example. 95 00:04:23,270 --> 00:04:25,790 Okay, this goes in the examples file. 96 00:04:25,790 --> 00:04:28,750 And it's simply exercising the code that we wrote. 97 00:04:28,750 --> 00:04:30,870 You can see right here, we're calling this, 98 00:04:30,870 --> 00:04:34,500 we're telling it that the source of this module 99 00:04:34,500 --> 00:04:38,180 is one level above in the file hierarchy. 100 00:04:38,180 --> 00:04:40,500 So it's gonna be the root level of the directory. 101 00:04:40,500 --> 00:04:42,490 And then we're giving it a source directory 102 00:04:42,490 --> 00:04:43,880 and an output directory. 103 00:04:43,880 --> 00:04:48,120 And then we just have some AWS code here for convenience, 104 00:04:48,120 --> 00:04:51,157 so that you can see how this would plug in to AWS. 105 00:04:53,400 --> 00:04:57,250 So we're using this, you can see we use output. 106 00:04:57,250 --> 00:04:59,720 Remember that archive path that was 107 00:04:59,720 --> 00:05:01,950 defined in the outputs? 108 00:05:01,950 --> 00:05:04,830 Well, that becomes an attribute of the module itself. 109 00:05:04,830 --> 00:05:07,310 So we don't have to say outputs or anything like that. 110 00:05:07,310 --> 00:05:10,010 That's just, it's the archive path 111 00:05:10,010 --> 00:05:13,320 of the Python lambda archive module. 112 00:05:13,320 --> 00:05:15,280 Same with the source code hash. 113 00:05:15,280 --> 00:05:17,380 Okay, so those are, if you think of the module 114 00:05:17,380 --> 00:05:21,220 as an object, then now these are properties of that object, 115 00:05:21,220 --> 00:05:23,860 and we can reference them in our interpolations. 116 00:05:23,860 --> 00:05:28,000 So that's kinda how you would use that, we can run a plan. 117 00:05:28,000 --> 00:05:30,670 Actually, before we run the plan, we have to run an inet. 118 00:05:30,670 --> 00:05:32,460 Okay, so now we did it an inet. 119 00:05:32,460 --> 00:05:34,440 And remember, it does the providers, 120 00:05:34,440 --> 00:05:35,790 but it also does modules. 121 00:05:35,790 --> 00:05:37,640 We haven't seen it do this before. 122 00:05:37,640 --> 00:05:40,370 So it actually pulls a copy of the module. 123 00:05:40,370 --> 00:05:43,740 Now, if this was, if this were a module hosted 124 00:05:43,740 --> 00:05:46,340 in the terraform registry or in a private registry, 125 00:05:46,340 --> 00:05:48,240 then it would have pulled the code for the module. 126 00:05:48,240 --> 00:05:50,070 Since it's in the file system, it doesn't put it in here. 127 00:05:50,070 --> 00:05:51,720 It just stores the metadata here. 128 00:06:24,930 --> 00:06:26,820 Okay, so that created our lambda. 129 00:06:26,820 --> 00:06:30,180 And we can see, if we look at our examples directory, 130 00:06:30,180 --> 00:06:32,480 we do create an artifacts. 131 00:06:32,480 --> 00:06:33,950 This is just part of the Python script. 132 00:06:33,950 --> 00:06:37,010 So we're actually creating this lambda.zip. 133 00:06:37,010 --> 00:06:40,630 You can see that we told it to create it here 134 00:06:40,630 --> 00:06:43,320 using that input, okay. 135 00:06:43,320 --> 00:06:44,580 We told it where the source was. 136 00:06:44,580 --> 00:06:45,950 It's our Python directory. 137 00:06:45,950 --> 00:06:50,210 So it basically, it creates an environment 138 00:06:50,210 --> 00:06:54,100 and it puts in these requirements.txt 139 00:06:54,100 --> 00:06:56,500 and then it creates the zip, which is an entire, 140 00:06:57,610 --> 00:07:00,340 all the code required for this lambda function. 141 00:07:00,340 --> 00:07:03,470 And then we used just the regular AWS resources 142 00:07:03,470 --> 00:07:06,790 to upload that to AWS and create a lambda, 143 00:07:06,790 --> 00:07:08,080 create a roll, all the other stuff 144 00:07:08,080 --> 00:07:09,793 that you would need to do for that.