0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ローカル環境でAWSを使ってみる(FlociをつかってTerraformで構築)

0
Last updated at Posted at 2026-08-28

AWSをローカル環境で使ってみる

以前は、LocalStackを使っていたんですけど、2026年3月にライセンスが変わってしまい一部有料になるなど困った状態に陥っていました。

そこで、今回は「Floci」を使ってみました。

ライセンスは MIT licensed になっています。

設定方法

今回は、Floci・Terraform・AWS CLIを全てdockerに入れる形として、ホストOSを汚さないように設定してみます。

docker-compose.yml

services:
  # 1. AWS エミュレータ本体
  floci:
    image: floci/floci:latest
    container_name: floci
    ports:
      - "4566:4566"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      - lab-net

  # 2. Terraform コンテナ
  terraform:
    image: hashicorp/terraform:latest
    container_name: terraform
    working_dir: /workspace
    volumes:
      - .:/workspace
    environment:
      - AWS_ACCESS_KEY_ID=mock_key
      - AWS_SECRET_ACCESS_KEY=mock_secret
      - AWS_DEFAULT_REGION=ap-northeast-1
    networks:
      - lab-net
    profiles: ["tools"]

  # 3. AWS CLI コンテナ
  aws:
    image: amazon/aws-cli:latest
    container_name: aws-cli
    environment:
      - AWS_ACCESS_KEY_ID=mock_key
      - AWS_SECRET_ACCESS_KEY=mock_secret
      - AWS_DEFAULT_REGION=ap-northeast-1
      - AWS_ENDPOINT_URL=http://floci:4566
    networks:
      - lab-net
    profiles: ["tools"]

networks:
  lab-net:
    driver: bridge

main.tf(Terraform設定ファイル)

s3、sqs、dynamodbを作る例です

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region                      = "ap-northeast-1"
  access_key                  = "mock_key"
  secret_key                  = "mock_secret"
  skip_credentials_validation = true
  skip_metadata_api_check     = true
  skip_requesting_account_id  = true
  s3_use_path_style           = true

  endpoints {
    s3       = "http://floci:4566"
    sqs      = "http://floci:4566"
    dynamodb = "http://floci:4566"
  }
}

# S3バケット
resource "aws_s3_bucket" "test_bucket" {
  bucket = "floci-test-bucket"
}

# SQSキュー
resource "aws_sqs_queue" "test_queue" {
  name = "floci-test-queue"
}

# DynamoDBテーブル
resource "aws_dynamodb_table" "test_table" {
  name         = "Users"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = "UserId"

  attribute {
    name = "UserId"
    type = "S"
  }
}

ターミナル操作用のエリアス

alias tf="docker compose run --rm terraform"
alias aws="docker compose run --rm aws"

【補足】docker compose run --rm
一時的なコンテナを起動して指定したコマンドを実行し、処理が終了したときにそのコンテナを自動で削除します。

起動および実験

Floci の起動

docker compose up -d floci

Terraform でリソース作成

tf init
tf apply -auto-approve

AWS CLI にてS3操作

S3パケット一覧

aws s3 ls

実行結果

2026-08-28 01:20:06 floci-test-bucket

S3にファイルをアップロード

echo "Hello from Floci S3!" > sample.txt
docker compose run --rm -v $(pwd):/workspace -w /workspace aws \
 s3 cp sample.txt s3://floci-test-bucket/sample.txt

実行結果

upload: ./sample.txt to s3://floci-test-bucket/sample.txt

S3上のファイルを確認

aws s3 ls s3://floci-test-bucket/

実行結果

2026-08-28 01:25:11 21 sample.txt

S3上のファイルをダウンロード

docker compose run --rm -v $(pwd):/workspace -w /workspace aws \
 s3 cp s3://floci-test-bucket/sample.txt downloaded.txt

実行結果

download: s3://floci-test-bucket/sample.txt to ./downloaded.txt

cat ./downloaded.txt

Hello from Floci S3!

AWS CLI にてSQS操作

SQS キュー一覧

aws sqs list-queues

実行結果

{
    "QueueUrls": [
        "http://localhost:4566/000000000000/floci-test-queue"
    ]
}

SQS にメッセージを送信

aws sqs send-message \
  --queue-url http://floci:4566/000000000000/floci-test-queue \
  --message-body "Hello from Dockerized AWS CLI!"

実行結果

{
    "MD5OfMessageBody": "3db7f5028bdefc61e65d88721e7e315e",
    "MessageId": "a8e578c7-b5bf-4751-9df6-e53e57feb773"
}

SQS からメッセージを受信

aws sqs receive-message \
  --queue-url http://floci:4566/000000000000/floci-test-queue

実行結果

{
    "Messages": [
        {
            "MessageId": "a8e578c7-b5bf-4751-9df6-e53e57feb773",
            "ReceiptHandle": "fe8d3933-b05d-434e-9958-a5050bb70719",
            "MD5OfBody": "3db7f5028bdefc61e65d88721e7e315e",
            "Body": "Hello from Dockerized AWS CLI!"
        }
    ]
}

AWS CLI にてDynamoDB操作

DynamoDB テーブル確認

aws dynamodb list-tables

実行結果

{
    "TableNames": [
        "Users"
    ]
}

DynamoDB レコードの挿入

aws dynamodb put-item \
  --table-name Users \
  --item '{"UserId": {"S": "user_001"}, "Name": {"S": "Taro"}, "Role": {"S": "Admin"}}'

DynamoDB レコードの取得

aws dynamodb get-item \
  --table-name Users \
  --key '{"UserId": {"S": "user_001"}}'

実行結果

{
    "Item": {
        "UserId": {
            "S": "user_001"
        },
        "Name": {
            "S": "Taro"
        },
        "Role": {
            "S": "Admin"
        }
    }
}

クリーアップ(消す場合)

tf destroy -auto-approve
docker compose down
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?