You've just decided to start building on services hosted for you by your cloud provider. Great! However, you will soon realize, that building applications locally against these APIs can be challenging. Should each developer create a development environment for themselves in the cloud? Will you test against a staging environment? Will you mock them out all the time?
Sadly, each of these solutions has their drawbacks. Wouldn't it be great to run your version of the cloud services you need locally? LocalStack can do that for you.
LocalStack provides an easy-to-use test/mocking framework for developing cloud applications - currently for AWS only.
ProvisionedThroughputExceededException
for DynamoDB,Sounds exciting? Let's see how you can set it up for your machine!
When it comes to running LocalStack, you have some options:
As LocalStack has many dependencies (like pip, npm, java or mvn), I wouldn't recommend installing them locally.
To run the docker image, you will have to clone the LocalStack repository first, then spin up the service with docker-compose
:
# cloning the repositorygit clone git@github.com:localstack/localstack.git# running docker composecd localstackdocker-compose up
Looks a lot cleaner than installing all the dependencies, right? However, if you use Kubernetes, and you'd like this to be the part of the local development workflow without starting it manually, or cloning it, you can take this to the next level with Skaffold. If you are not familiar with that, I'd recommend reading the Using Kubernetes for Local Development article before continue reading this article.
To run LocalStack with Skaffold, we have to create a Deployment and a Service Kubernetes object for it.
Let's start with the deployment first:
apiVersion: apps/v1kind: Deploymentmetadata:name: localstackspec:# using the selector, we will expose the running deployments# this is how Kubernetes knows, that a given service belongs to a deploymentselector:matchLabels:app: localstackreplicas: 1template:metadata:labels:app: localstackspec:containers:- name: localstackimage: localstack/localstack:0.8.6ports:# exposing dynamodb- containerPort: 31001# exposing sqs- containerPort: 31000# expsosing the localstack ui- containerPort: 32000env:# with the SERVICES environment variable, you can tell LocalStack# what services to expose on what port- name: SERVICESvalue: "sqs:31000,dynamodb:31001"- name: PORT_WEB_UIvalue: "32000"
Once we have deployment, we have to expose them using a service:
apiVersion: v1kind: Servicemetadata:name: localstackspec:# selector tells Kubernetes what Deployment this Service# belongs toselector:app: localstackports:- port: 32000protocol: TCPname: uinodePort: 32000- port: 31001protocol: TCPname: dynamodbnodePort: 31001- port: 31000protocol: TCPname: sqsnodePort: 31000type: LoadBalancer
If you'd like, you can put these manifests into a single file, with the ---
used as a separator. Once you have all these in place, the only thing you have to do is to update your skaffold.yml
file so that Skaffold can pick up your new changes.
apiVersion: skaffold/v1alpha2kind: Configbuild:artifacts:- imageName: your-appworkspace: .docker: {}bazel: nulllocal:skipPush: nullgoogleCloudBuild: nullkaniko: nulldeploy:helm: nullkubectl:manifests:- path-to-localstack-manifest.yml- path-to-your-app-manifest.yml