localstack

localstack on Mac #

Install #

https://docs.localstack.cloud/getting-started/installation/

% brew install localstack/tap/localstack-cli

% localstack --version
2.3.2

% localstack start -d

     __                     _______ __             __
    / /   ____  _________ _/ / ___// /_____ ______/ /__
   / /   / __ \/ ___/ __ `/ /\__ \/ __/ __ `/ ___/ //_/
  / /___/ /_/ / /__/ /_/ / /___/ / /_/ /_/ / /__/ ,<
 /_____/\____/\___/\__,_/_//____/\__/\__,_/\___/_/|_|

 ๐Ÿ’ป LocalStack CLI 2.3.2

[10:57:13] starting LocalStack in Docker mode ๐Ÿณ                localstack.py:495
           preparing environment                                bootstrap.py:1206
[10:57:14] configuring container                                bootstrap.py:1214
[10:57:19] starting container                                   bootstrap.py:1224
[10:58:15] detaching                                            bootstrap.py:122

% localstack status services
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”“
โ”ƒ Service                  โ”ƒ Status      โ”ƒ
โ”กโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ฉ
โ”‚ acm                      โ”‚ โœ” available โ”‚
โ”‚ apigateway               โ”‚ โœ” available โ”‚
โ”‚ cloudformation           โ”‚ โœ” available โ”‚
โ”‚ cloudwatch               โ”‚ โœ” available โ”‚
โ”‚ config                   โ”‚ โœ” available โ”‚
โ”‚ dynamodb                 โ”‚ โœ” available โ”‚
โ”‚ dynamodbstreams          โ”‚ โœ” available โ”‚
โ”‚ ec2                      โ”‚ โœ” available โ”‚
โ”‚ es                       โ”‚ โœ” available โ”‚
โ”‚ events                   โ”‚ โœ” available โ”‚
โ”‚ firehose                 โ”‚ โœ” available โ”‚
โ”‚ iam                      โ”‚ โœ” available โ”‚
โ”‚ kinesis                  โ”‚ โœ” available โ”‚
โ”‚ kms                      โ”‚ โœ” available โ”‚
โ”‚ lambda                   โ”‚ โœ” available โ”‚
โ”‚ logs                     โ”‚ โœ” available โ”‚
โ”‚ opensearch               โ”‚ โœ” available โ”‚
โ”‚ ram                      โ”‚ โœ” available โ”‚
โ”‚ redshift                 โ”‚ โœ” available โ”‚
โ”‚ resource-groups          โ”‚ โœ” available โ”‚
โ”‚ resourcegroupstaggingapi โ”‚ โœ” available โ”‚
โ”‚ route53                  โ”‚ โœ” available โ”‚
โ”‚ route53resolver          โ”‚ โœ” available โ”‚
โ”‚ s3                       โ”‚ โœ” available โ”‚
โ”‚ s3control                โ”‚ โœ” available โ”‚
โ”‚ scheduler                โ”‚ โœ” available โ”‚
โ”‚ secretsmanager           โ”‚ โœ” available โ”‚
โ”‚ ses                      โ”‚ โœ” available โ”‚
โ”‚ sns                      โ”‚ โœ” available โ”‚
โ”‚ sqs                      โ”‚ โœ” available โ”‚
โ”‚ ssm                      โ”‚ โœ” available โ”‚
โ”‚ stepfunctions            โ”‚ โœ” available โ”‚
โ”‚ sts                      โ”‚ โœ” available โ”‚
โ”‚ support                  โ”‚ โœ” available โ”‚
โ”‚ swf                      โ”‚ โœ” available โ”‚
โ”‚ transcribe               โ”‚ โœ” available โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

% brew install awscli

% which aws
/usr/local/bin/aws

% aws --version
aws-cli/2.13.32 Python/3.11.6 Darwin/21.6.0 source/x86_64 prompt/off

% aws configure --profile localstack
AWS Access Key ID [None]: dummy
AWS Secret Access Key [None]: dummy
Default region name [None]: us-east-1
Default output format [None]: json

% cat ~/.aws/credentials
[localstack]
aws_access_key_id = dummy
aws_secret_access_key = dummy

% cat ~/.aws/config
[profile localstack]
region = us-east-1
output = json

SQS (Simple Queue Service) #

  • ๆฆ‚่ฆ
    • ใ‚ทใ‚นใƒ†ใƒ ใ‹ใ‚‰ใƒกใƒƒใ‚ปใƒผใ‚ธใ‚’ๅ—ใ‘ๅ–ใ‚Šใ€ไธ€ๆ™‚็š„ใซไฟๅญ˜ใ—ใŸไธŠใงๅ‡ฆ็†
    • ๅคง้‡ใฎใƒกใƒƒใ‚ปใƒผใ‚ธใ‚’ๅนณๆบ–ๅŒ–ใ—ใฆ(ๅ‡็ญ‰ใชไฝœๆฅญ้‡ใง)ๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใฎใƒใƒƒใƒ•ใ‚ก(็ทฉ่กๆ)ใจใ—ใฆ
      ใพใŸใฏใ€Microservice้–“ใฎใ‚ฟใ‚นใ‚ฏใฎ้žๅŒๆœŸๅ‡ฆ็†ใจใ—ใฆไฝฟ็”จ
    • FIFO (First-In-First-Out)ใ‚ญใƒฅใƒผใ‚’ไฝฟ็”จใ™ใ‚‹ใจใƒกใƒƒใ‚ปใƒผใ‚ธใฎ้ †ๅบใ‚„ไธ€่ฒซๆ€งใ‚’ไฟ่จผ

SQSใ‚ญใƒฅใƒผใฎไฝœๆˆ

% aws sqs create-queue --queue-name test-queue --endpoint-url http://localhost:4566 --profile localstack 

SQSใ‚ญใƒฅใƒผใฎไธ€่ฆง

% aws sqs list-queues --endpoint-url http://localhost:4566 --profile localstack
{
    "QueueUrl": "http://localhost:4566/000000000000/test-queue"
}

SQSใ‚ญใƒฅใƒผใฎๅฑžๆ€ง

SQSใ‚ญใƒฅใƒผใฎๅฑžๆ€ง
% aws sqs get-queue-attributes --queue-url http://localhost:4566/000000000000/test-queue --attribute-names All --endpoint-url http://localhost:4566 --profile localstack

{
    "Attributes": {
        "ApproximateNumberOfMessages": "0",
        "ApproximateNumberOfMessagesNotVisible": "0",
        "ApproximateNumberOfMessagesDelayed": "0",
        "CreatedTimestamp": "1693055519",
        "DelaySeconds": "0",
        "LastModifiedTimestamp": "1693055519",
        "MaximumMessageSize": "262144",
        "MessageRetentionPeriod": "345600",
        "QueueArn": "arn:aws:sqs:us-east-1:000000000000:test-queue",
        "ReceiveMessageWaitTimeSeconds": "0",
        "VisibilityTimeout": "30",
        "SqsManagedSseEnabled": "false"
    }
}

SQSใ‚ญใƒฅใƒผใธใƒกใƒƒใ‚ปใƒผใ‚ธใ‚’้€ไฟก

%  aws sqs send-message --queue-url "http://localhost:4566/000000000000/test-queue" --message-body "hello sqs" --endpoint-url http://localhost:4566 --profile localstack

{
    "MD5OfMessageBody": "3b7bef57d06c0021d0aafe8f6d587241",
    "MessageId": "24b091c2-63fa-4814-bb84-30ff1930f5fd"
}

SQSใ‚ญใƒฅใƒผใฎ็พๅœจใฎใƒกใƒƒใ‚ปใƒผใ‚ธๆ•ฐใ‚’็ขบ่ช

% aws sqs get-queue-attributes --queue-url 'http://localhost:4566/000000000000/test-queue' --attribute-names ApproximateNumberOfMessages --query 'Attributes.ApproximateNumberOfMessages' --endpoint-url http://localhost:4566 --profile localstack

"1"

SQSใ‚ญใƒฅใƒผใฎๆถˆ่ฒป

% aws sqs receive-message --queue-url 'http://localhost:4566/000000000000/test-queue' --endpoint-url http://localhost:4566 --profile localstack

{
    "Messages": [
        {
            "MessageId": "24b091c2-63fa-4814-bb84-30ff1930f5fd",
            "ReceiptHandle": "YzNjMDIzZTEtNTBkZC00NmE5LTg3NmItYTYxOTI3YzE3MWIzIGFybjphd3M6c3FzOnVzLWVhc3QtMTowMDAwMDAwMDAwMDA6dGVzdC1xdWV1ZSAyNGIwOTFjMi02M2ZhLTQ4MTQtYmI4NC0zMGZmMTkzMGY1ZmQgMTY5MzA1NTk5MC42ODAyMTQ2",
            "MD5OfBody": "3b7bef57d06c0021d0aafe8f6d587241",
            "Body": "hello sqs"
        }
    ]
}

SQSใ‚ญใƒฅใƒผใฎใƒกใƒƒใ‚ปใƒผใ‚ธใ‚’ๅ‰Š้™ค

% aws sqs delete-message --queue-url 'http://localhost:4566/000000000000/test-queue' --receipt-handle "YzNjMDIzZTEtNTBkZC00NmE5LTg3NmItYTYxOTI3YzE3MWIzIGFybjphd3M6c3FzOnVzLWVhc3QtMTowMDAwMDAwMDAwMDA6dGVzdC1xdWV1ZSAyNGIwOTFjMi02M2ZhLTQ4MTQtYmI4NC0zMGZmMTkzMGY1ZmQgMTY5MzA1NTk5MC42ODAyMTQ2" --endpoint-url http://localhost:4566 --profile localstack

SQSใ‚ญใƒฅใƒผใฎใƒกใƒƒใ‚ปใƒผใ‚ธๆ•ฐใ‚’็ขบ่ช

% aws sqs get-queue-attributes --queue-url 'http://localhost:4566/000000000000/test-queue' --attribute-names ApproximateNumberOfMessages --query 'Attributes.ApproximateNumberOfMessages' --endpoint-url http://localhost:4566 --profile localstack

"0"

SNS #

  • ๆฆ‚่ฆ
    • ้€š็Ÿฅใฎ็™บ่กŒ่€…ใ‹ใ‚‰ใƒกใƒƒใ‚ปใƒผใ‚ธใ‚’ๅ—ใ‘ๅ–ใ‚Šใ€่ณผ่ชญ่€…(ใ‚ตใƒ–ใ‚นใ‚ฏใƒฉใ‚คใƒใƒผ)ใซ้…ไฟก
    • ่ค‡ๆ•ฐใฎใ‚จใƒณใƒ‰ใƒใ‚คใƒณใƒˆ(SQSใ‚ญใƒฅใƒผใ€Lambda้–ขๆ•ฐใ€ใƒกใƒผใƒซใ€SMSใ€HTTP/HTTPS etc)ใซ้€ไฟกๅฏ
    • ใ‚ทใ‚นใƒ†ใƒ ้–“ใฎ้žๅŒๆœŸใฎใƒกใƒƒใ‚ปใƒผใ‚ธใฎไบคๆ›ใ€ใ‚คใƒ™ใƒณใƒˆ้ง†ๅ‹•ๅž‹ใฎใƒฏใƒผใ‚ฏใƒ•ใƒญใƒผ(ใƒ‡ใƒผใ‚ฟใฎ็™ป้Œฒใƒปๅค‰ๆ›ด็ญ‰ใฎ
      ็‰นๅฎšใฎใ‚คใƒ™ใƒณใƒˆใŒ็™บ็”Ÿใ—ใŸ้š›ใ€ใƒกใƒƒใ‚ปใƒผใ‚ธใ‚’้€ไฟกใ—ใ€ๅ—ใ‘ๅ–ใฃใŸใ‚ทใ‚นใƒ†ใƒ ใŒๅ‡ฆ็†ใ‚’ๅฎŸ่กŒใ™ใ‚‹็ญ‰)ใ‚’ๅฎŸ็พ

SNSใƒˆใƒ”ใƒƒใ‚ฏใฎไฝœๆˆ

% aws sns create-topic --name test-topic --endpoint-url=http://localhost:4566 --profile localstack
{
    "TopicArn": "arn:aws:sns:us-east-1:000000000000:test-topic"
}

SQSใ‚ญใƒฅใƒผใฎไฝœๆˆ

% aws sqs create-queue --queue-name test-queue --endpoint-url=http://localhost:4566 --profile localstack
{
    "QueueUrl": "http://localhost:4566/000000000000/test-queue"
}

SNSใƒˆใƒ”ใƒƒใ‚ฏใ‚’SQSใ‚ญใƒฅใƒผใง่ณผ่ชญใ—ใ€SNSใƒˆใƒ”ใƒƒใ‚ฏใซๅฏพใ—ใฆใƒกใƒƒใ‚ปใƒผใ‚ธใŒ็™บ่กŒใ•ใ‚ŒใŸใ‚‰ SQSใซ่‡ชๅ‹•้€ไฟกใ•ใ‚Œใ‚‹ใ‚ˆใ†่จญๅฎš

% aws sns subscribe --topic-arn arn:aws:sns:us-east-1:000000000000:test-topic --protocol sqs --notification-endpoint arn:aws:sqs:us-east-1:000000000000:test-queue --endpoint-url=http://localhost:4566 --profile localstack
{
    "SubscriptionArn": "arn:aws:sns:us-east-1:000000000000:test-topic:7284befa-eb97-4f8e-a76e-63891be75bd3"
}

SNSใƒˆใƒ”ใƒƒใ‚ฏใธใฎใƒกใƒƒใ‚ปใƒผใ‚ธใ‚’็™บ่กŒ

% aws sns publish --topic-arn arn:aws:sns:us-east-1:000000000000:test-topic --message 'Hello, world!' --endpoint-url http://localhost:4566 --profile localstack
{
    "MessageId": "63af4d31-59c3-4222-bc73-1ad0724592f4"
}

SNSใƒˆใƒ”ใƒƒใ‚ฏใ‚’่ณผ่ชญใ—ใฆใ„ใ‚‹SQSใ‚ญใƒฅใƒผใ‹ใ‚‰ใƒกใƒƒใ‚ปใƒผใ‚ธใ‚’ๅ—ไฟกใงใใ‚‹ใ“ใจใ‚’็ขบ่ช (ใ“ใ“ใงใฏไธŠ่จ˜ใฎMessageId 63af4d31-59c3-4222-bc73-1ad0724592f4 ใŒ Body ใซๅซใพใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ช)

% aws sqs receive-message --queue-url http://localhost:4566/000000000000/test-queue --endpoint-url http://localhost:4566 --profile localstack
{
    "Messages": [
        {
            "MessageId": "10ba045a-6c00-44b2-b3df-6774eacd97a2",
            "ReceiptHandle": "MjM5OTk2ZjctMzJhNC00OTRkLWJjZTYtMDgyMzEwYTA5M2EwIGFybjphd3M6c3FzOnVzLWVhc3QtMTowMDAwMDAwMDAwMDA6dGVzdC1xdWV1ZSAxMGJhMDQ1YS02YzAwLTQ0YjItYjNkZi02Nzc0ZWFjZDk3YTIgMTY5OTA2NDYyOS42ODU2NzQ=",
            "MD5OfBody": "095875699e9fdb2ea41cf6385f0ea1e9",
            "Body": "{\"Type\": \"Notification\", \"MessageId\": \"63af4d31-59c3-4222-bc73-1ad0724592f4\", \"TopicArn\": \"arn:aws:sns:us-east-1:000000000000:test-topic\", \"Message\": \"Hello, world!\", \"Timestamp\": \"2023-11-04T02:20:13.239Z\", \"SignatureVersion\": \"1\", \"Signature\": \"EXAMPLEpH+..\", \"SigningCertURL\": \"https://sns.us-east-1.amazonaws.com/SimpleNotificationService-0000000000000000000000.pem\", \"UnsubscribeURL\": \"http://localhost:4566/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:000000000000:test-topic:7284befa-eb97-4f8e-a76e-63891be75bd3\"}"
        }
    ]
}

References #