Tạo AWS Fargate service

Tạo AWS Fargate service

  1. Khởi tạo FargateService trong ProductsServiceStack contructor với id là ProductsService
  FargateService fargateService = new FargateService(this, "ProductsService",
                FargateServiceProps.builder()
                        .build());

Architect

  1. Thêm service name là ProductsService
.serviceName("ProductsService")

Architect

  1. Tiếp theo chúng ta cần thêm cluster đã tạo trước đó để tạo fargate service
  .cluster(productsServiceProps.cluster())

Architect

  1. Ngoài cluster, chúng ta cần có task definition để tạo fragate. Chúng ta sẽ dùng fargateTaskDefinition đã tạo bên trên
  .taskDefinition(fargateTaskDefinition)

Architect

  1. Tiếp theo chúng ta sẽ khai báo bao nhiêu instance chúng ta muốn sử dụng. Trong workshop này chúng ta sẽ dử dụng 2
  .desiredCount(2)

Architect

  1. Tiếp theo để giữ fargate trong private subnet nối bên ngoài thông qua NAT Gateway chúng ta sẻ không thêm public IP
   .assignPublicIp(false)

Architect

  1. Ngoài ra nếu trong phần tạo PVC bạn không tạo NAT Gateway, bạn có thể làm như sau
   .assignPublicIp(true)

Đừng làm trong môi trường production

Architect

  1. ECS service của chúng ta cần permission để pull image từ ECR repository. Vì vậy chúng ta cần phải cấp quyền như sau
productsServiceProps.repository().grantPull(Objects.requireNonNull(fargateTaskDefinition.getExecutionRole()));

Architect

  1. Cuối cùng 1 đều quan trọng nữa là chúng ta cần định nghĩa là service của chúng ta chấp nhận requests từ http port 8081
fargateService.getConnections().getSecurityGroups().get(0).addIngressRule(Peer.anyIpv4(), Port.tcp(8081));

Architect