LocalStack: Emulate AWS Services for Local Development & Testing
Tags: aws, beginners, tutorial, devops
Published: 2023-08-24 07:06:19

It can be time-consuming, difficult, and even dangerous to create and test cloud-based apps in a production setting. This is when the significance of regional growth becomes apparent. Because it takes place on the developer's own system, local development helps keep costs down, makes debugging easier, and shortens development times.

---

#### -> Introducing LocalStack

![text](https://miro.medium.com/v2/resize:fit:800/0*8xFlBRolFUuHGx7F.jpg "text")

The best, if not one of the best local development tools is <a href="https://localstack.cloud/" target="_blank">LocalStack</a>, which is based on **AWS** (Amazon Web Services). **LocalStack** creates a fully working local <a href="https://aws.amazon.com/" target="_blank">AWS</a> cloud stack, which can do some offline development and testing of our cloud and Serverless apps. 

Local stack is user-friendly and provides a testing environment on your local system that mimics the APIs and behaviors of **AWS** cloud Services. Now you can create and test your **AWS** applications without spending money or requiring access to the internet.

![text](https://miro.medium.com/v2/resize:fit:640/1*IcrOPkGB-71Q5EcbGD9jqQ.png "text")

## Docker's Importance

Docker is an essential part of this infrastructure. <a href="https://www.docker.com/" target="_blank">Docker</a> is an open-source technology that employs containerized software distribution through virtualization at the operating system level. 

![text](https://miro.medium.com/v2/resize:fit:1400/1*ZP9cyJ4GkKjs6uhpknP5NQ.png "text")

This will not only guarantee that the application will function correctly in any setting because each container is self-contained and includes all necessary software, libraries, and system utilities but also that it is all working locally. **Docker** offers the containerization used by **LocalStack** to simulate the **AWS** cloud on your local workstation.

---

#### Getting started

## Initializing Docker
We need to make sure Docker is functional before we can launch **LocalStack**. Starting up Docker varies from one Operating system to another.

- ***MacOS***: Docker can be launched in the background on **MacOS** by typing `open --background -a Docker`. Docker be launched in the background.

- ***Linux***: Docker is typically deployed on **Linux** as a service. The command `sudo service docker start` can be used to start it.

- ***Windows***: On **Windows**, Docker can be initialized from the Start menu or with a command like `Start-Process -NoNewWindow "C:\Program Files\Docker\Docker\Docker Desktop.exe"` in PowerShell.

It's crucial to verify Docker's continued operation after starting it. The command `docker system info` can be used for this purpose. Information about the Docker system should be returned from this command if Docker is functioning properly.  If Docker is not running, the command will not return any output.

## How to Get LocalStack Up and Running

![text](https://miro.medium.com/v2/resize:fit:1200/1*9lFbRWsRBTyvk1V5RPhW0Q.png "text")

`LocalStack` can be installed and launched once Docker is up and running. But the installation procedure is different for each `OS`.

- Using the `brew install localstack` command, the `Homebrew` package manager may be used to set up LocalStack on **MacOS**.

* Using the `sudo apt-get install localstack` command, LocalStack may be set up on Linux machines with the help of the **APT** package management.

* The `LocalStack` package manager is available for **Windows** and may be installed using the command `choco install localstack`.


Once LocalStack has been installed, you can launch it with the command `localstack start`.

## Using a Shell Script to Fully Automate Everything
To simplify matters, we can write a shell script to **determine the OS**, **launch Docker**, **deploy LocalStack**, and **start** it up. The following script must be run with `chmod +x localstack.sh &&./localstack.sh`:

```bash
#!/bin/bash

# Function to start Docker and ensure it's running on macOS
start_docker_mac() {
    echo "Starting Docker on macOS..."
    open --background -a Docker
    while ! docker system info > /dev/null 2>&1; do sleep 1; done
    echo "Docker is running."
}

# Function to start Docker and ensure it's running on Linux
start_docker_linux() {
    echo "Starting Docker on Linux..."
    sudo service docker start
    while ! docker system info > /dev/null 2>&1; do sleep 1; done
    echo "Docker is running."
}

# Function to start Docker and ensure it's running on Windows
start_docker_windows() {
    echo "Starting Docker on Windows..."
    Start-Process -NoNewWindow "C:\Program Files\Docker\Docker\Docker Desktop.exe"
    while (!(Test-Connection localhost -count 1)) { Start-Sleep -Seconds 1 }
    echo "Docker is running."
}

# Function to install LocalStack on macOS
install_mac() {
    echo "Installing LocalStack on macOS..."
    brew install localstack
}

# Function to install LocalStack on Linux
install_linux() {
    echo "Installing LocalStack on Linux..."
    sudo apt-get install localstack
}

# Function to install LocalStack on Windows
install_windows() {
    echo "Installing LocalStack on Windows..."
    choco install localstack
}

# Detect the operating system
OS="$(uname)"
case $OS in
  'Linux')
    start_docker_linux
    install_linux
    ;;
  'Darwin') 
    start_docker_mac
    install_mac
    ;;
  'WindowsNT')
    OS='Windows'
    start_docker_windows
    install_windows
    ;;
  *) ;;
esac

# Start LocalStack
localstack start
```

This script above defines functions that, 
- **Depending on the operating system**, will either begin running Docker or install LocalStack. 
- It then **determines the operating system** and calls the functions that are relevant to that system. 
- Finally, it **activates the LocalStack**.

Please be aware that running this script requires that you already have Docker and the necessary package manager (`Homebrew` for **MacOS**, `APT` for **Linux**, and `Chocolatey` for **Windows**) installed on your computer. In the event that they are not already installed, you will be required to do so before running this script.

## Conclusion

The combination of LocalStack with Docker makes for a great tool for developing locally on AWS. Emulating the environment of Amazon Web Services (AWS) locally enables developers to improve their productivity, cut expenses, and avoid the dangers that are associated with building software directly in a live cloud environment. 

![text](https://github.com/apotitech/photos_document_md/raw/master/src/common/photos/giphy.gif "text")

You can make it even simpler to get your local AWS cloud environment up and running by automating the process of starting Docker, installing LocalStack, and running LocalStack with the help of a straightforward shell script. This will allow you to save time.