DEV Community

Cover image for Simulating AWS CLI with LocalStack
Israel-Lopes
Israel-Lopes

Posted on • Edited on

Simulating AWS CLI with LocalStack

Dependencies:

  • AWS CLI
  • Docker
  • Python 2.7.x or Python 3.4
  • python3-pip
  • python3-venv

To simulate the aws environment locally and very simply, first we have
to install the AWS CLI as follows:

Install the AWS CLI using a method appropriate for your operating system.

download aws-cli

As my system is Linux, I will follow other steps as follows:

If you don't have python on your machine, just follow

sudo apt-get update && sudo apt-get install python3 && sudo apt install python3-pip

Now install AWS CLI using virtual environment.

Make sure you have the python3-venv package installed by running the following command:

sudo apt install python3-venv

Create a new directory where you want to create the virtual environment. For example:

mkdir myenv

Enter the cd myenv directory

Create a virtual environment using the following command: python3 -m venv venv

Activate the virtual environment by running the command, whenever you want to run the AWS CLI, you will need
than run this command from the virtual environment:

source venv/bin/activate

then just install awscli in isolation so you don't run the risk of
dependency conflicts.

pip install awscli

Keep in mind that whenever you need to use the AWS CLI, you will first need to enable
virtual environment using the source venv/bin/activate command.

After the installation is complete, you can verify that the AWS CLI installed correctly
typing the following command:

aws --version

  1. Install Docker in your local environment, if not already installed.
  2. Download and install LocalStack using Docker Hub. For example, you can run the following command in the terminal:

docker run --rm -it -p 4566:4566 -p 4571:4571 localstack/localstack

When executing the command, if it returns this information, it means that it is roaming:

LocalStack version: 2.0.3.dev
LocalStack build date: 2023-05-06
LocalStack build git hash: 3e778577

2023-05-09T14:59:48.980 INFO --- [-functhread3] hypercorn.error : Running on https://0.0.0.0:4566 (CTRL + C to quit)
2023-05-09T14:59:48.980 INFO --- [-functhread3] hypercorn.error : Running on https://0.0.0.0:4566 (CTRL + C to quit)
Ready.
Enter fullscreen mode Exit fullscreen mode

This will launch the LocalStack container and expose ports 4566 and 4571,
that are used to access the simulated services.

Now you can use AWS CLI or SDKs to connect to LocalStack and interact
with simulated services. Be sure to set the region to
"us-east-1" and the endpoint to "http://localhost:4566" when using the AWS CLI or SDKs.

For example, to list Amazon S3 buckets using the AWS CLI:

aws --endpoint-url=http://localhost:4566 s3 ls

This command will give an error because first we have to configure aws-cli on our machine. let's go to
next step.

AWS CLI Configuration

Now we have to configure aws-cli before using it.

Run the aws configure command

It will ask for the following information:

  • AWS Access Key ID: Enter your AWS account access key ID.
  • AWS Secret Access Key: Enter the secret access key corresponding to the access key ID provided earlier.
  • Default region name: Enter the AWS region you want to use (for example, "us-east-1" or "eu-west-1").
  • Default output format: Choose a preferred output format, such as "json", "text" or "table".

After entering all the necessary information, the credentials will be configured and
stored in a configuration file in the AWS CLI directory.

Now you can start using LocalStack to interact with AWS services.
For example, you can list Amazon S3 buckets using the following command:

As with aws-cli: aws s3 ls

With local-stack: aws --endpoint-url=http://localhost:4566 s3 ls

This will return a list of buckets available in your AWS account.

Each AWS service has its own specific commands and options that can
be explored in the official AWS CLI documentation.

Let's now create a bucket for demo usage

aws --endpoint-url=http://localhost:4566 s3 mb s3://my-bucket-to-test

If you want, you can replace the name "my-bucket-to-test" with the name of the bucket you want.

When creating a bucket, it will return the message "make_bucket: my-bucket-to-test" in the prompt output.

Now we can list our bucket:

aws --endpoint-url=http://localhost:4566 s3 ls

will return "2023-05-09 12:15:00 my-bucket-to-test"

Finish

Remember that when running, you must first load the virtual environment and run the container:

source venv/bin/activate

docker run --rm -it -p 4566:4566 -p 4571:4571 localstack/localstack

Texto alternativo da imagem

Top comments (0)