Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AutomationD committed Oct 1, 2020
0 parents commit cf7b257
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.terraform
*.tfstate*
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020-present HazelOps OÜ https://hazelops.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test:
@echo "TBD: Linter"
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# AWS ECS Nginx Terraform Module

TBD
64 changes: 64 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
data "aws_region" "current" {}

locals {
secret_names = concat(var.secret_names, [
"PASSWORD"
])

environment = merge(var.environment,
{
ECS_FARGATE = var.ecs_launch_type == "FARGATE" ? "true" : "false"
}
)

container_definition = {
name = var.name
image = "${var.docker_image_name}:${var.docker_image_tag}",
memoryReservation = var.docker_memory_reservation,
essential = true,
resourceRequirements = var.resource_requirements

environment = [for k, v in local.environment : { name = k, value = v }]
secrets = module.ssm.secrets

portMappings = [{
containerPort = var.docker_container_port,
// In case of bridge an host use a dynamic port (0)
hostPort = var.ecs_network_mode == "awsvpc" ? var.docker_container_port : 0
}]

// This is used to make sure the app container has started before starting proxy (for nginx config to be copied to a volume and for port reachibility)
dependsOn = [{
containerName = var.app_name,
condition = "START"
}],

// This is used to map nginx config template from a volume (which can be created by the original app container)
mountPoints = var.enabled ? [
{
sourceVolume = "nginx-templates",
containerPath = "/etc/nginx/templates/"
}
] : []

logConfiguration = var.cloudwatch_log_group == "" ? {
logDriver = "json-file"
options = {}
} : {
logDriver = "awslogs",
options = {
awslogs-group = var.cloudwatch_log_group
awslogs-region = data.aws_region.current.name
awslogs-stream-prefix = var.name
}
}
}
}

module "ssm" {
source = "hazelops/ssm-secrets/aws"
version = "~> 1.0"
env = var.env
app_name = var.app_name
names = var.enabled ? local.secret_names : []
}
3 changes: 3 additions & 0 deletions output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "container_definition" {
value = local.container_definition
}
63 changes: 63 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
variable "env" {
}

variable "name" {
default = "nginx"
}

variable "app_name" {
type = string
}


variable "environment" {
type = map(string)
default = {}
}

variable "secret_names" {
type = list(string)
default = []
}

//variable "ecs_cluster" {
// type = string
//}

variable "docker_image_name" {
type = string
default = "nginx"
}

variable "docker_image_tag" {
type = string
default = "1.19.2-alpine"
}

variable "ecs_launch_type" {

}

variable "cloudwatch_log_group" {
default = ""
}

variable "docker_container_port" {
default = 80
}

variable "ecs_network_mode" {
}

variable "resource_requirements" {
default = []
}

variable "docker_memory_reservation" {
default = 128
}


variable "enabled" {
default = true
}
3 changes: 3 additions & 0 deletions versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform {
required_version = "~> 0.12.0"
}

1 comment on commit cf7b257

@AutomationD
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.