- Okay, so now I wanna run through
an example of a simple module,
just to kinda give you the idea of
how you can create a module,
how you can use a module on your own.
Modules can be very very simple.
Sometimes they're only a few lines,
sometimes they're very very complicated
with tons and tons of sub-modules.
This one is relatively simple.
This is one that I have actually published
on the public terraform repo,
so you're welcomed to go check that out.
Or it'll be in the source code for this class.
It's called terraform-aws-lambda-python-archive
and the purpose of this is to
package up a lambda function into a zip file
as required by lambda.
But one of the drawbacks that you find in,
if you're creating these zip files in the standard way,
is that they will have a different hash each time,
even if the code hasn't changed.
So I created this module to kinda work around that.
But we're just gonna take a look at this
to sorta understand the structure of a module,
how to call a module, and what to look for
when you're creating a module.
So you can see that I've named this directory
using the convention that's required for the repository.
Terraform is always required.
AWS is the provider.
And then the name of the module is lambda-python-archive.
Okay, and you can see our layout, we've got examples.
I've added scripts because this one
calls out to some external scripts
and we're not gonna talk too much
about that in this section,
but we've got some more on that in another lesson.
And then we have our main, our outputs,
our variables, and our README.
Okay, so this is a README.
It gives a explanation of what this does,
and you can read this.
This will be in the module repository as well.
But this gives a little bit of information
about how to use this module, why it was created.
It gives an example of using it.
So this is all just, you don't have to do this.
This is by convention.
But this is for completeness.
I highly encourage you to document
the thinking behind the module, how it works,
what the inputs are, what the outputs are, how to use it.
This is just a cut and paste
from the actual example directory.
So you can see that this is how the module itself is used.
And it's in context a little bit
of how it would actually be used
to deploy a lambda function.
So we can look at the variables
and the outputs constitute the interface for our module.
If you've done any kind of object oriented programming,
you're familiar with the concept of the interface.
So the variables are the inputs to this.
So we take a source directory,
and we give it a description,
which is the path to the root of the Python source.
And we give it an output path.
And this is the path that terraform
is going to create our zip file.
Okay, so we need to know that before we start.
And you can see that there's no default for any of these.
So this means that this is a,
these are both required variables.
So these must be provided.
If they're not provided, it's an error.
And then the output is,
this is the zip file that was created.
It should be the same as the input that we gave it.
And then this is the hash.
Okay, so this is just a Base64 SHA hash of the file.
This is required when you upload the zip file to lambda,
it expects that, so that's why that's there.
And then the main file just simply calls
an external program to create this.
Again, terraform does have methods for creating zip files.
It actually has a zip, it has an archive file resource type.
But we had to do some special magic in this Python
so that we didn't, so that the hash doesn't change.
We basically had to strip out some of the file metadata
and the binary Python files
in order to make those hashes stable.
And then we can just look at the example.
This is an example.
Okay, this goes in the examples file.
And it's simply exercising the code that we wrote.
You can see right here, we're calling this,
we're telling it that the source of this module
is one level above in the file hierarchy.
So it's gonna be the root level of the directory.
And then we're giving it a source directory
and an output directory.
And then we just have some AWS code here for convenience,
so that you can see how this would plug in to AWS.
So we're using this, you can see we use output.
Remember that archive path that was
defined in the outputs?
Well, that becomes an attribute of the module itself.
So we don't have to say outputs or anything like that.
That's just, it's the archive path
of the Python lambda archive module.
Same with the source code hash.
Okay, so those are, if you think of the module
as an object, then now these are properties of that object,
and we can reference them in our interpolations.
So that's kinda how you would use that, we can run a plan.
Actually, before we run the plan, we have to run an inet.
Okay, so now we did it an inet.
And remember, it does the providers,
but it also does modules.
We haven't seen it do this before.
So it actually pulls a copy of the module.
Now, if this was, if this were a module hosted
in the terraform registry or in a private registry,
then it would have pulled the code for the module.
Since it's in the file system, it doesn't put it in here.
It just stores the metadata here.
Okay, so that created our lambda.
And we can see, if we look at our examples directory,
we do create an artifacts.
This is just part of the Python script.
So we're actually creating this lambda.zip.
You can see that we told it to create it here
using that input, okay.
We told it where the source was.
It's our Python directory.
So it basically, it creates an environment
and it puts in these requirements.txt
and then it creates the zip, which is an entire,
all the code required for this lambda function.
And then we used just the regular AWS resources
to upload that to AWS and create a lambda,
create a roll, all the other stuff
that you would need to do for that.