Setting up your local system

Let's create a reliable development environment

The end goal will be to run the Django application in AWS Lambda, and it's best if we simulate that same environment on your computer. One way to create this simulated environment is use Docker. Docker is a tool which creates a virtual container on our local system that is not influenced by the Windows or Mac operating system. This is ideal for us if we want to test our project on the local system before deploying to the AWS cloud.

Not only will Docker give us a Lambda-like environment, but it also allows us to isolate the development environment from the host system. If you come back to the project at a future date, the environment will be preserved just as you left it.

Create a directory for this course#

Let's create a directory for this course. You can create it anywhere you like on your local system:

Install Docker#

Go to Docker to download and install the latest version of Docker Desktop. Once that is completed, you should be able to run docker -v from the command line and get something like this:

The versions and build numbers may differ, but we're just trying to validate that Docker is properly installed. All commands identified here should work on both Windows and Mac, and any differences will be highlighted.

Once you install Docker Desktop, often you will have to start the program manually. If any of these commands reports that docker is not running, check to make sure the Docker Desktop program has started properly.

Pull a Docker image for AWS Lambda#

The folks from lambci have created Docker images that accurately reflect the lambda environment for various programming languages. We'll use that here. Run the following command to download and install the Docker image for Python 3.8.

Once that's done, let's test out our local Lambda environment by creating and running a Lambda-like Docker instance. Run the following command to start the instance:

This command instructs Docker to:

  • create an instance using the image lambci/lambda:build-python3.8

  • map the current working directory $(pwd) to the container's directory /var/task

  • run in interactive mode: -ti

  • clean up the instance once it terminates: --rm

  • and run the bash shell so we can interact with the container via the terminal

Now you can check out an AWS Lambda environment (at least something close to it) on your command line. In addition, we've mapped the current directory on your host system to /var/task on the virtual system. This lets you see the local files on your host system and also access them in the docker instance. The /var/task directory is important because that is where AWS Lambda looks for your code during execution.

Once you're finished looking around, you may terminate the Docker instance by typing exit.

Create custom Docker image#

Now create a new file called Dockerfile in the project directory. Be sure to only capitalize the 'D' in the word. Windows users should ensure there is no file extension on Dockerfile. A Dockerfile instructs Docker to build a custom Docker container. We will use this Docker container throughout this course. Fill the Dockerfile with the following:

This Dockerfile does almost the same thing as the command we just ran above. But it also allows us to customize the environment a little bit. We added a command to instruct Docker to expose port 8000 so we can browse our Django application when it's running inside the Docker instance. Also, we added a custom command line prompt to help us identify when we are running the Docker instance.

We will need to tell Docker to build this Docker image only once. Run the following command:

Make a shortcut#

Now we will create a shortcut script to easily get us into the Docker image. And we will add a few more parameters. This allows us to leverage the AWS credentials we set up in the last lesson. Also, it makes sure the Docker instance can communicate with the host so that we can use a browser when running our Django project locally.

Create a file in the same directory as your Dockerfile called:

  • on Linux, macOS, or Unix

And the contents should be:

and then run: chmod +x to make sure it's executable.

  • zappashell.bat on Windows

And the contents should be:

Now all we have to do is run:

  • ./ on Linux, macOS, or Unix

  • zappashell.bat on Windows

And we are launched into our Docker instance right away! Remember, to leave the Docker instance, just type exit.

When you run this script, the current working directory will be mapped to the Docker instance. For AWS Lambda, all the user's code is mounted onto /var/task which is the main execution directory. Make sure you are in the correct directory when you run the script (for this course, we are using serverless-dz-course).

You'll be able to see the files in both environments. And if you create a file on the host, it will show up in the Docker instance and vice versa. We do this so we can comfortably edit files using our favorite editor and get the benefit of running the AWS Lambda environment locally (or at least get close).

You'll need a code editor#

Finally, you'll need to have a code editor. We will be using Visual Studio Code since it's free and full of features. However, feel free to use any code editor with which you are comfortable.

Start a new discussion. All notification go to the author.