[P5] Construcs: Hiểu Và Sử Dụng AWS CDK L1 Constructs cơ bản
AWS Cloud Development Kit (CDK) là một framework mạnh mẽ cho phép bạn định nghĩa infrastructure dưới dạng code. Trong bài viết này, chúng ta sẽ tìm hiểu chi tiết về L1 constructs - các building block cơ bản nhất của AWS CDK, thông qua việc xây dựng một VPC infrastructure hoàn chỉnh.
L1 constructs là các component cấp thấp nhất trong CDK, mapping 1-1 với các CloudFormation resources. Chúng có một số đặc điểm quan trọng:
Mọi L1 construct đều được khởi tạo với 3 thành phần chính:
Ví dụ cơ bản:
my_vpc = ec2.CfnVPC(
self, # scope
"MyVpc", # id
cidr_block="10.0.0.0/16" # props
)
mkdir sample-app
cdk init app --language python
# sample_app_stack.py
from aws_cdk import aws_ec2 as ec2
my_vpc = ec2.CfnVPC(
self,
"MyVpc",
cidr_block="10.0.0.0/16",
enable_dns_hostnames=True,
enable_dns_support=True
)
# sample_app_stack.py
my_internet_gateway = ec2.CfnInternetGateway(self, "MyInternetGateway")
# Attach Internet Gateway vào VPC
ec2.CfnVPCGatewayAttachment(
self,
"MyVPCGatewayAttachment",
vpc_id=my_vpc.attr_vpc_id,
internet_gateway_id=my_internet_gateway.attr_internet_gateway_id,
)
# sample_app_stack.py
# Định nghĩa cấu trúc subnets
subnet_config = [
{"cidrMask": 24, "name": "Ingress 1", "public": True},
{"cidrMask": 24, "name": "Ingress 2", "public": True},
{"cidrMask": 24, "name": "Application 2", "public": False},
{"cidrMask": 24, "name": "Application 3", "public": False},
]
for index, config in enumerate(subnet_config):
subnet_resource = ec2.CfnSubnet(
self,
f"MySubnet{index+1}",
vpc_id=my_vpc.attr_vpc_id,
cidr_block=f"10.0.{index}.0/{config['cidrMask']}",
availability_zone=Stack.availability_zones.fget(self)[index % 2],
map_public_ip_on_launch=config["public"],
)
# create route table
route_table = ec2.CfnRouteTable(
self, f"MyRouteTable{index}", vpc_id=my_vpc.attr_vpc_id
)
# Associate route table with subnet
ec2.CfnSubnetRouteTableAssociation(
self,
f"MySubnetRouteTableAssociation{index}",
route_table_id=route_table.attr_route_table_id,
subnet_id=subnet_resource.attr_subnet_id,
)
# If subnet is public, create route to internet gateway
if config["public"]:
ec2.CfnRoute(
self,
f"MyRoute{index}",
route_table_id=route_table.attr_route_table_id,
destination_cidr_block="0.0.0.0/0",
gateway_id=my_internet_gateway.attr_internet_gateway_id,
)
Chỉ cần thực hiện bước này một lần để thiết lập môi trường CDK. Nếu bạn đã làm điều này, bạn có thể bỏ qua bước này.
aws configure
npm install -g aws-cdk
Bước này sẽ tạo các resources cần thiết để CDK có thể deploy stack
cdk bootstrap
Tạo CloudFormation template
cdk synth
Triển khai VPC infrastructure
cdk deploy
cdk destroy
Sử Dụng CDK Construct Library Reference:
Attribute References:
attr_
prefix để access resource attributesmy_vpc.attr_vpc_id
để lấy VPC IDBest Practices:
Deployment và Testing:
cdk synth
để kiểm tra template trước khi deploycdk deploy
để triển khai infrastructureL1 constructs phù hợp khi:
L1 constructs là nền tảng của AWS CDK, cho phép bạn định nghĩa AWS resources với sự linh hoạt cao. Mặc dù chúng yêu cầu nhiều configuration thủ công, việc kết hợp với các tính năng của ngôn ngữ lập trình như Python có thể giúp code của bạn clean và maintainable hơn nhiều so với CloudFormation templates truyền thống.
Trong các bài học tiếp theo, chúng ta sẽ khám phá L2 constructs - cung cấp nhiều abstraction và defaults hợp lý hơn theo AWS best practices.