/aws-ec2-terraform

Deploy EC2 instance with Terraform

Primary LanguageHCLApache License 2.0Apache-2.0

aws-ec2-terraform

Deploy EC2 instance with Terraform

Setup for macOS

Make sure you do this setup first:

  1. Setup macOS for AWS Cloud DevOps

  2. AWS Authentication

  3. Install Terraform via Homebrew:

    # install
    brew tap hashicorp/tap
    brew install hashicorp/tap/terraform
    
    # verify
    terraform -help

Development

Configure the project:

source configure.sh

Format configuration:

terraform fmt

Validate configuration:

terraform validate

Deploy

Preview infrastructure:

terraform plan -out terraform.plan

Create infrastructure:

terraform apply terraform.plan

Check state:

terraform show

Test Deployment

Check that you can browse the nginx default site:

instance_public_ip=$(terraform output -json | jq -r '.instance_public_ip.value')
open http://$instance_public_ip

Connect to instance via SSH:

key="aws-ec2-key"
instance_public_ip=$(terraform output -json | jq -r '.instance_public_ip.value')
ssh -i ~/.ssh/$key ec2-user@$instance_public_ip

Cleanup

Review the changes and verify the execution plan:

terraform plan -destroy

Delete the resources:

terraform destroy

How to create a new project

# create main.tf
cat << 'EOF' > main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }

  required_version = ">= 1.2.0"
}

# AWS config
# leave empty to read the settings from the AWS CLI configuration
provider "aws" {}
EOF

# initialize the project
# this will download provider plugin into .terraform subdir
terraform init