AWS EmulatorでIaC(Terraform)を動かしてみる
AWS EmulatorでTerraformコードを実行できるか検証してみました。
作業ディレクトリを作成します。
# pwd
/root/data/emulatorS3を作成してみる
stsで、アカウントをEmulatorのアカウントと確認した後、main.tfを作成します。
cat > main.tf <<'EOF'
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
provider "aws" {
region = "us-east-1"
}
resource "aws_s3_bucket" "test" {
bucket = "terraform-emulator-test"
}
EOFterraform init,plan,applyを実行。
# terraform init
Initializing the backend...
Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws v6.73.0...
- Installed hashicorp/aws v6.73.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.# 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_s3_bucket.test will be created
+ resource "aws_s3_bucket" "test" {
+ acceleration_status = (known after apply)
+ acl = (known after apply)
+ arn = (known after apply)
+ bucket = "terraform-emulator-test"
+ bucket_domain_name = (known after apply)
+ bucket_namespace = (known after apply)
+ bucket_prefix = (known after apply)
+ bucket_region = (known after apply)
+ bucket_regional_domain_name = (known after apply)
+ force_destroy = false
+ hosted_zone_id = (known after apply)
+ id = (known after apply)
+ object_lock_enabled = (known after apply)
+ policy = (known after apply)
+ region = "us-east-1"
+ request_payer = (known after apply)
+ tags_all = (known after apply)
+ website_domain = (known after apply)
+ website_endpoint = (known after apply)
+ cors_rule (known after apply)
+ grant (known after apply)
+ lifecycle_rule (known after apply)
+ logging (known after apply)
+ object_lock_configuration (known after apply)
+ replication_configuration (known after apply)
+ server_side_encryption_configuration (known after apply)
+ versioning (known after apply)
+ website (known after apply)
}
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.# 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_s3_bucket.test will be created
+ resource "aws_s3_bucket" "test" {
+ acceleration_status = (known after apply)
+ acl = (known after apply)
+ arn = (known after apply)
+ bucket = "terraform-emulator-test"
+ bucket_domain_name = (known after apply)
+ bucket_namespace = (known after apply)
+ bucket_prefix = (known after apply)
+ bucket_region = (known after apply)
+ bucket_regional_domain_name = (known after apply)
+ force_destroy = false
+ hosted_zone_id = (known after apply)
+ id = (known after apply)
+ object_lock_enabled = (known after apply)
+ policy = (known after apply)
+ region = "us-east-1"
+ request_payer = (known after apply)
+ tags_all = (known after apply)
+ website_domain = (known after apply)
+ website_endpoint = (known after apply)
+ cors_rule (known after apply)
+ grant (known after apply)
+ lifecycle_rule (known after apply)
+ logging (known after apply)
+ object_lock_configuration (known after apply)
+ replication_configuration (known after apply)
+ server_side_encryption_configuration (known after apply)
+ versioning (known after apply)
+ website (known after apply)
}
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_s3_bucket.test: Creating...
╷
│ Error: listing tags for S3 (Simple Storage) Bucket (terraform-emulator-test): operation error S3 Control: ListTagsForResource, https response error StatusCode: 0, RequestID: , HostID: , request send failed, Get "https://000000000000.127.0.0.1:9297/v20180820/tags/arn%3Aaws%3As3%3A%3A%3Aterraform-emulator-test": dial tcp: lookup 000000000000.127.0.0.1: no such host
│
│ with aws_s3_bucket.test,
│ on main.tf line 13, in resource "aws_s3_bucket" "test":
│ 13: resource "aws_s3_bucket" "test" {
│
╵何やらエラーがでましたが、実行は流れました。S3のバケットも作成はできています。
# aws s3 ls
2026-11-15 17:08:17 terraform-emulator-testエラー内容を確認
エラーは以下の通りです。
Error: listing tags for S3 (Simple Storage) Bucket (terraform-emulator-test): operation error S3 Control: ListTagsForResource, https response error StatusCode: 0, RequestID: , HostID: , request send failed, Get "https://000000000000.127.0.0.1:9297/v20180820/tags/arn%3Aaws%3As3%3A%3A%3Aterraform-emulator-test": dial tcp: lookup 000000000000.127.0.0.1: no such hosthttps://000000000000.127.0.0.1:9297/
<ACCOUNT_ID>.<endpoint>存在しないホスト000000000000.127.0.0.1へのアクセスをしようとしています。AWSEmulatorは127.0.01の単一エンドポイントしか持っていない?ため、000000000000.127.0.0.1のDNS名前解決に失敗しているようです。
Terraformの内部では、CreateBucketを実行したあとに状態確認(Tag)をしに行こうとして接続できずにエラー出力しているようです。
当然destroyも同じようなエラーはでますが、バケットの削除はできます。
# terraform destroy
aws_s3_bucket.test: Refreshing state... [id=terraform-emulator-test]
╷
│ Error: listing tags for S3 (Simple Storage) Bucket (terraform-emulator-test): operation error S3 Control: ListTagsForResource, https response error StatusCode: 0, RequestID: , HostID: , request send failed, Get "https://000000000000.127.0.0.1:9297/v20180820/tags/arn%3Aaws%3As3%3A%3A%3Aterraform-emulator-test": dial tcp: lookup 000000000000.127.0.0.1: no such host
│
│ with aws_s3_bucket.test,
│ on main.tf line 15, in resource "aws_s3_bucket" "test":
│ 15: resource "aws_s3_bucket" "test" {
│エラーを出さないようにできるか
動くには動きますが、気持ち悪いのでエラーを出さない方法を検証。main.tfに記述したところ、エラーは出なくなりました。
# main.tf
...
provider "aws" {
region = "us-east-1"
skip_credentials_validation = true
skip_requesting_account_id = true
s3_use_path_style = true
}あくまでエラーを出さない設定。本番環境では上記は設定しない。
skip_credentials_validation
認証情報チェック。aws stsから認証確認をする。LocalStackやEmulatorでは不要。
skip_requesting_account_id
AWSアカウントID取得です(sts:GetCallerIdentity → 123456789012)。エラーは、https://000000000000.127.0.0.1:9297/...にアクセスしようとしているので、取得しないようにスキップ。
s3_use_path_style
デフォルトでは「https://bucket-name.s3.amazonaws.com」という名前になるが、この設定を行うと「https://s3.amazonaws.com/bucket-name」となる。今回のEmulator環境では「https://127.0.0.1:9297/bucket-name」となる。
DynamoDB
tfファイルにDynamoDB作成のコードを追加。これまでの検証で、動作するとは思いますが、念のため。
# main.tf
resource "aws_dynamodb_table" "test" {
name = "terraform-ddb"
billing_mode = "PAY_PER_REQUEST"
hash_key = "id"
attribute {
name = "id"
type = "S"
}
}# terraform init
# terraform plan
# terraform apply
...
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.# aws dynamodb list-tables
{
"TableNames": [
"terraform-ddb"
]
}
# terraform destroy
...
Destroy complete! Resources: 2 destroyed.大丈夫っぽいですね。
Lambda
続いてLambdaの作成と実行を検証します。簡単なpythonコードを実行させます。
# mkdir lambda
# cat > lambda/lambda_function.py << 'EOF'
def lambda_handler(event, context):
return {
"statusCode": 200,
"body": "Hello AWS Emulator"
}
EOF
# cd lambda
# zip function.zip lambda_function.py
# cd ..# main.tf
...
resource "aws_iam_role" "lambda_role" {
name = "emulator-lambda-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "lambda.amazonaws.com"
}
}]
})
}
resource "aws_lambda_function" "test" {
function_name = "hello-emulator"
filename = "lambda/function.zip"
source_code_hash = filebase64sha256("lambda/function.zip")
runtime = "python3.12"
handler = "lambda_function.lambda_handler"
role = aws_iam_role.lambda_role.arn
}terraformを実行します。
# terraform init
# terraform plan
# terraform apply
...
aws_iam_role.lambda_role: Creating...
aws_iam_role.lambda_role: Creation complete after 0s [id=emulator-lambda-role]
aws_lambda_function.test: Creating...
aws_lambda_function.test: Creation complete after 5s [id=hello-emulator]
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.通りましたので、lambda経由で実行してみます。
# aws lambda invoke --function-name hello-emulator output.json
{
"StatusCode": 200,
"ExecutedVersion": "$LATEST"
}
# cat output.json
{"statusCode": 200, "body": "Hello AWS Emulator"}Lambda経由でも実行することができました。
まとめ
AWS Emulatorにより、IaC(Terraform)の動作確認もできました。AWSの環境で実行する前にテストするもよし、教育で使用するもよしですね。