Running AWS Lambda’s Locally w/ VS Code
Overview
As my first medium post, I wanted to list the steps I used to enable the ability to run AWS Python lambda functions locally on my Mac running High Sierra using VS Code as my IDE.
Disclaimer: There may be better (more efficient) ways to accomplish this, but this is the process that made the most sense to me & my team members at work as we set this up in order to standardize our local environments.
Required Setup / Software
· ‘sudo’ or ‘administrator’ access on local machine
· AWS Account (Enabled with MFA)
· AWS CLI (Installed locally)
· Python 3.6
· pip
· virtualenvwrapper
· Visual Studio Code
Assumptions
If you are reading this, then I assume you are familiar with and already have an AWS Account. I also will assume you have the following software already setup:
*Python
*Pip
*VS Code
Install AWS CLI
- Run the following command in your terminal to install the AWS CLI:
- “pip install awscli — — upgrade — — user”
- Verify the install by running “aws — — version”
- Tip: If you get an “‘aws command not found’” error you will have to enter the path to the aws cli in your bash profile. - My path was located at “/users/mark/Library/Python/3.6/bin”
- Configure your ‘.bash_profile’ and add the following line:
- export PATH=/users/mark/Library/Python/3.6/bin:$PATH
- Run “source .bash_profile” after you added the previous line to activate your changes.
Setup AWS CLI / MFA
- To setup your AWS CLI Profile & MFA you’ll need the following information:
* AWS Access Key ID
* AWS Secret Access Key
* AWS MFA arn - The ‘Access Key ID’ & ‘MFA arn’ values can all be found by going to the AWS Console AWS console → IAM → Users → <your user name> →“Security credentials” tab.
The secret access key is something you will have saved from when you setup your access key. If you haven’t setup your access key at this point, you can do that in the console and keep a record of your secret access key. You only get this value once! If you forget it, you can always generate another access key and deactivate the old access key.
AWS CLI Configuration
- Run “aws configure” in your terminal
- Enter your AWS Access Key ID
- Enter your AWS Secret Access Key
- Enter Default Region Name
* I use “us-east-1” as my default - I kept the default output format as “none”
Generate MFA Tokens
- Now that your configuration is setup, you can generate your MFA tokens.
Run the AWS Security Token Service (STS) cli command to generate MFA Session Token:
* “aws sts get-session-token — serial-number <arn-of-the-mfa-device> token-code <code-from-token>”
* Tip: Record these 3 values (AccessKeyID; SecretAccessKey; SessionToken) to reference later when editing your aws credentials file.
* Tip: These credentials are only valid for 12 hours.
AWS Credentials & Profile Setup
[Config]
- In your “.aws” directory there are 2 files you’ll need to edit: “config & credentials”
- Edit the ‘config’ file to setup a profile of the role your account with assume when running commands.
* For my job function, I assume the account role of ‘PowerUser’.
[Credentials]
· Edit the .aws ‘credentials’ file to include your MFA generated access key & secret access key from the “Generate MFA Tokens” step above.
- Every account I use is enabled with MFA, so I created my credentials list as the “default” profile. I can share the keys above because they are temporary 12hr keys & are now expired.
- Tip: In order to simplify and automate the MFA credentials process, I setup a bash script to handle this:
https://medium.com/@bixlerm/aws-mfa-bash-script-f59e2b33093c
[Verification]
- Provided everything was setup properly you should be able to run an aws cli command to verify.
- I run the command “aws ec2 describe-instances — — output table — — region us-east-1 — — profile PowerUser”
VirtualEnv Setup
- Virtualenv’s are setup to isolate your development environments to keep external packages, modules, dependencies, etc.. contained and organized virtually.
- Install VirtualEnvWrapper:
* (https://virtualenvwrapper.readthedocs.io/en/latest/)
* “pip3 install virtualenvwrapper” - Add virtualenvwrapper to .bash_profile path
- My virtualenvwrapper.sh was located in the following directory:
* “/Library/Frameworks/Python.framework/Versions/3.6/bin/” - Each environment may be different depending on how the virtualenvwrapper setup completes, but I had to add the following lines into my .bash_profile for it to setup properly.
- I selected the directory “.virtualenvs” in my home directory to store my virtual environments
- Save your .bash_profile
- Run “source .bash_profile” and you’ll see virtualenv initialize
- You can now setup (make) your first virtual env
* “mkvirtualenv -p python3 myTestEnv” - I specified python3 as my version, otherwise it would have defaulted to 2.7 on my machine. ‘MyTestEnv’ is just the name for this example. You can name your new environment anything you wish.
- In your virtual environment, install the following packages for local development
* Tip: To make sure you’re in your virtualenv, you can see in your terminal the name of the virtualenv in parenthesis.
lambda-local-python {the local lambda package}
* pip install lambda-local-python
https://github.com/willwhy/lambda-local-python
pylint {python linter}
* pip install pylint
boto3 {AWS SDK for Python}
* pip install boto3
Helpful virtualenv commands:
Install & Setup VS Code
- https://code.visualstudio.com/
- For Mac, unzip it and copy to your ‘applications’ folder.
* If you got this far, you probably don’t need any instruction on how to main-stream programs :) - VS code uses ‘Workspaces’ to organize your working directories, files and environment.
* Tip: I setup a 1:1 ration between VSCode Workspaces and VirtualEnv’s.
VS Code Settings
- For creating aws lambda’s, I’m typically building in a git repository for my code, but you can use any local folder. For my example, I’ll continue to use the “MyTestEnv” virtualenv & corresponding directory.
- In VS Code add your test directory to your workspace.
- You can now see that folder structure in the VS Code explorer pane.
- Go to File → Save Workspace As
* Save workspace in an folder you create locally.
* I created mine under home directory called “workspaces” - Go to Code → Preferences → Settings
* This brings up 3 different tab settings
*{User} {Workspace} {Local
[WORKSPACE SETTINGS]
o Add the following block of text in your Workspace Settings, replacing each path to be your own directories which you created from previous steps.
{
“folders”: [
{
“path”: “/Users/mark/Projects/MyTestEnv”
}
],
“settings”:
{
“python.pythonPath”:”/users/mark/.virtualenvs/MyTestEnv/bin/python”, “python.lambdaLocalPythonPath”:”/users/mark/.virtualenvs/MyTestEnv/bin/lambda-local-python”
}
}
- Do a File → ‘Save All’, to save all your environment setting changes.
[Debug Setup]
- In your active workspace directory
* (e.g. /users/mark/workspaces/MyTestEnv) create a directory called “.vscode” if one isn’t created already. - In that newly created ‘.vscode’ directory, create another directory called “events”.
- In the events directory, create a file called “events.json” with the following place holder values:
{
"key3":"value3",
"key2":"value2",
"key1":"value1"
}
- In the ‘.vscode’ directory, create a file called “launch.json”.
* This is the file vscode uses to launch the file of your choice for local debugging - Paste the following lines into the ‘launch.json’ file.
{
"version": "0.2.0",
"configurations": [{
"name": "local-lambda",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${config:python.pythonPath}",
"envFile": "${workspaceRoot}/.env",
"cwd": "${workspaceRoot}",
"program": "${config:python.lambdaLocalPythonPath}",
"env": {
"env": "dev"
},
"args": [
"-f",
"describe_instances.lambda_handler",
"-t",
"-1",
"-m",
"256",
"-r",
"us-west-2",
"-E",
"events.json"
]
}]
}
- The only line in the code above you need to edit is the 2 below ‘args’ “describe_instances.lambda_handler”
* Replace the “describe_instances” to be the name of the file you’re looking to debug locally.
* In my example, I was working on a simple python lambda file called Describe_Instances.py.
Conclusion
- At this point your entire local development environment should be setup for you to program local lambda files.
- You should now be able to simply press ‘F5’ in VS Code to run and debug your lambda functions locally.
- Please let me know of any issues, improvements, comments below!
Updates
- [2/11/19]
— Changed VSCode Screenshots to text for copy/paste abilities
— Added github link to ‘lambda-local-python’. I’ve tried to get this to work with the other local python package ‘python-lambda-local’ but it doesn’t seem to work with the debug feature.