Closed15

LocalStack ことはじめ

opparaoppara

起動(Docker で動かす)

% localstack start -d

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

 💻 LocalStack CLI 0.14.2.11

[09:37:49] starting LocalStack in Docker mode 🐳      
           preparing environment   
           configuring container     
           starting container           
[09:37:54] detaching                
opparaoppara

Docker の状況

% localstack status docker
┏━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Runtime version ┃ 0.12.20                                               ┃
│ Docker image    │ tag: latest, id: abafc38deff4, 📆 2021-11-14T21:20:33 │
│ Runtime status  │ ✔ running (name: "localstack_main", IP: 172.17.0.2)   │
└─────────────────┴───────────────────────────────────────────────────────┘
opparaoppara

サービス一覧

% 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 │
│ redshift                 │ ✔ available │
│ resource-groups          │ ✔ available │
│ resourcegroupstaggingapi │ ✔ available │
│ route53                  │ ✔ available │
│ s3                       │ ✔ available │
│ secretsmanager           │ ✔ available │
│ ses                      │ ✔ available │
│ sns                      │ ✔ available │
│ sqs                      │ ✔ available │
│ ssm                      │ ✔ available │
│ stepfunctions            │ ✔ available │
│ sts                      │ ✔ available │
│ support                  │ ✔ available │
│ swf                      │ ✔ available │
└──────────────────────────┴─────────────┘
opparaoppara

終了

% localstack stop
container stopped: localstack_main

% localstack status services
✖ ERROR: could not connect to LocalStack health endpoint at http://localhost:4566
opparaoppara

設定

~/.aws/credentials
[localstack]
aws_access_key_id = dummy
aws_secret_access_key = dummy
~/.aws/config
[profile localstack]
region=us-east-1
output=json
opparaoppara

S3 バケットを作成する

% export AWS_PROFILE=localstack
% aws s3 mb s3://test-bucket --endpoint-url=http://localhost:4566
make_bucket: test-bucket

% aws s3 ls --endpoint-url=http://localhost:4566
2022-05-26 23:24:03 test-bucket

opparaoppara

endpoint 指定が面倒なので awslocal をインストール

% pip3 install awscli-local

% awslocal s3 ls
2022-05-26 23:24:03 test-bucket

opparaoppara

DynamoDB のテーブルを作る

% awslocal dynamodb create-table \
  --table-name vote \
  --attribute-definitions \
  AttributeName=user_id,AttributeType=S \
  AttributeName=dt,AttributeType=S \
  --key-schema AttributeName=user_id,KeyType=HASH AttributeName=dt,KeyType=RANGE \
  --billing-mode PAY_PER_REQUEST

opparaoppara

データの挿入

% awslocal dynamodb put-item \
--table-name vote \
--item '{ "product_id": { "N": "3" }, "dt": { "S": "20220512" }, "user_id": { "S": "u123" } }'

% awslocal dynamodb put-item \
--table-name vote \
--item '{ "product_id": { "N": "17" }, "dt": { "S": "20220512" }, "user_id": { "S": "u938" } }'

スキャン

% awslocal dynamodb scan \
--table-name vote

None    2       2
DT      20220512
PRODUCT_ID      3
USER_ID u123
DT      20220512
PRODUCT_ID      17
USER_ID u938

get-item

% awslocal dynamodb get-item \
--table-name vote \
--consistent-read \
--key '{"user_id": {"S": "u123"}, "dt": {"S": "20220512"}}'

DT      20220512
PRODUCT_ID      3
USER_ID u123
opparaoppara

ローカルのコマンドではなく、Docker で動かす

% git clone git@github.com:localstack/localstack.git repo
% cd repo
% docker-compose up -d
opparaoppara

EC2 起動

% awslocal ec2 run-instances

ID リスト表示

% awslocal ec2 describe-instances | jq -r '.Reservations[].Instances[].InstanceId'

インスタンス詳細

% awslocal ec2 describe-instances --instance-ids "i-xxxxxxxx" 

終了

% awslocal ec2 terminate-instances --instance-ids "i-xxxxxxxx" 
opparaoppara

タグ付き

% awslocal ec2 run-instances --tag-specifications 'ResourceType=instance,Tags=[{Key=env,Value=prod}]' 

出来たインスタンスはこんな感じ。
もちろん VPC とかは作ってない。

% awslocal ec2 describe-instances | jq -r '.Reservations[].Instances[]'
{
  "AmiLaunchIndex": 0,
  "ImageId": "None",
  "InstanceId": "i-b403282597afb0c71",
  "InstanceType": "m1.small",
  "KernelId": "None",
  "KeyName": "None",
  "LaunchTime": "2022-06-29T00:18:49+00:00",
  "Monitoring": {
    "State": "disabled"
  },
  "Placement": {
    "AvailabilityZone": "us-east-1a",
    "GroupName": "",
    "Tenancy": "default"
  },
  "PrivateDnsName": "ip-10-241-41-44.ec2.internal",
  "PrivateIpAddress": "10.241.41.44",
  "ProductCodes": [],
  "PublicDnsName": "ec2-54-214-94-237.compute-1.amazonaws.com",
  "PublicIpAddress": "54.214.94.237",
  "State": {
    "Code": 16,
    "Name": "running"
  },
  "StateTransitionReason": "",
  "SubnetId": "subnet-3166d735",
  "VpcId": "vpc-99b8ac71",
  "Architecture": "x86_64",
  "BlockDeviceMappings": [
    {
      "DeviceName": "/dev/sda1",
      "Ebs": {
        "AttachTime": "2022-06-29T00:18:49+00:00",
        "DeleteOnTermination": true,
        "Status": "in-use",
        "VolumeId": "vol-8b5a4d23"
      }
    }
  ],
  "ClientToken": "ABCDE0000000000003",
  "EbsOptimized": false,
  "Hypervisor": "xen",
  "NetworkInterfaces": [
    {
      "Association": {
        "IpOwnerId": "000000000000",
        "PublicIp": "54.214.94.237"
      },
      "Attachment": {
        "AttachTime": "2015-01-01T00:00:00+00:00",
        "AttachmentId": "eni-attach-8b24fa0a",
        "DeleteOnTermination": true,
        "DeviceIndex": 0,
        "Status": "attached"
      },
      "Description": "Primary network interface",
      "Groups": [
        {
          "GroupName": "default",
          "GroupId": "sg-5f007da0ab280ec2e"
        }
      ],
      "MacAddress": "1b:2b:3c:4d:5e:6f",
      "NetworkInterfaceId": "eni-3e1801df",
      "OwnerId": "000000000000",
      "PrivateIpAddress": "10.241.41.44",
      "PrivateIpAddresses": [
        {
          "Association": {
            "IpOwnerId": "000000000000",
            "PublicIp": "54.214.94.237"
          },
          "Primary": true,
          "PrivateIpAddress": "10.241.41.44"
        }
      ],
      "SourceDestCheck": true,
      "Status": "in-use",
      "SubnetId": "subnet-3166d735",
      "VpcId": "vpc-99b8ac71"
    }
  ],
  "RootDeviceName": "/dev/sda1",
  "RootDeviceType": "ebs",
  "SecurityGroups": [],
  "SourceDestCheck": true,
  "StateReason": {
    "Code": "",
    "Message": ""
  },
  "Tags": [
    {
      "Key": "env",
      "Value": "prod"
    }
  ],
  "VirtualizationType": "paravirtual"
}
このスクラップは2022/07/05にクローズされました