{"id":25436745,"url":"https://github.com/p4irin/explore_terraform_remote_state_on_localstack_s3_bucket","last_synced_at":"2025-05-15T03:10:59.786Z","repository":{"id":277215667,"uuid":"928424402","full_name":"p4irin/explore_terraform_remote_state_on_localstack_s3_bucket","owner":"p4irin","description":"Use an s3 bucket on LocalStack to store Terraform's state remotely","archived":false,"fork":false,"pushed_at":"2025-02-12T18:22:30.000Z","size":3,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-12T19:33:11.485Z","etag":null,"topics":["awscli","explore","localstack","s3-bucket","terraform"],"latest_commit_sha":null,"homepage":"","language":"HCL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/p4irin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-06T16:07:23.000Z","updated_at":"2025-02-12T18:22:33.000Z","dependencies_parsed_at":"2025-02-12T19:33:21.273Z","dependency_job_id":"2ce4e6ef-6c1c-4a4d-9fa4-81f4f5e6f87f","html_url":"https://github.com/p4irin/explore_terraform_remote_state_on_localstack_s3_bucket","commit_stats":null,"previous_names":["p4irin/explore_terraform_remote_state_on_localstack_s3_bucket"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p4irin%2Fexplore_terraform_remote_state_on_localstack_s3_bucket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p4irin%2Fexplore_terraform_remote_state_on_localstack_s3_bucket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p4irin%2Fexplore_terraform_remote_state_on_localstack_s3_bucket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p4irin%2Fexplore_terraform_remote_state_on_localstack_s3_bucket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/p4irin","download_url":"https://codeload.github.com/p4irin/explore_terraform_remote_state_on_localstack_s3_bucket/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254264766,"owners_count":22041794,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["awscli","explore","localstack","s3-bucket","terraform"],"created_at":"2025-02-17T08:22:12.690Z","updated_at":"2025-05-15T03:10:59.635Z","avatar_url":"https://github.com/p4irin.png","language":"HCL","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Explore Terraform remote state on s3 bucket\n\nUse an s3 bucket on LocalStack to store Terraform's state remotely\n\n## Create the s3 bucket using awscli\n\nFirst things first. The bucket must be available for Terraform to use it to store state remotely. The bucket is named `remote-state` and referenced in `terraform.tf`.\n\n```bash\n$ aws --profile localstack s3api create-bucket --bucket remote-state\n{\n    \"Location\": \"/remote-state\"\n}\n```\n\nVerify creation by listing buckets\n\n```bash\n$ aws --profile localstack s3api list-buckets\n{\n    \"Buckets\": [\n        {\n            \"Name\": \"remote-state\",\n            \"CreationDate\": \"2025-02-06T14:52:45.000Z\"\n        }\n    ],\n    \"Owner\": {\n        \"DisplayName\": \"webfile\",\n        \"ID\": \"75aa57f09aa0c8caeab4f8c24e99d10f8e7faeebf76c078efc7c6caea54ba06a\"\n    },\n    \"Prefix\": null\n}\n```\n\n## init, plan and apply the Terraform configuration\n\n### $ terraform init\n\n * The local provider is initialized.\n * The `.terraform` directory and the lock file `.terraform.lock.hcl` are created.\n * The \"s3\" backend is configured\n    * Terraform uses the s3 bucket on LocalStack to store the state file\n\n### $ terraform plan\n\nTerraform shows what it will do\n\n### $ terraform apply\n\nNote the state file `terraform.tfstate` is not created locally!\n\nVerify the state file is created in the `remote-state` s3 bucket by listing the objects in the bucket\n\n```bash\n$ aws --profile localstack s3api list-objects --bucket remote-state\n{\n    \"Contents\": [\n        {\n            \"Key\": \"terraform.tfstate\",\n            \"LastModified\": \"2025-02-06T14:54:33.000Z\",\n            \"ETag\": \"\\\"726aee4a9829b007fd95ecc3bb64f2ad\\\"\",\n            \"ChecksumAlgorithm\": [\n                \"SHA256\"\n            ],\n            \"ChecksumType\": \"FULL_OBJECT\",\n            \"Size\": 1659,\n            \"StorageClass\": \"STANDARD\",\n            \"Owner\": {\n                \"DisplayName\": \"webfile\",\n                \"ID\": \"75aa57f09aa0c8caeab4f8c24e99d10f8e7faeebf76c078efc7c6caea54ba06a\"\n            }\n        }\n    ],\n    \"RequestCharged\": null,\n    \"Prefix\": \"\"\n}\n```\n\nTo get the content of the state file locally in a file `the-remote-state`\n\n```bash\n$ aws --profile localstack s3api get-object --bucket remote-state --key terraform.tfstate  the-remote-state\n{\n    \"AcceptRanges\": \"bytes\",\n    \"LastModified\": \"Thu, 06 Feb 2025 14:54:33 GMT\",\n    \"ContentLength\": 1659,\n    \"ETag\": \"\\\"726aee4a9829b007fd95ecc3bb64f2ad\\\"\",\n    \"ChecksumSHA256\": \"rxzMl3lgaRkM8lrUnmZgodEiqyLeJyIQvS4W67QABQs=\",\n    \"ChecksumType\": \"FULL_OBJECT\",\n    \"ContentType\": \"application/json\",\n    \"ServerSideEncryption\": \"AES256\",\n    \"Metadata\": {}\n}\n```\n\nCheck the file `the-remote-state`\n\n```bash\n$ cat the-remote-state |jq\n{\n  \"version\": 4,\n  \"terraform_version\": \"1.10.5\",\n  \"serial\": 1,\n  \"lineage\": \"97dbb9ab-724b-29ea-30c1-477eed336e8a\",\n  \"outputs\": {},\n  \"resources\": [\n    {\n      \"mode\": \"managed\",\n      \"type\": \"local_file\",\n      \"name\": \"local-file\",\n      \"provider\": \"provider[\\\"registry.terraform.io/hashicorp/local\\\"]\",\n      \"instances\": [\n        {\n          \"schema_version\": 0,\n          \"attributes\": {\n            \"content\": \"This Terraform configuration uses an s3 bucket to store remote state on LocalStack\",\n            \"content_base64\": null,\n            \"content_base64sha256\": \"m77u845yqxXBL28XCxlIjfg5k3bPRzjNyYrR6axV2Jw=\",\n            \"content_base64sha512\": \"gDUKquLdSRZlvFTigVfPdkpx+3VHt6RZBsfwiN6BORuFN0YvagFMOC4Rqp0G0+GpFRb1aLqtklO8xfKq/pu4mA==\",\n            \"content_md5\": \"7f7c263bbcb3eb38ce41cd8d41ecbb6e\",\n            \"content_sha1\": \"76ca4f3a71454d59d89fc4375cef599176fbf4ac\",\n            \"content_sha256\": \"9bbeeef38e72ab15c12f6f170b19488df8399376cf4738cdc98ad1e9ac55d89c\",\n            \"content_sha512\": \"80350aaae2dd491665bc54e28157cf764a71fb7547b7a45906c7f088de81391b8537462f6a014c382e11aa9d06d3e1a91516f568baad9253bcc5f2aafe9bb898\",\n            \"directory_permission\": \"0777\",\n            \"file_permission\": \"0777\",\n            \"filename\": \"local-file\",\n            \"id\": \"76ca4f3a71454d59d89fc4375cef599176fbf4ac\",\n            \"sensitive_content\": null,\n            \"source\": null\n          },\n          \"sensitive_attributes\": [\n            [\n              {\n                \"type\": \"get_attr\",\n                \"value\": \"sensitive_content\"\n              }\n            ]\n          ]\n        }\n      ]\n    }\n  ],\n  \"check_results\": null\n}\n```\n\nCheck the state with Terraform cli\n\n```bash\n$ terraform show\n# local_file.local-file:\nresource \"local_file\" \"local-file\" {\n    content              = \"This Terraform configuration uses an s3 bucket to store remote state on LocalStack\"\n    content_base64sha256 = \"m77u845yqxXBL28XCxlIjfg5k3bPRzjNyYrR6axV2Jw=\"\n    content_base64sha512 = \"gDUKquLdSRZlvFTigVfPdkpx+3VHt6RZBsfwiN6BORuFN0YvagFMOC4Rqp0G0+GpFRb1aLqtklO8xfKq/pu4mA==\"\n    content_md5          = \"7f7c263bbcb3eb38ce41cd8d41ecbb6e\"\n    content_sha1         = \"76ca4f3a71454d59d89fc4375cef599176fbf4ac\"\n    content_sha256       = \"9bbeeef38e72ab15c12f6f170b19488df8399376cf4738cdc98ad1e9ac55d89c\"\n    content_sha512       = \"80350aaae2dd491665bc54e28157cf764a71fb7547b7a45906c7f088de81391b8537462f6a014c382e11aa9d06d3e1a91516f568baad9253bcc5f2aafe9bb898\"\n    directory_permission = \"0777\"\n    file_permission      = \"0777\"\n    filename             = \"local-file\"\n    id                   = \"76ca4f3a71454d59d89fc4375cef599176fbf4ac\"\n}\n```\n\nThe output should be in line with the content of file `the-remote-state`\n\n## References\n\n* [S3](https://docs.localstack.cloud/user-guide/aws/s3/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fp4irin%2Fexplore_terraform_remote_state_on_localstack_s3_bucket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fp4irin%2Fexplore_terraform_remote_state_on_localstack_s3_bucket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fp4irin%2Fexplore_terraform_remote_state_on_localstack_s3_bucket/lists"}