Skip to content

Instantly share code, notes, and snippets.

@m1g0r
Created February 17, 2020 19:06
Show Gist options
  • Save m1g0r/be0db734c4db599618cd60e29dfbaa7b to your computer and use it in GitHub Desktop.
Save m1g0r/be0db734c4db599618cd60e29dfbaa7b to your computer and use it in GitHub Desktop.
## AWS docker repository
resource "aws_ecr_repository" "example_app" {
name = "example-app"
}
## example ecs task definition
resource "aws_ecs_task_definition" "example_app" {
family = "example-app"
container_definitions = <<DEFINITION
[
{
"essential": true,
"image": "${aws_ecr_repository.example_app.repository_url}:latest",
"memory": 512,
"name": "example-app",
"environment": [
{
"name": "NODE_ENV",
"value": "production"
}
],
"portMappings": [
{
"containerPort": 8080
}
],
"dockerLabels": {
"name": "example-app"
},
"logConfiguration": {
"logDriver": "json-file"
}
}
]
DEFINITION
lifecycle {
ignore_changes = ["container_definitions"]
}
}
## ECS service
resource "aws_ecs_service" "example_app" {
name = "example-app"
cluster = "${aws_ecs_cluster.your_ecs_cluster.id}" // set your ECS cluster
task_definition = "${aws_ecs_task_definition.example_app.family}"
desired_count = "1"
deployment_minimum_healthy_percent = 100
deployment_maximum_percent = 200
ordered_placement_strategy {
type = "spread"
field = "instanceId"
}
load_balancer {
target_group_arn = "${aws_alb_target_group.example_app.arn}"
container_name = "example-app"
container_port = 8080
}
lifecycle {
ignore_changes = ["task_definition"]
}
}
## ALB target group
resource "aws_alb_target_group" "example_app" {
name = "example-app"
port = 8080
protocol = "HTTP"
vpc_id = "${aws_vpc.main.id}" // Set your VPC ID
deregistration_delay = 30
health_check {
path = "/"
timeout = 3
matcher = "200"
}
depends_on = ["aws_alb.example_app"]
}
## ALB
resource "aws_alb" "example_app" {
name = "example-app"
internal = false
subnets = ["${split(",", var.ecs["subnet_ids"])}"] // Set your subnets ID to vars
security_groups = ["${split(",", var.ecs["security_group_ids"])}"] // Set your security groups ID to vars
}
## ALB listener
resource "aws_alb_listener" "example_app" {
load_balancer_arn = "${aws_alb.example_app.id}"
port = "80"
protocol = "HTTP"
default_action {
target_group_arn = "${aws_alb_target_group.example_app.id}"
type = "forward"
}
}
## DNS Route 53
resource "aws_route53_record" "example_app" {
zone_id = "${aws_route53_zone.zonename.zone_id}" // set your DNS Zone name
name = "dnsname.com" // Set your DNS name
type = "A"
alias {
name = "${aws_alb.example_app.dns_name}"
zone_id = "${aws_alb.example_app.zone_id}"
evaluate_target_health = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment