以下の内容はhttps://kakakakakku.hatenablog.com/entry/2026/01/03/161725より取得しました。


Terraform sandboxes: 公式のサンドボックス環境で Terraform を勉強しよう

HashiCorp 公式の Terraform サンドボックス環境「Terraform sandboxes」を使うとブラウザ上で Terraform の勉強ができる💡LocalStack(AWS エミュレータ)もプリインストールされてて Terraform AWS Provider の勉強にもなる.

最近までなかったように思うけど(僕の勘違いの可能性もある)さっそく試してみた❗️

developer.hashicorp.com

バージョン確認

ドキュメントを読むと

  • Terraform
  • Docker
  • LocalStack
  • AWS CLI

の4種類がプリインストールされてると記載されていた.

2026年1月時点でバージョンを確認してみた.ちょっと古めかなと思う😶

  • Terraform 1.12.2
  • Terraform AWS Provider 5.87.0
  • Docker 28.4.0
  • LocalStack 4.2.0
  • AWS CLI 1.41.0
  • Python 3.12.3
root@workstation:~/terraform-sandbox# terraform -version
Terraform v1.12.2
on linux_amd64
+ provider registry.terraform.io/hashicorp/aws v5.87.0

Your version of Terraform is out of date! The latest version
is 1.14.3. You can update by downloading from https://developer.hashicorp.com/terraform/install

root@workstation:~/terraform-sandbox# docker --version
Docker version 28.4.0, build d8eb465

root@workstation:~/terraform-sandbox# docker ps
CONTAINER ID   IMAGE                   COMMAND                  CREATED         STATUS                   PORTS                                                                    NAMES
1a27206e1292   localstack/localstack   "docker-entrypoint.sh"   6 minutes ago   Up 6 minutes (healthy)   127.0.0.1:4510-4560->4510-4560/tcp, 127.0.0.1:4566->4566/tcp, 5678/tcp   localstack-main

root@workstation:~/terraform-sandbox# localstack --version
LocalStack CLI 4.2.0

root@workstation:~/terraform-sandbox# aws --version
aws-cli/1.41.0 Python/3.12.3 Linux/6.11.0-1016-gcp botocore/1.39.0

root@workstation:~/terraform-sandbox# awslocal --version
aws-cli/1.41.0 Python/3.12.3 Linux/6.11.0-1016-gcp botocore/1.39.0

root@workstation:~/terraform-sandbox# python --version
Python 3.12.3

コード確認

まずはサンドボックス環境を起動する.

すると Shell(ターミナル)Code Editor(エディタ)が表示される.

デフォルトのファイル構成は以下のようになっていた.

root@workstation:~/terraform-sandbox# tree .
.
├── ARCHITECTURE.md
├── LIMITATIONS.md
├── README.md
├── localstack_override.tf
├── main.tf
└── terraform.tf

1 directory, 6 files

👾 terraform.tf

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "5.87.0"
    }
  }
}

👾 main.tf

#######################
# Example AWS resources
#######################

resource "aws_instance" "web" {
  ami           = "test"
  instance_type = "t3.small"
}

output "instance_detail" {
  value = aws_instance.web.id
}

👾 localstack_override.tf

特徴的なのは localstack_override.tf で Terraform AWS Provider のデプロイ対象が AWS アカウントではなく LocalStack になっているところ.プロバイダー設定でエンドポイントが上書きされていた.

# This file overrides the AWS provider configuration to
# use LocalStack. If you wish to deploy to your own AWS
# account, delete this file.

provider "aws" {
  access_key                  = "test"
  secret_key                  = "test"
  region                      = "us-east-1"
  s3_use_path_style           = false
  skip_credentials_validation = true
  skip_metadata_api_check     = true
  skip_requesting_account_id  = true

  endpoints {
    apigateway     = "http://localhost:4566"
    apigatewayv2   = "http://localhost:4566"
    cloudformation = "http://localhost:4566"
    cloudwatch     = "http://localhost:4566"
    dynamodb       = "http://localhost:4566"
    ec2            = "http://localhost:4566"
    es             = "http://localhost:4566"
    elasticache    = "http://localhost:4566"
    firehose       = "http://localhost:4566"
    iam            = "http://localhost:4566"
    kinesis        = "http://localhost:4566"
    lambda         = "http://localhost:4566"
    rds            = "http://localhost:4566"
    redshift       = "http://localhost:4566"
    route53        = "http://localhost:4566"
    s3             = "http://s3.localhost.localstack.cloud:4566"
    secretsmanager = "http://localhost:4566"
    ses            = "http://localhost:4566"
    sns            = "http://localhost:4566"
    sqs            = "http://localhost:4566"
    ssm            = "http://localhost:4566"
    stepfunctions  = "http://localhost:4566"
    sts            = "http://localhost:4566"
  }
}

ちなみに LocalStack Terraform CLI(tflocal コマンド)を使えば一時的に localstack_providers_override.tf を生成して LocalStack にデプロイできて便利だけど,今回のサンドボックス環境だと AWS アカウントにデプロイする想定ではないからプロバイダー設定を LocalStack で固定しているんだと思う.あくまで予想だけど!

github.com

サンドボックス環境を試す

👾 main.tf

まずは aws_sqs_queue で必要最低限の実装をしておく.

resource "aws_sqs_queue" "sandbox" {
  name = "sandbox"
  receive_wait_time_seconds = 20
}

terraform init / plan / apply

そして LocalStack に Amazon SQS キューをデプロイする.

root@workstation:~/terraform-sandbox# terraform init

root@workstation:~/terraform-sandbox# terraform plan

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_sqs_queue.sandbox will be created
  + resource "aws_sqs_queue" "sandbox" {
      + arn                               = (known after apply)
      + content_based_deduplication       = false
      + deduplication_scope               = (known after apply)
      + delay_seconds                     = 0
      + fifo_queue                        = false
      + fifo_throughput_limit             = (known after apply)
      + id                                = (known after apply)
      + kms_data_key_reuse_period_seconds = (known after apply)
      + max_message_size                  = 262144
      + message_retention_seconds         = 345600
      + name                              = "sandbox"
      + name_prefix                       = (known after apply)
      + policy                            = (known after apply)
      + receive_wait_time_seconds         = 20
      + redrive_allow_policy              = (known after apply)
      + redrive_policy                    = (known after apply)
      + sqs_managed_sse_enabled           = (known after apply)
      + tags_all                          = (known after apply)
      + url                               = (known after apply)
      + visibility_timeout_seconds        = 30
    }

Plan: 1 to add, 0 to change, 0 to destroy.

───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.

root@workstation:~/terraform-sandbox# terraform apply

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_sqs_queue.sandbox will be created
  + resource "aws_sqs_queue" "sandbox" {
      + arn                               = (known after apply)
      + content_based_deduplication       = false
      + deduplication_scope               = (known after apply)
      + delay_seconds                     = 0
      + fifo_queue                        = false
      + fifo_throughput_limit             = (known after apply)
      + id                                = (known after apply)
      + kms_data_key_reuse_period_seconds = (known after apply)
      + max_message_size                  = 262144
      + message_retention_seconds         = 345600
      + name                              = "sandbox"
      + name_prefix                       = (known after apply)
      + policy                            = (known after apply)
      + receive_wait_time_seconds         = 20
      + redrive_allow_policy              = (known after apply)
      + redrive_policy                    = (known after apply)
      + sqs_managed_sse_enabled           = (known after apply)
      + tags_all                          = (known after apply)
      + url                               = (known after apply)
      + visibility_timeout_seconds        = 30
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_sqs_queue.sandbox: Creating...
aws_sqs_queue.sandbox: Still creating... [00m10s elapsed]
aws_sqs_queue.sandbox: Still creating... [00m20s elapsed]
aws_sqs_queue.sandbox: Creation complete after 25s [id=http://sqs.us-east-1.localhost.localstack.cloud:4566/000000000000/sandbox]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

awslocal sqs send-message

LocalStack AWS CLI(awslocal コマンド)で操作してみる😀 まずは awslocal sqs send-message コマンドで Amazon SQS キューにメッセージを登録する.

github.com

root@workstation:~/terraform-sandbox# awslocal sqs send-message --queue-url http://sqs.us-east-1.localhost.localstack.cloud:4566/000000000000/sandbox --message-body hello!
{
    "MD5OfMessageBody": "5a8dd3ad0756a93ded72b823b19dd877",
    "MessageId": "b165a472-9978-49c0-8bb7-75dbfa482e8c"
}

docs.aws.amazon.com

awslocal sqs receive-message

次に awslocal sqs receive-message コマンドで Amazon SQS キューからメッセージを取得する.

root@workstation:~/terraform-sandbox# awslocal sqs receive-message --queue-url http://sqs.us-east-1.localhost.localstack.cloud:4566/000000000000/sandbox
{
    "Messages": [
        {
            "MessageId": "b165a472-9978-49c0-8bb7-75dbfa482e8c",
            "ReceiptHandle": "NjQyZjE3OWYtZDE5NC00YWJlLTgwMzctOGQ4ZjdjZmY5NzgzIGFybjphd3M6c3FzOnVzLWVhc3QtMTowMDAwMDAwMDAwMDA6c2FuZGJveCBiMTY1YTQ3Mi05OTc4LTQ5YzAtOGJiNy03NWRiZmE0ODJlOGMgMTc2NzQxNDkxMS4wNDc4NjMy",
            "MD5OfBody": "5a8dd3ad0756a93ded72b823b19dd877",
            "Body": "hello!"
        }
    ]
}

docs.aws.amazon.com

イイ感じ❗️Terraform サンドボックス環境「Terraform sandboxes」で Amazon SQS キューを LocalStack にデプロイできた.

GitHub Codespaces

ちなみに「LocalStack 実践入門 | AWS x Terraform 入門ワークショップ」では GitHub Codespaces を Terraform のサンドボックス環境として使っている😀

zenn.dev

GitHub リポジトリに統合されてて使いやすく DevContainers でベースイメージやツールセットを自由にセットアップできるのも便利かなと.Terraform sandboxes 以外の選択肢として参考になれば✋️

github.com

X ポスト




以上の内容はhttps://kakakakakku.hatenablog.com/entry/2026/01/03/161725より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14