Localstack, giả lập môi trường AWS.

Mục đích sử dụng:

Localstack mô phỏng lại môi trường AWS trên máy local.

Nó giúp phát triển và kiểm thử các giải pháp như microservices hoặc functions và sử dụng các dịch vụ có trong AWS.

Các dịch vụ AWS trên Localstack:

API Gateway,
Kinesis, 
DynamoDB, 
DynamoDB Streams, 
Elasticsearch, 
S3, 
Firehose, 
Lambda, 
SNS, 
SQS, 
Redshift, 
ES (Elasticsearch Service), 
SES, 
Route53, 
CloudFormation, 
CloudWatch, 
SSM, 
SecretsManager

Cài đặt và sử dụng:

Cài đặt localstack:

YAML
# docker-compose.yml

version: "3.2"
services:
  localstack:
    image: localstack/localstack:latest
    container_name: localstack_demo
    ports:
      # - "127.0.0.1:4566:4566" # LocalStack Gateway
      - "127.0.0.1:4510-4559:4510-4559" # external services port range
      - "0.0.0.0:4566:4566"
    environment:
      - SERVICES=s3
      - DEBUG=1
      - DATA_DIR=/tmp/localstack/data
    volumes:
      - "./.localstack:/var/lib/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"

Start localstack:

Zsh
docker-compose up -d

Tạo profile cho Localstack:

Zsh
aws configure --profile localstack
 
AWS Access Key ID [****************demo]: dummy
AWS Secret Access Key [****************demo]: dummy
Default region name [ap-northeast-1]: ap-northeast-1
Default output format [json]: json

Or

Zsh
vi ~/.aws/config
VimL
[profile localstack]
output = json
region = ap-northeast-1
Zsh
vi ~/.aws/credentials
VimL
[localstack]
aws_access_key_id = dummy
aws_secret_access_key = dummy

Tạo S3 trong Localstack:

Zsh
aws --endpoint-url=http://localhost:4566 --profile localstack s3api create-bucket --bucket test-bucket --create-bucket-configuration LocationConstraint=ap-northeast-1

# Kết quả:
{
    "Location": "http://test-bucket.s3.localhost.localstack.cloud:4566/"
}
Zsh
aws s3 ls --endpoint-url=http://localhost:4566 --profile localstack

# Kết quả:
2023-11-15 10:24:40 test-bucket
YAML
# .env

# Cả 2 endpoint đều hoạt động với Localstack ở local.
#S3_ENDPOINT='http://localhost:4566'
S3_ENDPOINT='http://test-bucket.s3.localhost.localstack.cloud:4566/'
S3_AWS_ACCESS_KEY_ID='dummy'
S3_AWS_SECRET_KEY='dummy'
S3_AWS_BUCKET_NAME='test-bucket'

Tạo Dynamodb trong Localstack:

YAML
version: "3.2"
services:
  localstack:
    image: localstack/localstack:latest
    container_name: localstack_demo
    ports:
      # - "127.0.0.1:4566:4566" # LocalStack Gateway
      - "127.0.0.1:4510-4559:4510-4559" # external services port range
      - "0.0.0.0:4566:4566"
    environment:
      - SERVICES=dynamodb
      - DEBUG=1
      - DATA_DIR=/tmp/localstack/data
    volumes:
      - "./.localstack:/var/lib/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"
Zsh
aws dynamodb create-table \
    --table-name test \
    --key-schema AttributeName=id,KeyType=HASH \
    --attribute-definitions AttributeName=id,AttributeType=S \
    --billing-mode PAY_PER_REQUEST \
    --region ap-northeast-1 \
    --endpoint-url=http://localhost:4566 \
    --profile localstack

# Kết quả:
{
    "TableDescription": {
        "AttributeDefinitions": [
            {
                "AttributeName": "id",
                "AttributeType": "S"
            }
        ],
        "TableName": "test",
        "KeySchema": [
            {
                "AttributeName": "id",
                "KeyType": "HASH"
            }
        ],
        "TableStatus": "ACTIVE",
        "CreationDateTime": "2023-11-15T11:29:05.597000+07:00",
        "ProvisionedThroughput": {
            "LastIncreaseDateTime": "1970-01-01T07:00:00+07:00",
            "LastDecreaseDateTime": "1970-01-01T07:00:00+07:00",
            "NumberOfDecreasesToday": 0,
            "ReadCapacityUnits": 0,
            "WriteCapacityUnits": 0
        },
        "TableSizeBytes": 0,
        "ItemCount": 0,
        "TableArn": "arn:aws:dynamodb:ap-northeast-1:000000000000:table/test",
        "TableId": "fb25d41f-0563-4be9-8b21-75bc3b7006b9",
        "BillingModeSummary": {
            "BillingMode": "PAY_PER_REQUEST",
            "LastUpdateToPayPerRequestDateTime": "2023-11-15T11:29:05.597000+07:00"
        }
    }
}
Bạn muốn sử dụng dịch vụ nào của AWS thì thêm dịch vụ đấy vào SERVICES trong file docker-compose.yml.

Tài liệu tham khảo: https://docs.localstack.cloud/

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like