Skip to content

Commit

Permalink
Merge pull request #229 from nimblehq/release/2.1.0
Browse files Browse the repository at this point in the history
Release - 2.1.0
  • Loading branch information
hoangmirs authored Sep 29, 2023
2 parents 5537995 + 0f5df0d commit f969f44
Show file tree
Hide file tree
Showing 36 changed files with 100 additions and 80 deletions.
9 changes: 7 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ yarn.lock
.DS_Store
tsconfig.tsbuildinfo

# IDE
.vscode
# IDEA
.idea
*.iml

# Vscode
.vscode

# Emacs
.dir-locals.el
6 changes: 3 additions & 3 deletions src/generators/addons/aws/modules/alb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const albModuleContent = dedent`
source = "../modules/alb"
vpc_id = module.vpc.vpc_id
namespace = var.namespace
env_namespace = local.env_namespace
app_port = var.app_port
subnet_ids = module.vpc.public_subnet_ids
security_group_ids = module.security_group.alb_security_group_ids
Expand All @@ -49,12 +49,12 @@ const albOutputsContent = dedent`

const albSGMainContent = dedent`
resource "aws_security_group" "alb" {
name = "\${var.namespace}-alb-sg"
name = "\${var.env_namespace}-alb-sg"
description = "ALB Security Group"
vpc_id = var.vpc_id
tags = {
Name = "\${var.namespace}-alb-sg"
Name = "\${var.env_namespace}-alb-sg"
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/generators/addons/aws/modules/bastion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const bastionModuleContent = dedent`
subnet_ids = module.vpc.public_subnet_ids
instance_security_group_ids = module.security_group.bastion_security_group_ids
namespace = var.namespace
env_namespace = local.env_namespace
image_id = var.bastion_image_id
instance_type = var.bastion_instance_type
Expand All @@ -61,12 +61,12 @@ const bastionModuleContent = dedent`

const bastionSGMainContent = dedent`
resource "aws_security_group" "bastion" {
name = "\${var.namespace}-bastion"
name = "\${var.env_namespace}-bastion"
description = "Bastion Security Group"
vpc_id = var.vpc_id
tags = {
Name = "\${var.namespace}-bastion-sg"
Name = "\${var.env_namespace}-bastion-sg"
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/generators/addons/aws/modules/cloudwatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const cloudwatchModuleContent = dedent`
module "cloudwatch" {
source = "../modules/cloudwatch"
namespace = var.namespace
env_namespace = local.env_namespace
log_retention_in_days = var.cloudwatch_log_retention_in_days
}`;
Expand Down
2 changes: 1 addition & 1 deletion src/generators/addons/aws/modules/core/securityGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const securityGroupModuleContent = dedent`
module "security_group" {
source = "../modules/security_group"
namespace = var.namespace
env_namespace = local.env_namespace
vpc_id = module.vpc.vpc_id
app_port = var.app_port
private_subnets_cidr_blocks = module.vpc.private_subnets_cidr_blocks
Expand Down
2 changes: 1 addition & 1 deletion src/generators/addons/aws/modules/core/vpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const vpcModuleContent = dedent`
module "vpc" {
source = "../modules/vpc"
namespace = var.namespace
env_namespace = local.env_namespace
}`;

const applyAwsVpc = async (options: AwsOptions) => {
Expand Down
4 changes: 2 additions & 2 deletions src/generators/addons/aws/modules/ecr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const ecrModuleContent = dedent`
module "ecr" {
source = "../modules/ecr"
namespace = var.namespace
image_limit = var.image_limit
env_namespace = local.env_namespace
image_limit = var.image_limit
}`;

const applyAwsEcr = async (options: AwsOptions) => {
Expand Down
6 changes: 3 additions & 3 deletions src/generators/addons/aws/modules/ecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const ecsModuleContent = dedent`
source = "../modules/ecs"
subnets = module.vpc.private_subnet_ids
namespace = var.namespace
env_namespace = local.env_namespace
region = var.region
app_host = module.alb.alb_dns_name
app_port = var.app_port
Expand Down Expand Up @@ -110,12 +110,12 @@ const ecsModuleContent = dedent`

const ecsSGMainContent = dedent`
resource "aws_security_group" "ecs_fargate" {
name = "\${var.namespace}-ecs-fargate-sg"
name = "\${var.env_namespace}-ecs-fargate-sg"
description = "ECS Fargate Security Group"
vpc_id = var.vpc_id
tags = {
Name = "\${var.namespace}-ecs-fargate-sg"
Name = "\${var.env_namespace}-ecs-fargate-sg"
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/generators/addons/aws/modules/rds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const rdsModuleContent = dedent`
module "rds" {
source = "../modules/rds"
namespace = var.namespace
env_namespace = local.env_namespace
vpc_security_group_ids = module.security_group.rds_security_group_ids
vpc_id = module.vpc.vpc_id
Expand All @@ -70,12 +70,12 @@ const rdsModuleContent = dedent`

const rdsSGMainContent = dedent`
resource "aws_security_group" "rds" {
name = "\${var.namespace}-rds"
name = "\${var.env_namespace}-rds"
description = "RDS Security Group"
vpc_id = var.vpc_id
tags = {
Name = "\${var.namespace}-rds-sg"
Name = "\${var.env_namespace}-rds-sg"
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/generators/addons/aws/modules/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const s3ModuleContent = dedent`
module "s3" {
source = "../modules/s3"
namespace = var.namespace
env_namespace = local.env_namespace
}`;

const applyAwsS3 = async (options: AwsOptions) => {
Expand Down
2 changes: 1 addition & 1 deletion src/generators/addons/aws/modules/ssm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const ssmModuleContent = dedent`
module "ssm" {
source = "../modules/ssm"
namespace = var.namespace
env_namespace = local.env_namespace
secrets = {
database_url = "postgres://\${var.rds_username}:\${var.rds_password}@\${module.rds.db_endpoint}/\${var.rds_database_name}"
Expand Down
17 changes: 16 additions & 1 deletion src/generators/terraform/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import { dedent } from 'ts-dedent';

import { GeneralOptions } from '@/commands/generate';
import { copy, rename } from '@/helpers/file';
import {
INFRA_CORE_MAIN_PATH,
INFRA_SHARED_MAIN_PATH,
} from '@/generators/terraform/constants';
import { copy, rename, appendToFile } from '@/helpers/file';

const applyTerraformCore = async (generalOptions: GeneralOptions) => {
const { projectName } = generalOptions;

copy('terraform/', '.', projectName);

// Use projectName to append the Namespace local in the main.tf file
const coreLocalsContent = dedent`
locals {
env_namespace = "${projectName}-\${var.environment}"
}`;

appendToFile(INFRA_CORE_MAIN_PATH, coreLocalsContent, projectName);
appendToFile(INFRA_SHARED_MAIN_PATH, coreLocalsContent, projectName);

// Need to rename .gitignore to gitignore because NPN package doesn't include .gitignore
// https://github.com/npm/npm/issues/3763
rename('gitignore', '.gitignore', projectName);
Expand Down
2 changes: 1 addition & 1 deletion templates/addons/aws/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ First, edit the `*-infra` source code:
module "ssm" {
source = "../modules/ssm"
namespace = var.namespace
env_namespace = local.env_namespace
secrets = {
secret_key_base = var.secret_key_base,
Expand Down
6 changes: 3 additions & 3 deletions templates/addons/aws/modules/alb/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ locals {

# tfsec:ignore:aws-elb-alb-not-public
resource "aws_lb" "main" {
name = "${var.namespace}-alb"
name = "${var.env_namespace}-alb"
internal = false
subnets = var.subnet_ids
load_balancer_type = "application"
Expand All @@ -14,13 +14,13 @@ resource "aws_lb" "main" {
drop_invalid_header_fields = true

access_logs {
bucket = "${var.namespace}-alb-log"
bucket = "${var.env_namespace}-alb-log"
enabled = true
}
}

resource "aws_lb_target_group" "target_group" {
name = "${var.namespace}-alb-tg"
name = "${var.env_namespace}-alb-tg"
port = var.app_port
protocol = "HTTP"
vpc_id = var.vpc_id
Expand Down
4 changes: 2 additions & 2 deletions templates/addons/aws/modules/alb/variables.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
variable "namespace" {
description = "The namespace for the LB"
variable "env_namespace" {
description = "The namespace with environment for the LB"
type = string
}

Expand Down
8 changes: 4 additions & 4 deletions templates/addons/aws/modules/bastion/main.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# tfsec:ignore:aws-ec2-no-public-ip
resource "aws_launch_configuration" "bastion_instance" {
name_prefix = "${var.namespace}-bastion-"
name_prefix = "${var.env_namespace}-bastion-"
image_id = var.image_id
instance_type = var.instance_type
key_name = "${var.namespace}-bastion"
key_name = "${var.env_namespace}-bastion"
security_groups = var.instance_security_group_ids
associate_public_ip_address = true

Expand All @@ -21,7 +21,7 @@ resource "aws_launch_configuration" "bastion_instance" {
}

resource "aws_autoscaling_group" "bastion_instance" {
name = "${var.namespace}-bastion"
name = "${var.env_namespace}-bastion"
launch_configuration = aws_launch_configuration.bastion_instance.name
min_size = var.min_instance_count
max_size = var.max_instance_count
Expand All @@ -30,7 +30,7 @@ resource "aws_autoscaling_group" "bastion_instance" {

tag {
key = "Name"
value = "${var.namespace}-bastion"
value = "${var.env_namespace}-bastion"
propagate_at_launch = true
}
}
4 changes: 2 additions & 2 deletions templates/addons/aws/modules/bastion/variables.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
variable "namespace" {
description = "The namespace for the bastion instance"
variable "env_namespace" {
description = "The namespace with environment for the bastion instance"
type = string
}

Expand Down
2 changes: 1 addition & 1 deletion templates/addons/aws/modules/cloudwatch/main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# tfsec:ignore:aws-cloudwatch-log-group-customer-key
resource "aws_cloudwatch_log_group" "main" {
name = "awslogs-${var.namespace}-log-group"
name = "awslogs-${var.env_namespace}-log-group"
retention_in_days = var.log_retention_in_days
}
4 changes: 2 additions & 2 deletions templates/addons/aws/modules/cloudwatch/variables.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
variable "namespace" {
description = "The namespace for the CloudWatch"
variable "env_namespace" {
description = "The namespace with environment for the CloudWatch"
type = string
}

Expand Down
2 changes: 1 addition & 1 deletion templates/addons/aws/modules/ecr/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# tfsec:ignore:aws-ecr-enforce-immutable-repository tfsec:ignore:aws-ecr-repository-customer-key
resource "aws_ecr_repository" "main" {
name = var.namespace
name = var.env_namespace

image_scanning_configuration {
scan_on_push = true
Expand Down
4 changes: 2 additions & 2 deletions templates/addons/aws/modules/ecr/variables.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
variable "namespace" {
description = "The namespace for the ECR"
variable "env_namespace" {
description = "The namespace with environment for the ECR"
type = string
}

Expand Down
20 changes: 10 additions & 10 deletions templates/addons/aws/modules/ecs/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ locals {
])

container_vars = {
namespace = var.namespace
env_namespace = var.env_namespace
region = var.region
app_host = var.app_host
app_port = var.app_port
Expand Down Expand Up @@ -98,18 +98,18 @@ data "aws_ecs_task_definition" "task" {
}

resource "aws_iam_policy" "ecs_task_execution_ssm" {
name = "${var.namespace}-ECSTaskExecutionAccessSSMPolicy"
name = "${var.env_namespace}-ECSTaskExecutionAccessSSMPolicy"
policy = local.ecs_task_execution_ssm_policy
}

# tfsec:ignore:aws-iam-no-policy-wildcards
resource "aws_iam_policy" "ecs_task_excution_service_scaling" {
name = "${var.namespace}-ECSAutoScalingPolicy"
name = "${var.env_namespace}-ECSAutoScalingPolicy"
policy = local.ecs_service_scaling_policy
}

resource "aws_iam_role" "ecs_task_execution_role" {
name = "${var.namespace}-ecs-execution-role"
name = "${var.env_namespace}-ecs-execution-role"
assume_role_policy = local.ecs_task_execution_assume_role_policy
}

Expand All @@ -129,7 +129,7 @@ resource "aws_iam_role_policy_attachment" "ecs_task_excution_service_scaling_pol
}

resource "aws_ecs_cluster" "main" {
name = "${var.namespace}-ecs-cluster"
name = "${var.env_namespace}-ecs-cluster"

setting {
name = "containerInsights"
Expand All @@ -138,7 +138,7 @@ resource "aws_ecs_cluster" "main" {
}

resource "aws_ecs_task_definition" "main" {
family = "${var.namespace}-service"
family = "${var.env_namespace}-service"
cpu = var.cpu
memory = var.memory
network_mode = "awsvpc"
Expand All @@ -148,7 +148,7 @@ resource "aws_ecs_task_definition" "main" {
}

resource "aws_ecs_service" "main" {
name = "${var.namespace}-ecs-service"
name = "${var.env_namespace}-ecs-service"
cluster = aws_ecs_cluster.main.id
launch_type = "FARGATE"
deployment_maximum_percent = var.deployment_maximum_percent
Expand All @@ -168,7 +168,7 @@ resource "aws_ecs_service" "main" {

load_balancer {
target_group_arn = var.alb_target_group_arn
container_name = var.namespace
container_name = var.env_namespace
container_port = var.app_port
}

Expand All @@ -187,7 +187,7 @@ resource "aws_appautoscaling_target" "main" {
}

resource "aws_appautoscaling_policy" "memory_policy" {
name = "${var.namespace}-appautoscaling-memory-policy"
name = "${var.env_namespace}-appautoscaling-memory-policy"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.main.resource_id
scalable_dimension = aws_appautoscaling_target.main.scalable_dimension
Expand All @@ -206,7 +206,7 @@ resource "aws_appautoscaling_policy" "memory_policy" {
}

resource "aws_appautoscaling_policy" "cpu_policy" {
name = "${var.namespace}-appautoscaling-cpu-policy"
name = "${var.env_namespace}-appautoscaling-cpu-policy"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.main.resource_id
scalable_dimension = aws_appautoscaling_target.main.scalable_dimension
Expand Down
Loading

0 comments on commit f969f44

Please sign in to comment.