Bootstrap FreeKB - Amazon Web Services (AWS) - Create an EC2 instance using Terraform
Amazon Web Services (AWS) - Create an EC2 instance using Terraform


Let's say you have the following files on your Terraform server.

├── required_providers.tf
├── amazon_machine_images (directory)
│   ├── data.tf
│   ├── outputs.tf
│   ├── provider.tf
├── ec2_instances (directory)
│   ├── provider.tf
│   ├── remote_state.tf
│   ├── resources.tf
├── key_pairs (directory)
│   ├── keys.tf
│   ├── outputs.tf
│   ├── provider.tf
├── virtual_private_clouds (directory)
│   ├── data.tf
│   ├── outputs.tf
│   ├── resources.tf
│   ├── provider.tf

 

required_providers.tf will almost always have this.

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

 

Let's say provider.tf has the following. In this example, the "default" profile in /home/username/.aws/config and /home/username/.aws/credentials is being used. This assumes you have setup Terraform as described in Amazon Web Services (AWS) - Getting Started with Terraform.

provider "aws" {
  alias   = "default"
  profile = "default"
  region  = "default"
}

 

This assumes you are familiar with how to get output variables using terraform_remote_state. In this example, the ec2_instances directory will get:

resources.tf in the ec2_instances directory could have something like this, to create a t3.micro EC2 instance using the amazon_machine_image ID.

resource "aws_instance" "my-instance" {
  ami           = data.terraform_remote_state.amazon_machine_images.outputs.image.id
  instance_type = "t3.micro"
  subnet_id     = data.terraform_remote_state.virtual_private_clouds.outputs.my_subnets.ids[0]
  key_name      = data.terraform_remote_state.key_pairs.outputs.my_key.key_name

  tags = {
    Name = "my-instance"
  }
}

 

The terraform plan command can be used to see what Terraform will try to do.

~]# terraform plan
Terraform will perform the following actions:

  # aws_instance.my-instance will be created
  + resource "aws_instance" "my-instance" {
      + ami                                  = "ami-123456789abcdefg"
      + arn                                  = (known after apply)
      + associate_public_ip_address          = (known after apply)
      + availability_zone                    = (known after apply)
      + cpu_core_count                       = (known after apply)
      + cpu_threads_per_core                 = (known after apply)
      + disable_api_stop                     = (known after apply)
      + disable_api_termination              = (known after apply)
      + ebs_optimized                        = (known after apply)
      + get_password_data                    = false
      + host_id                              = (known after apply)
      + host_resource_group_arn              = (known after apply)
      + iam_instance_profile                 = (known after apply)
      + id                                   = (known after apply)
      + instance_initiated_shutdown_behavior = (known after apply)
      + instance_lifecycle                   = (known after apply)
      + instance_state                       = (known after apply)
      + instance_type                        = "t3.micro"
      + ipv6_address_count                   = (known after apply)
      + ipv6_addresses                       = (known after apply)
      + key_name                             = (known after apply)
      + monitoring                           = (known after apply)
      + outpost_arn                          = (known after apply)
      + password_data                        = (known after apply)
      + placement_group                      = (known after apply)
      + placement_partition_number           = (known after apply)
      + primary_network_interface_id         = (known after apply)
      + private_dns                          = (known after apply)
      + private_ip                           = (known after apply)
      + public_dns                           = (known after apply)
      + public_ip                            = (known after apply)
      + secondary_private_ips                = (known after apply)
      + security_groups                      = (known after apply)
      + source_dest_check                    = true
      + spot_instance_request_id             = (known after apply)
      + subnet_id                            = "subnet-00a2efcf89006a953"
      + tenancy                              = (known after apply)
      + user_data                            = (known after apply)
      + user_data_base64                     = (known after apply)
      + user_data_replace_on_change          = false
      + vpc_security_group_ids               = (known after apply)
    }

 

The terraform apply command can be used to create, update or delete the EC2 instance.

terraform apply

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 9a8391 in the box below so that we can be sure you are a human.