Terraform remote state - Initializing S3 as remote backend.

  1. The S3 bucket is created.

     resource "aws_s3_bucket" "s3-tf-bucket" {
       bucket = "abhijit-tf-test-bucket-for-remote-state"
     tags = {
         Name        = "My tf bucket"
         Environment = "test"
       }
     }
    
  2. kindly run terraform plan command.

  1. Kindly run terraform apply command.

  2. the bucket is created on AWS.

  3. create backend.tf file and set backend state as S3 instead of storing state file locally.
    Note: use the same bucket name which was created in 1st step.

     terraform {
       backend "s3" {
         bucket = "abhijit-tf-test-bucket-for-remote-state"
         key    = "key/terraform-state-info"
         region = "us-east-1"
       }
     }
    
  4. After creating backend.tf file kindly run terraform init command to set backend to S3.

  5. Create EC2 instance using terraform and run terraform plan.

  1. Kindly apply the changes using terraform apply command. EC2 instance will be created.

  2. Verify resource in AWS console. Instance id i-08c5aa3de41fa245b is created.

  3. Verify terraform state file is created and updated in S3.

  4. Kindly update tag info for the EC2 instance and run terraform plan to view the changes.

  5. kindly run terraform apply command to apply the changes and then verify the changes in console.

  6. Tag info is updated for our AWS EC2 instance id. environment = Test and project as Payments.

  7. Kindly verify the terraform state file. it should contain all the updated changes which we did using above terraform commands. Alternatively, you can run terraform state pull command which will show all the latest changes which has been done on the terraform state file.
    sample output is listed below.

     terraform state pull
     {
       "version": 4,
       "terraform_version": "1.5.4",
       "serial": 2,
       "lineage": "95d2c431-3152-0ac8-a90c-4e56e2b209a2",
       "outputs": {},
       "resources": [
         {
           "mode": "managed",
           "type": "aws_instance",
           "name": "ec2_tf",
           "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
           "instances": [
             {
               "schema_version": 1,
               "attributes": {
                 "ami": "ami-0dbc3d7bc646e8516",
                 "arn": "arn:aws:ec2:us-east-1:562044960159:instance/i-08c5aa3de41fa245b",
                 "associate_public_ip_address": true,
                 "availability_zone": "us-east-1d",
                 "capacity_reservation_specification": [
                   {
                     "capacity_reservation_preference": "open",
                     "capacity_reservation_target": []
                   }
                 ],
                 "cpu_core_count": 1,
                 "cpu_options": [
                   {
                     "amd_sev_snp": "",
                     "core_count": 1,
                     "threads_per_core": 1
                   }
                 ],
                 "cpu_threads_per_core": 1,
                 "credit_specification": [
                   {
                     "cpu_credits": "standard"
                   }
                 ],
                 "disable_api_stop": false,
                 "disable_api_termination": false,
                 "ebs_block_device": [],
                 "ebs_optimized": false,
                 "enclave_options": [
                   {
                     "enabled": false
                   }
                 ],
                 "ephemeral_block_device": [],
                 "get_password_data": false,
                 "hibernation": false,
                 "host_id": "",
                 "host_resource_group_arn": null,
                 "iam_instance_profile": "",
                 "id": "i-08c5aa3de41fa245b",
                 "instance_initiated_shutdown_behavior": "stop",
                 "instance_lifecycle": "",
                 "instance_market_options": [],
                 "instance_state": "running",
                 "instance_type": "t2.micro",
                 "ipv6_address_count": 0,
                 "ipv6_addresses": [],
                 "key_name": "",
                 "launch_template": [],
                 "maintenance_options": [
                   {
                     "auto_recovery": "default"
                   }
                 ],
                 "metadata_options": [
                   {
                     "http_endpoint": "enabled",
                     "http_protocol_ipv6": "disabled",
                     "http_put_response_hop_limit": 2,
                     "http_tokens": "required",
                     "instance_metadata_tags": "disabled"
                   }
                 ],
                 "monitoring": false,
                 "network_interface": [],
                 "outpost_arn": "",
                 "password_data": "",
                 "placement_group": "",
                 "placement_partition_number": 0,
                 "primary_network_interface_id": "eni-042c88b85604493f9",
                 "private_dns": "ip-172-31-38-2.ec2.internal",
                 "private_dns_name_options": [
                   {
                     "enable_resource_name_dns_a_record": false,
                     "enable_resource_name_dns_aaaa_record": false,
                     "hostname_type": "ip-name"
                   }
                 ],
                 "private_ip": "172.31.38.2",
                 "public_dns": "ec2-54-210-4-83.compute-1.amazonaws.com",
                 "public_ip": "54.210.4.83",
                 "root_block_device": [
                   {
                     "delete_on_termination": true,
                     "device_name": "/dev/xvda",
                     "encrypted": false,
                     "iops": 3000,
                     "kms_key_id": "",
                     "tags": {},
                     "throughput": 125,
                     "volume_id": "vol-01edae1228ecf58f3",
                     "volume_size": 8,
                     "volume_type": "gp3"
                   }
                 ],
                 "secondary_private_ips": [],
                 "security_groups": [
                   "default"
                 ],
                 "source_dest_check": true,
                 "spot_instance_request_id": "",
                 "subnet_id": "subnet-0eeea463222a2f0d3",
                 "tags": {
                   "environment": "Test",
                   "project": "Payments"
                 },
                 "tags_all": {
                   "environment": "Test",
                   "project": "Payments"
                 },
                 "tenancy": "default",
                 "timeouts": null,
                 "user_data": null,
                 "user_data_base64": null,
                 "user_data_replace_on_change": false,
                 "volume_tags": null,
                 "vpc_security_group_ids": [
                   "sg-01d680e1f1ba92a4c"
                 ]
               },
               "sensitive_attributes": [],
               "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjo2MDAwMDAwMDAwMDAsImRlbGV0ZSI6MTIwMDAwMDAwMDAwMCwidXBkYXRlIjo2MDAwMDAwMDAwMDB9LCJzY2hlbWFfdmVyc2lvbiI6IjEifQ=="
             }
           ]
         }
       ],
       "check_results": null
     }