Working Less, Automating More: Simplifying AWS Management with Terraform and GitHub Actions
Tags: devops, github, aws, terraform
Published: 2024-04-13 02:39:45



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jl16no7t3h0xcfeoexos.png)

## 📌Prerequisites:

- AWS [account](https://aws.amazon.com/free/)
- [VsCode](https://code.visualstudio.com/download)

## 🎯Workflow

1️⃣Create IAM user👤
2️⃣Save🔒 Acces & Secret 
3️⃣Key Pair for ssh🔑
4️⃣Terraform Pipeline📝
5️⃣Job from Github Actions📝

---
### 1️⃣Create IAM user👤

- Login AWS 
- IAM -> Users -> **Create user**

>_Name of user whatever as you wish_

- Select **Attach policies directly** -> Select **AmazonEC2FullAccess**

_It's not❌ the best practice, but it's simple for this purpose_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/atgiy0pu90wa8if2bnfu.png)

User created successfully✅

<u>Create Credentials</u>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10te4wc0g83rzvsjzcrr.png)
- **Create Access Key** -> **Third-party service**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h1ludf49956vflp0n2ym.png)


### 2️⃣Save🔒 Acces & Secret 
- Go to Github repository
- Settings -> Secrets & Variables -> Actions
- Save as a **Secret**

`AWS_ACCESS_KEY_ID` `TF_USER_AWS_SECRET`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nmyzqchfyvpes0v105s3.png)

### 3️⃣Create Key Pair for ssh

- Just go here and name it.Is simple

>_The key will download⬇ automatically_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vezsj0239fpafayvd36.png)

### 4️⃣Terraform Pipeline📝

Create a new directory in the root [project](https://github.com/Sharker3312/Node-App)
🔰Name: terraform

>_This is a very simple pipeline. Remember this is a tutorial. I will soon make📍 a Terraform series._

```yml
provider "aws" {
  region = "us-east-2" #Region as you wish
}

resource "aws_instance" "nodeapp" {
  ami           = "ami-00cda30cf72311684" #Check the AMI list for free tier
  instance_type = "t2.micro" #Well... very intuitive -_-
  key_name = "ec2_key" #Name of key pair created in step 3
  vpc_security_group_ids = [aws_security_group.nodeapp.id] # ID security group
     

  tags = {
    Name = "Nodeapp"
  }
}

#Security Group
resource "aws_security_group" "nodeapp" {
  name        = "ec2_ecurity-group" #Whatever name
  description = "Allow inbound traffic on port 8080 and SSH" #The app expose port  8080
  

  
  
  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
  
  ingress {
    from_port   = 8080
    to_port     = 8080
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}
```

### 5️⃣Job from Github Actions📝

```bash
name: Deploy Node app 

on:
  push:
    branches:
      - master

jobs:
  tf-aws:
    name: Deploy Node app to AWS
    runs-on: ubuntu-latest

    defaults:
      run:
        working-directory: terraform #Path directory

    steps:

      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: us-east-2

      - name: Setup Terraform CLI
        uses: hashicorp/setup-terraform@v3

      - name: Terraform Init
        run: terraform init

      - name: Terraform Validate
        run: terraform validate

      - name: Terraform Plan
        run: terraform plan 

      - name: Terraform Apply
        run: terraform apply -auto-approve 
```

---
🎬 Run the job

![job done](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qr2mw28qe2iforh4dg6t.png)
✅

![ec2_nodeapp](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ipj6aoheimiz0ksb8cd2.png)



![security group](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i0wj8xm7yaoi3d93adzj.png)

---
<a href="https://linkedin.com/in/lesterdprez" rel="nofollow"><img src="https://camo.githubusercontent.com/d94940866c98cb4fca5783c4e8ac95776d2f52df6bbf3d5ab9e30d76836f30ae/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c696e6b6564496e2d2532333030373742352e7376673f6c6f676f3d6c696e6b6564696e266c6f676f436f6c6f723d7768697465" alt="LinkedIn" data-canonical-src="https://img.shields.io/badge/LinkedIn-%230077B5.svg?logo=linkedin&amp;logoColor=white" style="max-width: 100%;"></a>