April 19, 2020
This is a follow-up post from Building an Alexa Skill to explain how simple it easy to work on your Lambda function locally instead of in the AWS console.
In my Building an Alexa Skill post, we created a simple Alexa skill together, created our Lambda function, and connected the 2 together. Right now, our Lambda function lives in the AWS console.
Let's start by exporting our function. Click on the 'Actions' dropdown.
Then select 'Download deployment package'.
Once your function has downloaded, feel free to move the directory to a location of your choice and rename. When it's how and where you'd like it to be, change into that directory from the terminal and install the dependencies using either npm or yarn.
Before we go through the simple steps of deploying our function, let's make a small change somewhere so that when we deploy our function soon, you can confirm the difference exists in the console.
The first thing we need to do is create a zip file that we'll deploy. From within your lambda function, you can do this easily by running:
zip -r ../name-of-your-directory.zip
. For me, when I run zip -r ../super-awesome-facts.zip .
I see a really a really quick output of node_modules, the index.js file, package.json, and package-lock.json files and folders being added.There's no errors or warnings so I'm assuming everything worked out. To be sure, there's 2 things I can do:
- run
ls ..
to make sure there's a zip file called 'super-awesome-facts.zip', and or - run
unzip -l ../super-awesome-facts.zip
If you run that second command with the name of your zip file, you should see something like the following which signifies the zip file was created successfully:
The only thing that is left to do is deploy our function. In order for this to be possible you need to make sure you have the aws-cli installed on your machine and have your credentials set up locally. If you don't know how to do that, I recommend reading Setting Up Your AWS Credentials first and then coming back to wrap this up when you're done.
The command to deploy our lambda function is a pretty lengthy command that looks something like this:
aws lambda update-function-code --function-name serverlessrepo-super-awes-alexaskillskitnodejsfact-6YT0UPG98S3 --zip-file fileb://../super-awesome-facts.zip
The only parts you'll need to change are the name of the zip file (unless you followed me step-by-step and gave your zip file the same name as mine) and the function name. You'll find the name of your lambda function on the screen where all your functions are listed.
When you run the command, you should see something like this:
If you see one of the following errors, it means:
- You don't have your AWS credentials set up on your machine.
2. You don't have your default region set on your machine.
If you're not sure how to resolve either of these issues, refer to Setting Up Your AWS Credentials for help.
Finally, all we need to do now is to return to our Lambda function in AWS and see if that change we made locally is there. You should see the following modal when you return to your function which makes it pretty obvious the deployment was successful.
Click on 'Use remote' and then look for whatever change you made.
And it's as simple as that! From now on, you can work locally and then simply create a zip file and deploy it! This is particularly handy for projects over 10MB. Once your project exceeds that size, you'll lose the ability to edit inline in the AWS console.
...