cdk-workshop-02
cdk-workshop-02
python
cdk-workshop-02/cdk_workshop_02_stack.py
, thêm các thư viện này vào trong tệp tinfrom aws_cdk import (
Stack,
aws_ecs as ecs,
aws_ecs_patterns as ecsp,
aws_apigateway as apigateway
)
from constructs import Construct
cdk-workshop-02/cdk_workshop_02_stack.py
, khai báo một cụm ECS với Cân bằng tải ứng dụng (ALB) trong hàm __init__()
# Declare a Load Balancer Fargate
lb_fargate_service = ecsp.ApplicationLoadBalancedFargateService(
self,
"Workshop02-MyWebServer",
task_image_options=ecsp.ApplicationLoadBalancedTaskImageOptions(
image=ecs.ContainerImage.from_registry("nginxdemos/hello")
),
public_load_balancer=True,
desired_count=3
)
ecsp.ApplicationLoadBalancedFargateService
sẽ tạo ra các tài nguyên như sau:
nginxdemos/hello
từ Dockerhub# Define API Gateway
api = apigateway.RestApi(self, "ProxyToLBECS")
# Get the DNS value of the Application Load Balancer
lb = lb_fargate_service.load_balancer
lb_dns = lb_fargate_service.load_balancer.load_balancer_dns_name
# Add resource and method for proxy request
proxy = api.root.add_resource("ecs")
proxy.add_method("GET", apigateway.HttpIntegration(f"http://{lb_dns}"))
Bạn có thể đọc thêm về các phương thức và thuộc tính của ApplicationLoadBalancedFargateService tại the official CDK documentation of AWS (Python)
cdk-workshop-02/cdk_workshop_02_stack.py
from aws_cdk import (
Stack,
aws_ecs as ecs,
aws_ecs_patterns as ecsp,
aws_apigateway as apigateway
)
from constructs import Construct
class CdkWorkshop02Stack(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
# Declare a Load Balancer Fargate
lb_fargate_service = ecsp.ApplicationLoadBalancedFargateService(
self,
"Workshop02-MyWebServer",
task_image_options=ecsp.ApplicationLoadBalancedTaskImageOptions(
image=ecs.ContainerImage.from_registry("nginxdemos/hello")),
public_load_balancer=True,
desired_count=3
)
# Define API Gateway
api = apigateway.RestApi(self, "ProxyToLBECS")
# Get the DNS value of the Application Load Balancer
lb = lb_fargate_service.load_balancer
lb_dns = lb_fargate_service.load_balancer.load_balancer_dns_name
# Add resource and method for proxy request
proxy = api.root.add_resource("ecs")
proxy.add_method("GET", apigateway.HttpIntegration(f"http://{lb_dns}"))
Trước khi mà chúng ta có thể triển khai lên Cloud, thì
cdk-workshop-02
, vì máy chủ của chúng ta đang có 2 phiên bản python, nên chúng ta cần phải cô lập các thư viện python ở mỗi phiên bản ra để tránh bị nhầm lẫn giữa các thư viện python.requirements.txt
virtualenv
/home/ec2-user
pip3.9 install virtualenv
cdk-workshop-02
venv
venv/bin/activate
virtualenv venv
source venv/bin/activate
requirements.txt
pip install -r requirements.txt
cdk diff
cdk boostrap
cdk deploy
Nhấn y
để tiếp tục
Truy cập vào đường link của API gateway (ProxyToLBECSEndpoint
), thêm path /ecs
. Bạn sẽ truy cập được vào web server được triển khai trong ECS.
Để kiểm tra sự hoạt động của Application Load Balancer, nhấn làm mới trình duyệt một vài lần, bạn sẽ thấy đích đến của trang web thay đổi. Ở phần trước, chúng ta đặt tham số desired_count=3, nên sẽ có 3 container được triển khai song song với nhau đằng sau ALB. Bạn sẽ thấy sự thay đổi ở Server address qua các lần làm mới trình duyệt.
Nếu bạn đã chạy được đến phần này, chúc mừng bạn đã hoàn thành phần đầu tiên của bài lab CDK cơ bản 2. Ở phần tiếp theo, chúng ta sẽ sử dụng CDK để triển khai một hàm Lambda đằng sau 1 resource của API Gateway.