Skip to content

Instantly share code, notes, and snippets.

@larkintuckerllc
Last active March 20, 2020 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larkintuckerllc/cb2f4670ecb703af55502a8fa32a13ba to your computer and use it in GitHub Desktop.
Save larkintuckerllc/cb2f4670ecb703af55502a8fa32a13ba to your computer and use it in GitHub Desktop.
aws_journey
variable "certificate_arn" {
default = "arn:aws:acm:us-east-1:143287522423:certificate/88006345-e981-4ce7-9b59-f9f4e5000601"
}
variable "execution_role_arn" {
default = "arn:aws:iam::143287522423:role/ecsTaskExecutionRole"
}
variable "image" {
default = "143287522423.dkr.ecr.us-east-1.amazonaws.com/todosrus"
}
variable "task_role_arn" {
default = "arn:aws:iam::143287522423:role/TodosRUsECSTask"
}
provider "aws" {
version = "~> 2.0"
region = "us-east-1"
}
data "aws_route53_zone" "this" {
name = "todosrus.com."
}
data "aws_vpc" "this" {
tags = {
Name = "development"
}
}
data "aws_subnet_ids" "this" {
vpc_id = data.aws_vpc.this.id
}
resource "aws_security_group" "this" {
egress {
cidr_blocks = ["0.0.0.0/0"]
from_port = 0
protocol = "-1"
to_port = 0
}
ingress {
cidr_blocks = ["0.0.0.0/0"]
from_port = 80
protocol = "tcp"
to_port = 80
}
ingress {
cidr_blocks = ["0.0.0.0/0"]
from_port = 443
protocol = "tcp"
to_port = 443
}
vpc_id = data.aws_vpc.this.id
}
resource "aws_lb" "this" {
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.this.id]
subnets = data.aws_subnet_ids.this.ids
}
resource "aws_lb_target_group" "this" {
health_check {
path = "/todos"
}
port = 80
protocol = "HTTP"
target_type = "ip"
vpc_id = data.aws_vpc.this.id
}
resource "aws_lb_listener" "this" {
certificate_arn = var.certificate_arn
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.this.arn
}
load_balancer_arn = aws_lb.this.arn
port = "443"
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-2016-08"
}
resource "aws_route53_record" "this" {
zone_id = data.aws_route53_zone.this.id
name = "api.todosrus.com"
type = "A"
alias {
name = aws_lb.this.dns_name
zone_id = aws_lb.this.zone_id
evaluate_target_health = false
}
}
resource "aws_ecs_cluster" "this" {
name = "todosrus"
}
resource "aws_ecs_task_definition" "this" {
container_definitions = <<EOF
[
{
"cpu": 256,
"environment": [
{
"name": "REGION",
"value": "us-east-1"
}
],
"image": "${var.image}",
"memory": 512,
"name": "todosrus",
"networkMode": "awsvpc",
"portMappings": [
{
"containerPort": 80
}
]
}
]
EOF
cpu = 256
execution_role_arn = var.execution_role_arn
family = "todosrus"
memory = 512
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
task_role_arn = var.task_role_arn
}
resource "aws_ecs_service" "this" {
cluster = aws_ecs_cluster.this.id
depends_on = [aws_lb_listener.this]
desired_count = 3
launch_type = "FARGATE"
load_balancer {
target_group_arn = aws_lb_target_group.this.arn
container_name = "todosrus"
container_port = 80
}
name = "todosrus"
network_configuration {
assign_public_ip = true
security_groups = [aws_security_group.this.id]
subnets = data.aws_subnet_ids.this.ids
}
task_definition = aws_ecs_task_definition.this.arn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment