AWS Dev Environment setup with LocalStack

Prima Virani
4 min readApr 24, 2019

--

‘Serverless architecture’ is one of the major popular trends in tech at the moment. AWS particularly is the cloud platform of choice for a lot of the recent companies/start-ups to run their infrastructure on. So ‘Serverless architecture’ tends to involve services built a permutation-combination of Lambda, Cloudwatch, S3, IAM, Kinesis, Step Functions and so on.

One of the biggest challenges while trying to build a serverless service is development and testing. This is particularly true when a team is trying to build these services for the first time from the ground up where shipping the service is #1 priority, no CI/CD has been built out for AWS and the team doesn’t have cycles to spare on CI/CD.

In a lot of those cases, development and testing efforts tend to be in the Production environment itself. This is not only a bad practice but it can also be challenging in terms of code reviews and collaboration. To solve the entire challenge would require multiple tools and multiple blog-posts! For now let’s focus on a major challenge — setting up Dev Environment for AWS locally.

LocalStack is a very useful tool for this purpose. LocalStack provides an easy-to-use test/mocking framework for developing Cloud applications. It spins up a testing environment on your local machine that provides the same functionality and APIs as the real AWS cloud environment. Here are some basic instructions on how to test a lambda function using LocalStack.

Setup

install Local Stack:

#for Python2
pip install localstack
#for Python3
pip3 install localstack

install awscli-local:

#for Python2
pip install awscli-local
#for Python3
pip3 install awscli-local

Start LocalStack with this command:

localstack start

This should start LocalStack on your computer and should look like the following:

$ localstack start
Starting local dev environment. CTRL-C to quit.
2019–03–06T12:56:21:INFO:localstack.services.install: Downloading and installing LocalStack Java libraries. This may take some time.
Starting mock ES service (http port 4578)…
Starting local Elasticsearch (http port 4571)…
Starting mock S3 (http port 4572)…
Starting mock SNS (http port 4575)…
Starting mock SQS (http port 4576)…
Starting mock SES (http port 4579)…
Starting mock SSM (http port 4583)…
Starting mock STS (http port 4592)…
Starting mock IAM (http port 4593)…
Starting mock Secrets Manager (http port 4584)…
Starting mock API Gateway (http port 4567)…
Starting mock DynamoDB (http port 4569)…
Starting mock DynamoDB Streams service (http port 4570)…
Starting mock Firehose service (http port 4573)…
Starting mock Lambda service (http port 4574)…
Starting mock Kinesis (http port 4568)…
Starting mock Redshift (http port 4577)…
Starting mock Route53 (http port 4580)…
Starting mock CloudFormation (http port 4581)…
Starting mock CloudWatch (http port 4582)…
Starting mock StepFunctions (http port 4585)…
Ready.

Created a Lambda function in your LocalStack:

Create a test function of your choice with a handler method or create a new file named lambda.py.You can simply copy and paste the following lines in it

def handler(event, context):
print(‘test’)
return {‘foo’: ‘bar final’}

Zip the lambda.py file:

zip lambda.zip lambda.py

Create a new lambda function f1 with the lamda.zip file with the following command:

awslocal lambda create-function — function-name f1 — handler lambda.handler — runtime python — role r1 — zip-file fileb://lambda.zip

This should give you the following result:

{
“FunctionName”: “f1”,
“FunctionArn”: “arn:aws:lambda:us-east-1:000000000000:function:f1”,
“Runtime”: “python”,
“Role”: “r1”,
“Handler”: “lambda.handler”,
“VpcConfig”: {
“SubnetIds”: [
null
],
“SecurityGroupIds”: [
null
]
},
“Environment”: {
“Variables”: {},
“Error”: {}
},
“TracingConfig”: {}
}

Run the function in LocalStack

Invoke and run the lambda function f1 that we just created with the following command:

awslocal lambda invoke — function-name f1 my_test

If the function ran successfully, you should now be able to see the following results in the terminal window where you ran the command

“StatusCode”: 200 

Whatever value was returned by your function should be printed in my_test file. In the lambda.py function that we just ran above, that value was {‘foo’: ‘bar final’}.

Anything that the function prints (in this case ‘test’) should be printed in the terminal window where the lambda is running. I.e. In the window where you ran ‘localstack start’ .

Create an S3 bucket

You can create an S3 bucket called ‘my-test-bucket’ with the following command:

awslocal s3 mb s3://my-test-bucket

You should see make_bucket: my-test-bucket in your terminal

Verify the bucket was created successfully with this command:

 awslocal s3 ls

Your bucket name (‘my-test-bucket’ in this case) should be listed there

Create secrets in Secretsmanager

You can create a new secret with the name ‘my_secret’ and the secret mysupersecretpassword in LocalStack secretsmanager with the following command:

awslocal secretsmanager create-secret — name my_secret — secret-string mysupersecretpassword

If you have multiple secrets to store in ‘my_secret’ such as username and password, you can save it as a key:value pair

awslocal secretsmanager create-secret — name my_secret — secret-string [{“my_uname”:”username”,“my_pwd”:”password”}]

About awslocal

Awslocal is a wrapper around AWS cli that just makes sure that everything you run, runs in Local Stack and not in your AWS account. Therefore, you can pretty much run any commands from AWS cli in awslocal identically. Check out the AWS cli reference for more info on what you can run and how: https://docs.aws.amazon.com/cli/latest/reference/

--

--