Coder Social home page Coder Social logo

localstack_poc's Introduction

localstack_poc

POC repository for someone who wants a first good look and hands-on experience at Localstack.

What this repository does:

  • Creates an AWS Lambda that will get triggered by an S3 bucket file upload. The Lambda stores names of uploaded files in a list in ElastiCache (Redis). All the infrastructure is deployed using Localstack (https://github.com/localstack/localstack) to emulate actual AWS infrastructure at no cost.
  • Runs Localstack using Docker via Docker-Compose
  • Creates all required infrastructure on Localstack using Terraform

Installation

Requires

Use the package manager pip to install the localstack_poc dev dependencies using a virtual environment.

$ python -m venv --prompt=localstack_poc venv
$ source venv/bin/activate
$ pip install --upgrade pip
$ pip install -r name_transcriber_lambda/requirements_dev.txt

Set AWS keys to dummy values to avoid connecting to actual AWS by mistake:

$ export AWS_DEFAULT_REGION=us-east-1
$ export AWS_SECRET_ACCESS_KEY=test
$ export AWS_ACCESS_KEY_ID=test

You need a Localstack API KEY in order to instantiate Localstack and use the pro version to support Elasticache - REDIS. Get a 14-days free trial key at https://localstack.cloud/pricing/

Once you have one, use it:

$ export LOCALSTACK_API_KEY=<YOUR_PRO_LOCALSTACK_KEY>
# Make sure your API looks as expected
$ echo $LOCALSTACK_API_KEY

Now let's instantiate Localstack via Docker-compose in background

$ docker-compose up -d --build

Provision AWS Resources on LocalStack via Terraform

$ cd tf
$ terraform init
$ terraform apply -auto-approve

Usage/Testing

Create this shell alias to easily interact with AWS resources within the Localstack instance we just set up via Docker-compose. Keep in mind this alias is just scoped to your current shell session.

alias awslocal="aws --endpoint-url=http://localhost:4566"

Test that the Redis Cluster is on and listening:

Use terraform output to grab the redis_endpoint and connect to it using the redis-cli.

$ terraform output
lambda_arn = "arn:aws:lambda:us-east-1:000000000000:function:redis-cloudapp_transcriber_default"
lambda_role_name = "redis-cloudapp_transcriber_default"
redis_endpoint = "localhost:4510"

As the hostname is localhost, we just need to provide the port to ping the Redis cluster:

$ redis-cli -p 4510 ping
PONG

It says PONG, so we are ready to go!

Let's copy a file to s3 to test the lambda:

cd ..
awslocal s3 cp testing_file.txt s3://files-redis-cloudapp/

And let's retrieve the values in the Redis cluster for the lists object_names and object_keys:

$ redis-cli -p 4510 lrange object_names 0 -1
1) "testing_file.txt"
$ redis-cli -p 4510 lrange object_keys 0 -1
1) "testing_file.txt"

We can see the name of the testing file that we just used (testing_file.txt) and both lists, so we confirm our POC application is working as expected.

Thanks for reading!

Checking Lambda logs

# Use this command to see a list of logstreams for your lambda
awslocal logs describe-log-streams --log-group-name '/aws/lambda/redis-cloudapp_transcriber_default' --query logStreams[*].logStreamName

# Pick one and then see all the events by using the command below
awslocal logs get-log-events --log-group-name '/aws/lambda/redis-cloudapp_transcriber_default' --log-stream-name <<YOUR_LOGSTREAM_NAME>>
# e.g.
awslocal logs get-log-events --log-group-name '/aws/lambda/redis-cloudapp_transcriber_default' --log-stream-name 2022/05/24/[LATEST]fd37f362

Docs used:

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

Unlicensed

localstack_poc's People

Contributors

jsvasquez avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.