Hi everybody, in this video we are going to learn how to deploy an AWS Lambda function
using Serverless Framework. Now, there are going to be some assumptions before we just get started.
The assumptions for this video are, you already know the concept of serverless and how
theoretically a serverless work environment should work, at least roughly, you should also know what
AWS Lambda is, and in that sense, you already know how AWS Lambda works in a serverless environment.
Basically, you know that Lambda hosts your code, and then with the help of API Gateway you can
associate a web API to your Lambda function. You should also already have NodeJs installed on your
machine so you can go to this URL nodejs.org and follow the download instructions in order
to download node.js onto your computer or you can use your preferred packet manager,
for example right now I'm using a Windows machine and I like chocolatey as my packet manager so I
use that to download node.js. Finally, the most important assumption is you have an AWS account,
can't deploy our AWS Lambda function without already having an AWS account.
So now that we've already covered those assumptions let's talk about serverless
framework serverless framework is a CLI that helps you develop and deploy your AWS Lambda functions
along with the AWS infrastructure that is required serverless framework provides you with IaC that's
infrastructure as code basically this is a way of creating deploying and maintaining your services
or resources on the cloud with machine-readable configuration files serverless framework is a
wrapper on top of AWS cloud formation and unlike AWS called formation the configuration files that
we can write are small and manageable and then the code in serverless framework handles the
rest of the complex changes behind the scene now that we've covered these basics let's get started
so you should have already installed node on your machine so right here if I write
node v you can see back that's my version of node you can have a lower version of node
that's fine at the time of this video the LTS version for node is 16.17.0
All right, so you have node installed let's install the Serverless Framework
so we will write npm install g means that we are installing the Serverless Framework globally
you can check to make sure Serverless Framework is installed properly by writing server lists
v will show back the version that has been installed so we have the framework cores 3.22
our plugin 6.2.2 and our SDK is 4.3.2 okay so we have serverless framework installed now we need
to make sure that our AWS conf credentials are configured to our computer now this is
important not only for serverless framework but also for just interacting with other AWS services
so if you haven't done this already you should go ahead and do it i'm going to show you how to set
it up using a windows machine but you can read the installation instructions for AWS to set up
your AWS credentials so there are directions for linux mac and windows so for a windows environment
you either download and run the msi or you can use the msi exec command to install the AWS CLI
I am now going to go through the default settings of this setup wizard and after we are done
installing the AWS CLI we will then learn how to set up our AWS credentials
now that we've installed the AWS CLI let's just confirm that the installation worked so let's
write AWS --version and you see this is the AWS CLI version and then also python and everything
else that we have on our machine if you are on a windows machine you may not be able to just
immediately do your AWS version here you might have to exit out of your command
line and then go back in so now that we've installed the AWS CLI we need to configure
the CLI to use our AWS credentials so the fastest way to do this is to type in AWS configure
now you're prompted with an AWS access key id, well in order to populate this we need to get
an access key id from our AWS account so it is time to log in to our AWS console so now
i've logged into my AWS console as my root user actually for this account I only have my root user
and that is the problem for best practices you should only use your root user account for a
few account and service management tasks other than that you should lock that root user away
and have different users that can perform administrative functions so in order to do
this we go to our im console and we create an administrative user so what we want to do now is
create a new user that would be an administrator and it would be part of an admin user group so in
the im console we are going to go to our users and we are going to click add user and let create an
admin user so for the username in this scenario um i'm going to call them administrator and this will
be enabled via a password so that this user can sign in to the AWS console as well and so that I
know the user's password because i'm going to use this administrative account i'm going to choose a
custom password and add in the password I know now by default you can require a password user reset
where when the user logs in they have to create a new password on the next sign-in this is going to
be me so I actually don't want this but if you were going to be providing this to someone else
you shouldn't really know the password afterwards so you unchecked it for that security measures
next we need to add our permissions we do not have any groups yet so we have to create a group
let's name this group administrators and we are going to give them the administrator
access level as you can see here there are many types of policies that you can provide so you can
give or give full access partial access when you create a user and you put them in specific groups
make sure to limit it you don't want everybody to be admins maybe there's going to be groups that
only need to access elastics being stock narrow it down that is the best security practice is
to limit access and grant access when necessary don't just give access to everybody in this case
we are making an administrator so we're going to give them the administrative access which gives
full access to the AWS services and resources now the next area is a tag a I am tag is a key value
pair that you can use to add to your user and they can use different information like you can have
your email address or certain descriptions or the job title as they mentioned here
this is optional you do not have to add any but this is useful when you want to track
or control access for the specific user finally we're going to review the user we created the
usernames administrator they have access to the AWS management console with a password
the password type is console and in this case no password reset is required so let's create
this user so now we have successfully created our administrator user and this user can log in
so you will have your AWS account id and then you can go to the url that is going to be displayed
here in this black box and you could sign in as this user so we have created a user now with this
user we want to create an access key so we're back to the user screen and now we're going to go into
our user and on the security credential side we're going to create an access key as it says here is
used to make programmatic calls to the AWS CLI and other tools and the user can have a maximum of two
access keys that are active or inactive at a time if you lose or if you forget your access key you
can't view it again so instead you need to create a new access key and make the old key inactive
I think you should already know this but you should never share your secret keys with anyone
and you should rotate your keys pretty frequently so we're going to create our access key so
here you should see your access key id and your secret access key if you click close without
copying this or downloading the csv file it is gone you will have to make it inactive and make
generate a new access key so right now as it says right here this is the only time the secret access
key can be viewed or downloaded you can't recover it so download it now download the csv file
so now that you've downloaded the file you and you know where it's located you can click close
and we can take that access key id and secret that we have retrieved and populated
in the AWS CLI prompt that we have in our command line so going back to our AWS configure we can add
our aw access key so you can copy it from your csv that you downloaded you are then prompt with
your secret access key which again you can copy from the csv you've downloaded and now
you want to choose your default region name so in my case i'm going to choose us yeast one and
for the default output format I am going to do json so now that we have this all set up we can
make a simple Lambda function and deploy it using serverless framework so what i'm going to do here
is a small demo of an AWS Lambda function that will be deployed using the serverless framework
let's start with using the templates that are provided by serverless so we can type serverless
in our terminal and we are prompted with a choice of what do we want to make you can use the up and
down arrows to select what you want I am going to choose the node js starter example now after
choosing these what type of project I want to make it asks me what do I want to call this project i'm
going to call this serverless demo example and now it is downloading the serverless demo example
and it's adding it to the folder I already have the file structure opened in visual studio code
so the folder is now there now the next question that is being prompted here is if I want to log
in or register this with the serverless dashboard it is not required to have a serverless account
you can have a serverless account if you want in this example not going to have one
so i'm going to choose no and now it asks me if I want to deploy if I want to deploy now if I choose
yes it will deploy to my AWS account right now I am going to choose no because I do not want it to
deploy right now so when we are ready to deploy we will run the command serverless deploy but
we're not there yet we need to walk through the template that was provided to us so first let's
look at the handler.js it's just a simple message that says go server list 3.0 and telling you that
your function executed successfully for this demo we are not going to change this also has a readme
file that is auto-generated and it breaks down how you could deploy this template and where to find
more examples finally there is the yaml file it contains the information regarding our AWS
resources so talks about our provider name the runtime environment that we're doing the
and then and then the function's name in this case the function's name
the function is hello and the handler is handler.hello that goes back to our handler and we
know that it is hello so keep in mind that every Lambda function you create as part of this project
has to be registered here in functions in order for it to deploy successfully so let's test this
function locally so what i'm going to write here in a new terminal is serverless invoke vocal
function and the function name is hello and we get back our result that was defined in our handler
here that status code 200 with the message being go serverless 3.0 there are more commands that you
can do when when calling the serverless invoke for example if you don't include local you're actually
going to invoke the call on your AWS instance after you've deployed it so if we do serverless
invoke and do not include local function
hello it's actually going to fit air because it is looking in our default
AWS regions all those setups that we did in the AWS CLI where we put in our our key and our secret
it's using our keen secret to access our AWS account it looks into our default region which
is us east one and it looks for this function but it is not there because it has not been deployed
you can specify the region so if you include dash dash region then if you were going to
deploy it to a different region you can then invoke from that region you could also specify
another AWS profile because you can have more than one AWS profile we have just one
and that's our default so let's deploy this code now so we can type serverless
deploy and it's going to use the default settings that we had set up with our AWS CLI
so you can see here it's saying that it's deploying our serverless demo example to stage dev
it's creating the cloud formation stack necessary so after it goes through deploying the serverless
demo example and sets up the cloud formation stack the service will be deployed to the stack in this
case serverlessdemo example dev so let's check it out in our AWS console so first we can find
our demo example in cloud formation so go to cloud formation you can see there is our stack name that
we just completed serverless demo example dev this has been just created if you go to your
Lambda console you'll see the functions that you created here is our function serverlessdemo
example hello if there was more more would have been added as well if we go into the console
we can see the code and this is the code that we had before that was deployed here successfully
and we're going to even test it here if we want it to so last but not least let's go back and try to
invoke it again from our terminal not locally this time here caller and vote function hello and there
is our response this is not being run locally so congratulations you have now learned how to deploy
an AWS Lambda function using serverless framework if you liked what you saw please remember to
subscribe to my channel so you can get notified of more AWS tutorials and until then happy learning