-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
161 lines (142 loc) · 5.32 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from aws_cdk import (
aws_ec2 as ec2,
aws_ecs as ecs,
aws_ssm as ssm,
aws_elasticache as ec,
aws_ecs_patterns as ecs_patterns,
aws_elasticloadbalancingv2 as elb,
core,
)
class CosmosBot(core.Stack):
def __init__(self, scope: core.Construct, id: str, env: core.Environment, **kwargs) -> None:
super().__init__(scope, id, env=env, *kwargs)
# Create a /16 VPC with 1 public and private subnet
# CIDR range will be divided evenly among subnets
vpc = ec2.Vpc(
self, 'bot-vpc',
max_azs=2
)
# Create a Redis instance
redis_security_group = ec2.SecurityGroup(
self, 'redis-security-group',
vpc=vpc
)
redis_security_group.add_ingress_rule(
peer=ec2.Peer.ipv4(vpc.vpc_cidr_block),
connection=ec2.Port.tcp(6379),
description='Allow connection from within VPC'
)
redis_subnetgroup = ec.CfnSubnetGroup(
self, 'redis-subnetgroup',
description="Group of private subnets from the VPC",
subnet_ids=[ps.subnet_id for ps in vpc.private_subnets]
)
redis_cluster = ec.CfnCacheCluster(
self, 'redis-cluster',
cache_node_type='cache.t2.small',
engine='redis',
num_cache_nodes=1,
port=6379,
cache_subnet_group_name=redis_subnetgroup.ref,
vpc_security_group_ids=[redis_security_group.security_group_id]
)
redis_cluster.add_depends_on(redis_subnetgroup)
# Create a cluster
cluster = ecs.Cluster(
self, 'bot-cluster',
vpc=vpc
)
# Create a Worker task definition
worker_logging = ecs.AwsLogDriver(stream_prefix='worker')
worker_task = ecs.FargateTaskDefinition(
self, 'worker-task',
cpu=256,
memory_limit_mib=512
)
worker_task.add_container(
id='worker-container',
image=ecs.ContainerImage.from_registry('leonweecs/cosmos-worker:1.1'),
environment={
'REDIS_HOST': redis_cluster.attr_redis_endpoint_address,
'REDIS_PORT': redis_cluster.attr_redis_endpoint_port
},
logging=worker_logging
).add_port_mappings(
ecs.PortMapping(
container_port=80,
host_port=80
)
)
# Create Worker Service
worker_service = ecs_patterns.ApplicationLoadBalancedFargateService(
self, 'worker-service',
cluster=cluster,
assign_public_ip=False,
cpu=256,
memory_limit_mib=512,
task_definition=worker_task,
desired_count=1,
protocol=elb.ApplicationProtocol.HTTP # HTTPS requires valid domain name and SSL certificate
)
# Add a rule to allow ELB to talk with containers
worker_service.service.connections.security_groups[0].add_ingress_rule(
peer = ec2.Peer.ipv4(vpc.vpc_cidr_block),
connection = ec2.Port.tcp(80),
description='Allow http inbound from VPC'
)
# Configure ELB health check route
worker_service.target_group.configure_health_check(
path='/worker',
healthy_http_codes='200-299',
)
# Setup AutoScaling policy
scaling = worker_service.service.auto_scale_task_count(
max_capacity=3
)
scaling.scale_on_cpu_utilization(
'CpuScaling',
target_utilization_percent=65,
scale_in_cooldown=core.Duration.seconds(60),
scale_out_cooldown=core.Duration.seconds(60),
)
# Create a MX task definition
mx_logging = ecs.AwsLogDriver(stream_prefix='mx')
mx_task = ecs.FargateTaskDefinition(
self, 'mx-task',
cpu=256,
memory_limit_mib=512
)
mx_task.add_container(
id='mx-container',
image=ecs.ContainerImage.from_registry('leonweecs/cosmos-mx:1.1'),
environment={
'REDIS_HOST': redis_cluster.attr_redis_endpoint_address,
'REDIS_PORT': redis_cluster.attr_redis_endpoint_port,
'WORKER_HOST': worker_service.load_balancer.load_balancer_dns_name,
'WORKER_PORT': '80',
'API_ID': ssm.StringParameter.value_for_string_parameter(self, "API_ID"),
'API_HASH': ssm.StringParameter.value_for_string_parameter(self, "API_HASH"),
'BOT_TOKEN': ssm.StringParameter.value_for_string_parameter(self, "BOT_TOKEN"),
},
logging=mx_logging
)
# Create a MX service
mx_security_group = ec2.SecurityGroup(
self, 'mx-security-group',
vpc=vpc
)
mx_service = ecs.FargateService(
self, 'mx-service',
task_definition=mx_task,
assign_public_ip=True,
security_group=mx_security_group,
cluster=cluster
)
core.CfnOutput(
self, "LoadBalancerDNS",
value=worker_service.load_balancer.load_balancer_dns_name
)
app = core.App()
env = core.Environment(account='839234112409' ,region='ap-southeast-2')
CosmosBot(app, 'CoSMoS-Bot', env)
app.synth()