Skip to content

Commit

Permalink
Merge pull request #1 from silmarasilva/prototipo
Browse files Browse the repository at this point in the history
feat:protótipo inicial
  • Loading branch information
lgfa29 authored Oct 21, 2023
2 parents 4fc1f88 + 451b3e4 commit 0c689a4
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 27 deletions.
131 changes: 119 additions & 12 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,18 +1,125 @@
data "hashicups_coffees" "all" {}
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.12.0"
}
}
}

locals {
coffee_name_id_map = { for coffee in data.hashicups_coffees.all.coffees : coffee.name => coffee.id }
provider "aws" {
# Configuration options
}

resource "hashicups_order" "order" {
dynamic "items" {
for_each = var.order
content {
coffee {
id = local.coffee_name_id_map[items.key]
}
quantity = items.value
}
module "rede_prototipo" {
source = "./modules/rede"
vpc_cidr = "10.1.0.0/16"
subnets = {
primaria = "10.1.1.0/24",
secundaria = "10.1.2.0/24",
}
}

resource "aws_ecs_cluster" "cluster" {
name = "mentoria-teste"
}

resource "aws_ecs_cluster_capacity_providers" "example" {
cluster_name = aws_ecs_cluster.cluster.name

capacity_providers = ["FARGATE"]

default_capacity_provider_strategy {
base = 1
weight = 100
capacity_provider = "FARGATE"
}
}
resource "aws_ecs_task_definition" "service" {
family = "sample-fargate"
memory = 512
cpu = 256
requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"
container_definitions = <<EOF
[
{
"name": "fargate-app",
"image": "public.ecr.aws/docker/library/httpd:latest",
"portMappings": [
{
"containerPort": 80,
"hostPort": 80,
"protocol": "tcp"
}
],
"essential": true,
"entryPoint": [
"sh",
"-c"
],
"command": [
"/bin/sh -c \"echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground\""
]
}
]
EOF
}

resource "aws_ecs_service" "apache" {
name = "apache"
cluster = aws_ecs_cluster.cluster.id
task_definition = aws_ecs_task_definition.service.arn
desired_count = 1
launch_type = "FARGATE"
network_configuration {
subnets = [module.rede_prototipo.subnet_id.primaria]
assign_public_ip = true
security_groups = [module.rede_prototipo.security_group_id]
}
}


resource "aws_ecs_task_definition" "service2" {
family = "sample-fargate"
memory = 512
cpu = 256
requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"
container_definitions = <<EOF
[
{
"name": "fargate-app",
"image": "public.ecr.aws/docker/library/httpd:latest",
"portMappings": [
{
"containerPort": 80,
"hostPort": 80,
"protocol": "tcp"
}
],
"essential": true,
"entryPoint": [
"sh",
"-c"
],
"command": [
"/bin/sh -c \"echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground\""
]
}
]
EOF
}

resource "aws_ecs_service" "apache2" {
name = "apache2"
cluster = aws_ecs_cluster.cluster.id
task_definition = aws_ecs_task_definition.service2.arn
desired_count = 1
launch_type = "FARGATE"
network_configuration {
subnets = [module.rede_prototipo.subnet_id.secundaria]
assign_public_ip = true
security_groups = [module.rede_prototipo.security_group_id]
}
}
46 changes: 46 additions & 0 deletions modules/rede/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
}

resource "aws_route" "internet_gateway" {
route_table_id = aws_vpc.main.default_route_table_id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}

resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.main.id
}

resource "aws_subnet" "ecs" {
for_each = var.subnets
vpc_id = aws_vpc.main.id
cidr_block = each.value
}

resource "aws_security_group" "ecs" {
name = "ecs-teste"
vpc_id = aws_vpc.main.id

ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
}
7 changes: 7 additions & 0 deletions modules/rede/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "subnet_id" {
value = { for k, v in aws_subnet.ecs : k => v.id }
}

output "security_group_id" {
value = aws_security_group.ecs.id
}
10 changes: 10 additions & 0 deletions modules/rede/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
variable "vpc_cidr" {
description = "Bloco cidr da VPC"
default = "10.0.0.0/16"
type = string
}

variable "subnets" {
type = map(string)
default = {}
}
4 changes: 1 addition & 3 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
output "order_id" {
value = resource.hashicups_order.order.id
}

4 changes: 0 additions & 4 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +0,0 @@
variable "order" {
description = "Mapa de cafe e quantidade"
type = map(number)
}
9 changes: 1 addition & 8 deletions versions.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
terraform {
required_version = ">= 1.0.0"
required_providers {
hashicups = {
version = "~> 0.3.1"
}
}
}


0 comments on commit 0c689a4

Please sign in to comment.