python을 이용해 간단하게 플라스크 도커 이미지 만들기
[root@ip-10-1-8-104 ecs]# cat Dockerfile
vi Dockerfile
FROM python:3.9-slim
COPY . /app
RUN pip3 install flask
WORKDIR /app
CMD ["python3", "-m", "flask", "run", "--host=0.0.0.0"]
[root@ip-10-1-8-104 ecs]# cat app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_yusa():
return 'hello yusa!!!'
if __name__ == '__main__':
app.run(debug=True)
docker build -t yusapython:latest .
docker tag
docker push ${ECR repo}
ecsTaskExecutionRole이 없다면 생성
1. AmazonECSTaskExecutionRolePolicy 선택
2. 신뢰관계 설정
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "ecs-tasks.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
나는 테라폼이 좋더라...콘솔에서 생성하면 자꾸 오류가 생기고 설정 뭐 잘못한거 있고~
코드로 보면 한번에 보이니까 편해...
# locals
data "aws_iam_role" "ecs_task_execution_role" {
name = "ecsTaskExecutionRole"
}
locals {
vpc_id = data.terraform_remote_state.vpc.outputs.vpc_id
pub_subnet_ids = data.terraform_remote_state.vpc.outputs.public_subnets
priv_subnet_ids = data.terraform_remote_state.vpc.outputs.private_subnets
tags = merge(
data.aws_default_tags.aws_dt.tags,
{ Owner = "yusa" }
)
}
resource "aws_security_group" "lb" {
name = "ecs-test-yusa-alb-sg"
vpc_id = local.vpc_id
ingress {
protocol = "tcp"
from_port = 80
to_port = 80
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_lb" "default" {
name = "ecs-test-yusa-lb"
subnets = local.pub_subnet_ids
security_groups = [aws_security_group.lb.id]
}
resource "aws_lb_target_group" "hello_yusa" {
name = "ecs-test-yusa-target-group"
port = 80
protocol = "HTTP"
vpc_id = local.vpc_id
target_type = "ip"
}
resource "aws_lb_listener" "hello_yusa" {
load_balancer_arn = aws_lb.default.id
port = "80"
protocol = "HTTP"
default_action {
target_group_arn = aws_lb_target_group.hello_yusa.id
type = "forward"
}
}
resource "aws_ecs_task_definition" "hello_yusa" {
family = "hello-yusa-app"
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = 1024
memory = 2048
execution_role_arn = "${data.aws_iam_role.ecs_task_execution_role.arn}" # for Using ECR
container_definitions = <<DEFINITION
[
{
"image": "${계정번호}.dkr.ecr.us-east-1.amazonaws.com/test-ecr:latest",
"cpu": 1024,
"memory": 2048,
"name": "hello-yusa-app",
"networkMode": "awsvpc",
"portMappings": [
{
"containerPort": 5000,
"hostPort": 5000
}
]
}
]
DEFINITION
}
resource "aws_security_group" "hello_yusa_task" {
name = "ecs-test-yusa-task-security-group" # for flask
vpc_id = local.vpc_id
ingress {
protocol = "tcp"
from_port = 5000
to_port = 5000
security_groups = [aws_security_group.lb.id]
}
egress {
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_ecs_cluster" "main" {
name = "ecs-test-yusa-cluster"
}
resource "aws_ecs_service" "hello_yusa" {
name = "hello-yusa-service"
cluster = aws_ecs_cluster.main.id
task_definition = aws_ecs_task_definition.hello_yusa.arn
desired_count = var.app_count
launch_type = "FARGATE"
network_configuration {
security_groups = [aws_security_group.hello_yusa_task.id]
subnets = local.priv_subnet_ids
}
load_balancer {
target_group_arn = aws_lb_target_group.hello_yusa.id
container_name = "hello-yusa-app"
container_port = 5000
}
depends_on = [aws_lb_listener.hello_yusa]
}