diff --git a/backend.tf b/backend.tf new file mode 100644 index 0000000..7a900c4 --- /dev/null +++ b/backend.tf @@ -0,0 +1,38 @@ +# +# Copyright (c) 2021-present Sonatype, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +terraform { + backend "s3" { + bucket = "vendorcorp-platform-core" + key = "terraform-state/the-cla" + dynamodb_table = "vendorcorp-terraform-state-lock" + region = "us-east-2" + } + required_providers { + aws = { + source = "hashicorp/aws" + version = ">= 4.6.0" + } + kubernetes = { + source = "hashicorp/kubernetes" + version = ">= 2.19.0" + } + postgresql = { + source = "cyrilgdn/postgresql" + version = ">= 1.15.0" + } + } +} \ No newline at end of file diff --git a/locals.tf b/locals.tf new file mode 100644 index 0000000..36c0014 --- /dev/null +++ b/locals.tf @@ -0,0 +1,20 @@ +# +# Copyright (c) 2021-present Sonatype, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +locals { + cla_db_username = "the_cla_bot" + cla_db_name = "the_cla" +} \ No newline at end of file diff --git a/main.tf b/main.tf index dff5c5e..f4a37e7 100644 --- a/main.tf +++ b/main.tf @@ -14,370 +14,209 @@ # limitations under the License. # - -data "aws_vpc" "main" { - tags = { - "use-case" = "shared-vpcs" - "environment" = "ci" - } -} - -data "aws_subnet_ids" "private" { - vpc_id = data.aws_vpc.main.id - - tags = { - access = "private" - } -} - -data "aws_subnet_ids" "public" { - vpc_id = data.aws_vpc.main.id - - tags = { - access = "public" - } -} - -resource "aws_db_subnet_group" "the_cla_rds_subnet_group" { - name = "${var.app_name}-rds-subnet-group" - description = "RDS subnet group" - subnet_ids = data.aws_subnet_ids.private.ids -} - -resource "aws_security_group" "the_cla" { - vpc_id = data.aws_vpc.main.id - name = "${var.app_name}-db-access-sg" - description = "Allow access to RDS" -} - -resource "aws_security_group" "the_cla_rds_sg" { - name = "${var.app_name}-rds-sg" - description = "${var.app_name} Security Group" - vpc_id = data.aws_vpc.main.id - - // allows traffic from the SG itself - ingress { - from_port = 0 - to_port = 0 - protocol = "-1" - self = true - } - - // allow traffic for TCP 5432, on the SG that the ecs service is running on - ingress { - from_port = 5432 - to_port = 5432 - protocol = "tcp" - security_groups = [ - aws_security_group.the_cla.id - ] +# -------------------------------------------------------------------------- +# Create k8s Namespace +# -------------------------------------------------------------------------- +resource "kubernetes_namespace" "the_cla" { + metadata { + name = "the-cla" } +} - // allow traffic from external IP, pgAdmin, etc. - ingress { - from_port = 5432 - to_port = 5432 - protocol = "tcp" - cidr_blocks = [ var.external_db_cidr_group ] +# -------------------------------------------------------------------------- +# Create k8s Secrets +# -------------------------------------------------------------------------- +resource "kubernetes_secret" "the_cla" { + metadata { + name = "the-cla" + namespace = kubernetes_namespace.the_cla.metadata[0].name } - // outbound internet access - egress { - from_port = 0 - to_port = 0 - protocol = "-1" - cidr_blocks = ["0.0.0.0/0"] + data = { + "psql_password" = module.database.user_password } -} -resource "aws_db_instance" "the_cla" { - allocated_storage = 50 - engine = "postgres" - engine_version = "12.5" - instance_class = "db.t3.micro" - identifier = "the-cla" - name = var.postgres_db_name - username = var.postgres_username - password = var.postgres_password - db_subnet_group_name = aws_db_subnet_group.the_cla_rds_subnet_group.id - vpc_security_group_ids = [aws_security_group.the_cla_rds_sg.id] - storage_encrypted = true - skip_final_snapshot = true + type = "Opaque" } -resource "aws_ecr_repository" "the_cla" { - name = "${var.app_name}-app" -} - -resource "aws_ecs_cluster" "the_cla" { - name = "${var.app_name}-cluster" -} - -resource "aws_cloudwatch_log_group" "the_cla" { - name = "${var.app_name}-cloudwatch-lergs" - - tags = { - Application = "${var.app_name}" +# -------------------------------------------------------------------------- +# Create k8s Deployment +# -------------------------------------------------------------------------- +resource "kubernetes_deployment" "the_cla" { + metadata { + name = "the-cla" + namespace = kubernetes_namespace.the_cla.metadata[0].name + labels = { + app = "the-cla" + } } -} + spec { + replicas = 1 -resource "aws_ecs_task_definition" "the_cla" { - family = "${var.app_name}-task" - container_definitions = <