github.com/thiagoyeds/go-cloud@v0.26.0/postgres/awspostgres/main.tf (about)

     1  # Copyright 2018 The Go Cloud Development Kit Authors
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #     https://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  
    15  # Harness for RDS PostgreSQL tests.
    16  
    17  terraform {
    18    required_version = "~>0.12"
    19  }
    20  
    21  provider "aws" {
    22    version = "~> 2.7"
    23    region  = var.region
    24  }
    25  
    26  provider "random" {
    27    version = "~> 2.1"
    28  }
    29  
    30  variable "region" {
    31    type        = string
    32    description = "Region to create resources in. See https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RegionsAndAvailabilityZones.html for valid values."
    33  }
    34  
    35  resource "aws_security_group" "main" {
    36    name_prefix = "testdb"
    37    description = "Security group for the Go CDK Postgres test database."
    38  
    39    ingress {
    40      from_port   = 5432
    41      to_port     = 5432
    42      protocol    = "tcp"
    43      cidr_blocks = ["0.0.0.0/0"]
    44      description = "Public Postgres access"
    45    }
    46  
    47    egress {
    48      from_port   = 0
    49      to_port     = 0
    50      protocol    = "-1"
    51      cidr_blocks = ["0.0.0.0/0"]
    52      description = "All outgoing traffic allowed"
    53    }
    54  }
    55  
    56  resource "random_string" "db_password" {
    57    keepers = {
    58      region = var.region
    59    }
    60  
    61    special = false
    62    length  = 20
    63  }
    64  
    65  resource "aws_db_instance" "main" {
    66    identifier_prefix      = "go-cloud-test"
    67    engine                 = "postgres"
    68    engine_version         = "10.5"
    69    instance_class         = "db.t2.micro"
    70    allocated_storage      = 20
    71    username               = "root"
    72    password               = random_string.db_password.result
    73    name                   = "testdb"
    74    publicly_accessible    = true
    75    vpc_security_group_ids = [aws_security_group.main.id]
    76    skip_final_snapshot    = true
    77    parameter_group_name   = aws_db_parameter_group.main.name
    78  }
    79  
    80  resource "aws_db_parameter_group" "main" {
    81    name_prefix = "go-cloud-test"
    82    family      = "postgres10"
    83  
    84    parameter {
    85      name  = "rds.force_ssl"
    86      value = "1"
    87    }
    88  }
    89  
    90  output "endpoint" {
    91    value       = aws_db_instance.main.endpoint
    92    description = "The RDS instance's host/port."
    93  }
    94  
    95  output "username" {
    96    value       = "root"
    97    description = "The PostgreSQL username to connect with."
    98  }
    99  
   100  output "password" {
   101    value       = random_string.db_password.result
   102    sensitive   = true
   103    description = "The RDS instance password for the user."
   104  }
   105  
   106  output "database" {
   107    value       = "testdb"
   108    description = "The name of the database inside the RDS instance."
   109  }
   110