Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto create S3 Buckets on localstack

Using localstack in my docker-compose mainly to mimic S3.

I know I can create buckets, thats not the issue. What I would like to do is automatically create the buckets when I run a docker-compose up.

Is there something build in already for localstack?

like image 539
ThomasVdBerge Avatar asked Dec 04 '18 19:12

ThomasVdBerge


People also ask

How many S3 buckets can you create in AWS by default?

By default, you can create up to 100 buckets in each of your AWS accounts. If you need additional buckets, you can increase your account bucket limit to a maximum of 1,000 buckets by submitting a service limit increase.

Can I create S3 bucket for free?

Every object in Amazon S3 is stored in a bucket. Before you can store data in Amazon S3, you must create a bucket. You are not charged for creating a bucket. You are charged only for storing objects in the bucket and for transferring objects in and out of the bucket.

Where does Localstack store files?

You might need to check the docker-compose file to see where the container's /tmp/localstack/data path is located on your local machine -- it looks like the default is $TMPDIR/data. The docker-compose. yml is setup with DATA_DIR=/tmp/localstack/data , and a volume of './. localstack:/tmp/localstack' .


2 Answers

A change that came in with this commit since version 0.10.0.

When a container is started for the first time, it will execute files with extensions .sh that are found in /docker-entrypoint-initaws.d. Files will be executed in alphabetical order. You can easily create aws resources on localstack using awslocal (or aws) cli tool in the initialization scripts.

version: '3.7' services:   localstack:     image: localstack/localstack     environment:       - SERVICES=s3     ports:       - "4566:4566"       # - "4572:4572" Old S3 port     volumes:       - ./aws:/docker-entrypoint-initaws.d 

With a script in directory ./aws/buckets.sh:

#!/usr/bin/env bash set -x awslocal s3 mb s3://bucket set +x 

Note: the set [-/+] x is purely there to turn on and off outputting of the commands being executed.

Will produce this output:

... localstack_1  | Starting mock S3 (http port 4572)... localstack_1  | Waiting for all LocalStack services to be ready localstack_1  | Ready. localstack_1  | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initaws.d/buckets.sh localstack_1  | ++ awslocal s3 mb s3://bucket localstack_1  | make_bucket: bucket localstack_1  | ++ set +x localstack_1  | 
like image 62
Matthew Warman Avatar answered Oct 06 '22 04:10

Matthew Warman


I was been able to achieve this with Localstack with kind of "workaround":

  1. Start Localstack
  2. Create expected buckets, e.g.:

    aws --endpoint-url=http://localhost:4572 s3 mb s3://test1    
  3. Above line will update the s3_api_calls.json file in the Localstack directory (by default on Linux it's /tmp/localstack/data
  4. Backup the file
  5. Put the copied file in the Localstack directory ( /tmp/localstack/data by default) before starting the stack again
  6. You should be able to see something like 2019-03-21T08:38:28:INFO:localstack.utils.persistence: Restored 2 API calls from persistent file: /tmp/localstack/data/s3_api_calls.json the in startup log after you start Localstack again and the bucket should be available: aws --endpoint-url=http://localhost:4572 s3 ls s3://test1
like image 45
SathOkh Avatar answered Oct 06 '22 03:10

SathOkh