> For the complete documentation index, see [llms.txt](https://bradsen.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bradsen.gitbook.io/docs/dev-sec-ops.md).

# DEV-SEC-OPS

## AWS Infrastructure Deployment Architecture (Terraform, Ansible, and AWS Secrets Manager)

<figure><img src="https://1174946829-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F4K3GhEEELaFCyhvEQ0Wp%2Fuploads%2F4RUeVjNdK9jBzCbRkp3f%2FPlanning.png?alt=media&amp;token=f60ceb7f-4864-47d9-bb5e-8b3c98ce61c8" alt=""><figcaption></figcaption></figure>

## Prerequisites

Before deploying the infrastructure, install and configure the following tools.

### 1. Create an IAM User and IAM Role

In AWS IAM, create an IAM user (Terra-user) with permissions to provision AWS infrastructure using Terraform

User:&#x20;

```
Terra-user
```

Group:

```
Terraform-group
```

Permissions:

```
AmazonDynamoDBFullAccess
AmazonEC2FullAccess
AmazonRDSFullAccess
AmazonRoute53FullAccess
AmazonS3FullAccess
IAMFullAccess
```

Expected output

![](https://1174946829-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F4K3GhEEELaFCyhvEQ0Wp%2Fuploads%2FWtEyATx3uH6PGzAxkJrX%2Funknown.png?alt=media\&token=18e0c7a3-c674-44d2-8877-be1779110e68)

![](https://1174946829-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F4K3GhEEELaFCyhvEQ0Wp%2Fuploads%2Fmvy974cJCCWzPwA7G2y7%2Funknown.png?alt=media\&token=6c8f0f76-9024-4c6c-8706-79ef88b3d3be)

![](https://1174946829-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F4K3GhEEELaFCyhvEQ0Wp%2Fuploads%2FEVl0m7Ys9xPt9TgllgYD%2Funknown.png?alt=media\&token=c01b7fdd-cb51-4252-a7e6-66247c976f3f)

After creating the IAM user, generate an Access Key ID and Secret Access Key. These credentials will be used when configuring the AWS CLI.

![](https://1174946829-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F4K3GhEEELaFCyhvEQ0Wp%2Fuploads%2F5Fo1e4FMotTKCFjVZYZ2%2Funknown.png?alt=media\&token=39aad162-05c7-42f8-8c22-3d836513eba6)

### 2. Install AWS CLI

The AWS CLI allows Terraform to authenticate with your AWS account.

#### **Installation**

Refer to the AWS CLI installation guide.

<https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html>

#### **Configure AWS Credentials**

Run:

```
aws configure
```

Provide the following values:

* AWS Access Key ID
* AWS Secret Access Key
* Default Region (e.g. `ap-southeast-1`)
* Default Output Format (`json`)

Verify the configuration:

```
aws sts get-caller-identity
```

### 3. Install Terraform

Terraform provisions the AWS infrastructure required for this project.

#### Installation

<https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli>

Verify

```
terraform version
```

#### Project Structure

```
📁 DevSecOps Project
├── 📁 Boostrap
│ ├── 📄 iam.tf
│ ├── 📄 provider.tf
├── 📄 main.tf
├── 📄 output.tf
├── 📄 secrets.tf
├── 📄 security_group.tf
├── 📄 terraform_tfvars.tf
├── 📄 variables.tf
├── 📄 ec2.tf
├── 📄 inventory.tf
└── 📄 inventory.tpl
```

#### iam.tf&#x20;

Creates an IAM role that can be assumed by the Terra-user IAM user and grants the role full administrative access by attaching the AdministratorAccess managed policy.&#x20;

This enables Terraform to provision and manage AWS infrastructure using temporary credentials obtained through role assumption.&#x20;

**Note**: Update the **Principal ARN** to use your own AWS account ID and IAM username.&#x20;

```
resource "aws_iam_role" "terraform_admin_role" {
  name = "terraform-admin-role"

  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Effect = "Allow"

      Principal = {
        AWS = "arn:aws:iam::<your-aws-account-id>:user/Terra-user"
      }

      Action = "sts:AssumeRole"
    }]
  })
}

resource "aws_iam_role_policy_attachment" "admin" {
  role       = aws_iam_role.terraform_admin_role.name
  policy_arn = "arn:aws:iam::aws:policy/AdministratorAccess"
}
```

#### **provider.tf**

Specify the required AWS provider and its version, ensuring compatibility with the project. It also configures the AWS provider to deploy and manage resources in the ap-southeast-2 (Sydney) region.&#x20;

```
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = "ap-southeast-2"
}
```

#### **main.tf**

Specify the AWS region using the aws\_region variable and configures Terraform to assume the terraform-admin-role using AWS Security Token Service (STS). By assuming this role, Terraform obtains temporary credentials to provision and manage AWS resources securely

**Note**: Update the **Principal ARN** to use your own AWS account ID and IAM username.&#x20;

```
provider "aws" {
  region = var.aws_region

  assume_role {
    role_arn     = "arn:aws:iam::<your-aws-account-id>:role/terraform-admin-role"
    session_name = "terraform-session"
  }
}
```

#### **output.tf**

This output displays the public IP address of the EC2 instance after the infrastructure has been deployed. The output can be used to connect to the instance via SSH or as input for subsequent configuration tasks, such as Ansible.

```
output "instance_public_ip" {
  description = "Public IP of the EC2 instance"
  value       = aws_instance.main.public_ip
}
```

#### **secrets.tf**

This creates an AWS Secrets Manager secret named dev/application to securely store application secrets, such as passwords, API keys, or other sensitive configuration values.&#x20;

```
resource "aws_secretsmanager_secret" "application" {
  name = "dev/application"
  description = "Application secrets"
}
```

#### security\_group.tf

This configuration creates a security group for the EC2 instance that allows inbound SSH (port 22) access only from the IP address specified by the ssh\_ip variable. It also permits all outbound traffic, enabling the instance to communicate with external services such as software repositories and AWS services.

```
resource "aws_security_group" "ec2_sg" {

  name        = "ec2-ssh-security-group"
  description = "Allow SSH access to EC2 instance"
  vpc_id      = var.vpc_id

  ingress {
    description = "Allow SSH from my IP"
    from_port = 22
    to_port   = 22
    protocol = "tcp"
    cidr_blocks = [
      var.ssh_ip
    ]
  }
  
  egress {
    description = "Allow all outbound traffic"
    from_port = 0
    to_port   = 0
    protocol = "-1"
    cidr_blocks = [
      "0.0.0.0/0"
    ]
  }
}
```

#### terraform\_tfvars.tf

This file defines the input variables used by the Terraform configuration. It specifies deployment settings such as the AWS region, VPC and subnet IDs, EC2 instance details, SSH configuration, storage settings, and GitHub username.&#x20;

```
aws_region = "ap-southeast-2"

vpc_id    = "<your-VPC-id>"
subnet_id = "<your-subnet-id>"

ami_id        = "ami-0f5d1713c9af4fe30"
instance_type = "t2.medium"

key_name = "b"

ssh_ip = "<your-own-IP>/32"

root_volume_size = 20
root_volume_type = "gp3"

associate_public_ip = true

ssh_user         = "ubuntu"
private_key_path = "<your-own-private-key>"

github_username = "<your-own-github-username>"
```

#### variables.tf

This configuration defines the input variables required to deploy the infrastructure. These variables specify configurable settings such as the AWS region, networking details, EC2 instance configuration, SSH access, storage options, and GitHub username, allowing the infrastructure to be customized without modifying the Terraform code.

```
variable "aws_region" {
  description = "AWS region where resources will be deployed"
  type        = string
}

variable "vpc_id" {
  description = "VPC ID where the EC2 instance will be created"
  type        = string
}

variable "subnet_id" {
  description = "Subnet ID where the EC2 instance will be launched"
  type        = string
}

variable "ami_id" {
  description = "AMI ID used to launch the EC2 instance"
  type        = string
}

variable "instance_type" {
  description = "EC2 instance type"
  type        = string
}

variable "key_name" {
  description = "AWS key pair name for SSH access"
  type        = string
}

variable "ssh_ip" {
  description = "Allowed public IP address for SSH access in CIDR format"
  type        = string
}

variable "associate_public_ip" {
  description = "Whether to assign a public IP to the instance"
  type        = bool
  default     = true
}

variable "root_volume_size" {
  description = "Root EBS volume size in GB"
  type        = number
  default     = 8
}

variable "root_volume_type" {
  description = "Root EBS volume type"
  type        = string
  default     = "gp3"
}

variable "ssh_user" {
  description = "SSH username for Ansible"
  type        = string
  default     = "ubuntu"
}

variable "private_key_path" {
  description = "Private key path for SSH"
  type        = string
}

variable "github_username" {
  description = "GitHub username"
  type        = string
}
```

#### ec2.tf

This configuration provisions an EC2 instance using the specified AMI, instance type, subnet, and SSH key pair. It associates the instance with the configured security group, assigns a public IP address if enabled, creates an encrypted root EBS volume, and applies a name tag for easy identification.

```
resource "aws_instance" "main" {

  ami = var.ami_id
  instance_type = var.instance_type
  subnet_id = var.subnet_id

  vpc_security_group_ids = [
    aws_security_group.ec2_sg.id
  ]

  key_name = var.key_name
  associate_public_ip_address = var.associate_public_ip
  
  root_block_device {
    volume_size = var.root_volume_size
    volume_type = var.root_volume_type
    encrypted = true
    delete_on_termination = true
  }
  
  tags = {
    Name = "terraform-ec2-instance"
  }
}
```

#### inventory.tf

This configuration automatically generates an Ansible inventory file using the EC2 instance's public IP address and SSH details. Once the instance is provisioned, it waits for the instance to become available, updates the SSH key permissions, and executes the Ansible playbook to configure the server automatically.

```
resource "local_file" "ansible_inventory" {
  # Dynamically points to the private key within the Terraform module folder
  content = templatefile("${path.module}/inventory.tpl", {
    public_ip = aws_instance.main.public_ip
    ssh_user  = var.ssh_user
    key_path  = "${path.module}/${var.private_key_path}" 
  })

  filename = "${path.module}/../Ansible/inventory.ini"

  depends_on = [aws_instance.main]
}

resource "null_resource" "run_ansible" {
  depends_on = [
    aws_instance.main,
    local_file.ansible_inventory
  ]

  triggers = {
    inventory_content_hash = md5(local_file.ansible_inventory.content)
  }

  provisioner "local-exec" {
    interpreter = ["/bin/bash", "-c"]

    # ONE CONTINUOUS LINE: Joined by '&&' (if one fails, it stops; if it succeeds, it proceeds)
    command = "sleep 90 && chmod 400 ${path.module}/${var.private_key_path} && /usr/bin/ansible-playbook -i ${path.module}/../Ansible/inventory.ini ${path.module}/../Ansible/playbook.yml"

    environment = {
      AWS_PROFILE = "terraform-admin"
      ANSIBLE_HOST_KEY_CHECKING = "False"
      ANSIBLE_SSH_ARGS          = "-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
    }
  }
}
```

#### inventory.tpl

This Ansible inventory template dynamically generates an inventory file using the EC2 instance's public IP address, SSH username, and private key path provided by Terraform.&#x20;

This enables Ansible to establish an SSH connection to the newly provisioned EC2 instance for automated configuration.

```
[ec2]
${public_ip} ansible_user=${ssh_user} ansible_ssh_private_key_file=${key_path}
```

### 4. Install Ansible

Ansible configures the EC2 instance after Terraform has provisioned it.

#### Installation

<https://docs.ansible.com/projects/ansible/latest/installation_guide/installation_distros.html>

#### Project Structure

```
📁Ansible
├── 📄 playbook.yml
```

Verify the installation:

```
ansible --version
```

#### playbook.yml

This Ansible playbook automates the configuration of the EC2 application server after it has been provisioned by Terraform.&#x20;

It securely retrieves application secrets from AWS Secrets Manager, installs the required software packages, configures the MySQL database, clones the application source code from a private GitHub repository, installs the Node.js runtime and dependencies, and prepares the server for application deployment.&#x20;

Fork the repo from **Aditya Jaiswal** to your own repo.

<https://github.com/jaiswaladi246/3-Tier-User-Platform-Project.git>

```
---
- name: Configure EC2 application server
 hosts: ec2
 become: yes

 vars:
   aws_secret: "{{ lookup('amazon.aws.aws_secret', 'dev/application') | from_json }}"

   github_username: "{{ aws_secret.github_username }}"
   github_token: "{{ aws_secret.github_token }}"
   mysql_user: "{{ aws_secret.mysql_username }}"
   mysql_password: "{{ aws_secret.mysql_password }}"

   app_dir: "/home/ubuntu/3-Tier-User-Platform-Project"
   app_branch: "main"
   mysql_db: "test_db"
   
   repo: "https://{{ github_username }}:{{ github_token }}@github.com/0xff0xfe/3-Tier-User-Platform-Project.git"

 tasks:
   - name: Update apt cache
     apt:
       update_cache: yes

   - name: Install required packages
     apt:
       name:
         - git
         - curl
         - mysql-server
         - python3-pymysql
       state: present

   - name: Start and enable MySQL
     service:
       name: mysql
       state: started
       enabled: yes
       
   - name: Clone private GitHub repo
     git:
       repo: "{{ repo }}"
       dest: "{{ app_dir }}"
       version: "{{ app_branch }}"
       force: yes
     become: no
     #no_log: true

   - name: Install NVM
     shell: |
      curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
     args:
       executable: /bin/bash
       creates: "/home/ubuntu/.nvm/nvm.sh"
     become: no

   - name: Install Node.js LTS
     shell: |
      export NVM_DIR="/home/ubuntu/.nvm"
      . "$NVM_DIR/nvm.sh"
      nvm install --lts
      nvm use --lts
      nvm alias default 'lts/*'
     args:
       executable: /bin/bash
     become: no

   - name: Install global npm tools
     shell: |
      export NVM_DIR="/home/ubuntu/.nvm"
      . "$NVM_DIR/nvm.sh"
      npm install -g nodemon
     args:
       executable: /bin/bash
     become: no

   - name: Create MySQL database
     mysql_db:
       name: "{{ mysql_db }}"
       state: present
       login_unix_socket: /var/run/mysqld/mysqld.sock

   - name: Create MySQL user
     mysql_user:
       name: "{{ mysql_user }}"
       password: "{{ mysql_password }}"
       priv: "{{ mysql_db }}.*:ALL"
       host: localhost
       plugin: mysql_native_password
       state: present
       login_unix_socket: /var/run/mysqld/mysqld.sock
     no_log: true

   - name: Create users table
     mysql_query:
       login_db: "{{ mysql_db }}"
       login_unix_socket: /var/run/mysqld/mysqld.sock
       query: |
        CREATE TABLE IF NOT EXISTS users (
          id INT AUTO_INCREMENT PRIMARY KEY,
          name VARCHAR(255) NOT NULL,
          email VARCHAR(255) NOT NULL UNIQUE,
          role ENUM('Admin', 'User') NOT NULL
        );
```

## Deploying the Environment

Navigate to the Terraform **bootstrap** directory:

```
cd bootstrap
```

Initialize Terraform:

```
terraform init
```

Review the execution plan:

```
terraform plan
```

Deploy the infrastructure:

```
terraform apply
```

### Create AWS profile to assume roles

```
nano ~/.aws/config 
```

In the file, put the following configuration

```
[profile terraform-admin]
role_arn = arn:aws:iam::<your-aws-account-id>:role/terraform-admin-role
source_profile = default
region = ap-southeast-2
```

Verifies that the terraform-admin AWS CLI profile is configured correctly and can successfully authenticate with AWS.&#x20;

It will return the AWS account ID, IAM user or assumed role ARN, and user ID associated with the current credentials, confirming that Terraform will use the intended identity when provisioning resources.

```
 aws sts get-caller-identity --profile terraform-admin
```

Temporarily comment out **inventory.tf** to prevent the Ansible configuration from executing, as the required secrets have not yet been created in AWS Secrets Manager.&#x20;

Then run the **terraform init**, **terraform plan**, and **terraform apply** commands.

```
terraform init
terraform plan
terraform apply
```

This AWS CLI command stores a JSON-formatted set of application secrets in the **`dev/application`** secret within AWS Secrets Manager. These secrets include the GitHub username and personal access token, as well as the MySQL username and password, which are securely retrieved by the Ansible playbook during server configuration.

```
aws secretsmanager put-secret-value \ --profile terraform-admin \ --secret-id dev/application \ --secret-string ' { "github_username":"<your-github-username>", "github_token":"<your-github-token>", "mysql_username":"<your-mysql-username>", "mysql_password":"<your-mysql-password>" }'
```

Uncomment **inventory.tf**, then run **terraform plan** and **terraform apply** to execute the Ansible configuration.

```
terraform plan
terraform apply
```

Once the deployment is complete, the EC2 instance should be provisioned and configured according to the defined Terraform and Ansible configuration.&#x20;

To reduce AWS costs, run **terraform destroy** to remove the provisioned resources when they are no longer needed. This is particularly useful when using the environment for learning or occasional testing.&#x20;

Recreate the infrastructure at any time by running terraform apply.

```
terraform destroy
```

<br>
