id: nat-gateway-use
info:
  name: Managed NAT Gateway Usage
  author: princechaddha
  severity: medium
  description: |
    Ensure the use of Amazon Managed NAT Gateway for better availability and bandwidth in VPC networks instead of self-managed NAT instances.
  impact: |
    Using self-managed NAT instances can lead to single points of failure and potential bandwidth bottlenecks.
  remediation: |
    Replace NAT instances with Amazon Managed NAT Gateway to ensure high availability and scalability in your VPC network.
  reference:
    - https://docs.aws.amazon.com/vpc/latest/userguide/vpc-nat-gateway.html
  tags: cloud,devops,aws,amazon,vpc,aws-cloud-config

variables:
  region: "us-east-1"

flow: |
  code(1)
  for(let VPCIds of template.vpcid){
    set("vpc", VPCIds)
    code(2)
  }

self-contained: true
code:
  - engine:
      - sh
      - bash
    source: |
      aws ec2 describe-vpcs --region $region --query 'Vpcs[*].VpcId' --output json

    extractors:
      - type: json
        name: vpcid
        internal: true
        json:
          - '.[]'

  - engine:
      - sh
      - bash
    source: |
         aws ec2 describe-nat-gateways --region $region --filter "Name=vpc-id,Values=$vpc" "Name=state,Values=available" --query 'NatGateways'

    matchers:
      - type: word
        words:
          - "[]"

    extractors:
      - type: dsl
        dsl:
          - 'vpc + " VPC is not using Managed NAT Gateways"'
# digest: 4a0a00473045022100c0877797c85620a3b1a36e36908c96c828bcd0dc31d1b46eadfbad0cb57887cb0220322e780cb1d41741e04ff1fa0dfd2d35efe10a367c625bf3dfa3ef63c1297eb8:922c64590222798bb761d5b6d8e72950