Skip to content
This repository was archived by the owner on Mar 27, 2023. It is now read-only.

Commit 19d9232

Browse files
committed
wip add flower service
1 parent 1f84226 commit 19d9232

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

awscdk/awscdk/flower.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import os
2+
3+
from aws_cdk import (
4+
core,
5+
aws_ec2 as ec2,
6+
aws_ecs as ecs,
7+
aws_logs as logs,
8+
aws_cloudformation as cloudformation,
9+
aws_elasticloadbalancingv2 as elbv2,
10+
)
11+
12+
13+
class FlowerServiceStack(cloudformation.NestedStack):
14+
def __init__(self, scope: core.Construct, id: str, **kwargs,) -> None:
15+
super().__init__(
16+
scope, id, **kwargs,
17+
)
18+
19+
self.flower_task = ecs.FargateTaskDefinition(self, "FlowerTask")
20+
21+
FLOWER_PASSWORD = os.environ.get("FLOWER_PASSWORD", "flowerpassword")
22+
REDIS_SERVICE_HOST = (
23+
scope.elasticache.elasticache.attr_redis_endpoint_address
24+
)
25+
CELERY_BROKER_URL = f"redis://{REDIS_SERVICE_HOST}:6379/0"
26+
self.flower_task.add_container(
27+
"BackendContainer",
28+
image=ecs.ContainerImage.from_registry("mher/flower"),
29+
logging=ecs.LogDrivers.aws_logs(
30+
stream_prefix="FlowerContainer",
31+
log_retention=logs.RetentionDays.ONE_DAY,
32+
),
33+
command=[
34+
"--url_prefix=flower",
35+
f"--broker={CELERY_BROKER_URL}",
36+
f"--basic_auth=flower:{FLOWER_PASSWORD}",
37+
],
38+
)
39+
40+
scope.backend_assets_bucket.grant_read_write(
41+
self.backend_task.task_role
42+
)
43+
44+
for secret in [scope.variables.django_secret_key, scope.rds.db_secret]:
45+
secret.grant_read(self.backend_task.task_role)
46+
47+
port_mapping = ecs.PortMapping(
48+
container_port=5555, protocol=ecs.Protocol.TCP
49+
)
50+
self.flower_task.default_container.add_port_mappings(port_mapping)
51+
52+
self.flower_service = ecs.FargateService(
53+
self,
54+
"FlowerService",
55+
task_definition=self.flower_task,
56+
assign_public_ip=True,
57+
cluster=scope.ecs.cluster,
58+
security_group=ec2.SecurityGroup.from_security_group_id(
59+
self,
60+
"BackendServiceSecurityGroup",
61+
security_group_id=scope.vpc.vpc_default_security_group,
62+
),
63+
)
64+
65+
scope.https_listener.add_targets(
66+
"BackendTarget",
67+
port=80,
68+
targets=[self.flower_service],
69+
priority=1,
70+
path_patterns=["/flower/*"],
71+
health_check=elbv2.HealthCheck(healthy_http_codes="200-401",),
72+
)

cloudformation/services/flower.yaml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ Description: >
22
This is an example of a long running ECS service that serves a JSON API of products.
33
44
Parameters:
5-
65
StackName:
76
Description: The name of the stack
87
Type: String
@@ -37,15 +36,13 @@ Parameters:
3736
Type: String
3837

3938
Resources:
40-
4139
FlowerService:
4240
Type: AWS::ECS::Service
4341
DependsOn: FlowerListenerRule
4442
Properties:
4543
# Cluster: !Ref ECSCluster
4644
Cluster:
47-
Fn::ImportValue:
48-
!Sub ${StackName}:ECSCluster
45+
Fn::ImportValue: !Sub ${StackName}:ECSCluster
4946
Role: !Ref ServiceRole
5047
DesiredCount: !Ref DesiredCount
5148
TaskDefinition: !Ref FlowerTaskDefinition
@@ -64,7 +61,7 @@ Resources:
6461
Image: mher/flower
6562
Memory: 128
6663
Command:
67-
- 'flower'
64+
- "flower"
6865
- !Sub "--broker=redis://${ElastiCacheHost}:6379//"
6966
- !Sub "--basic_auth=${FlowerUsername}:${FlowerPassword}"
7067
PortMappings:
@@ -85,8 +82,7 @@ Resources:
8582
Type: AWS::ElasticLoadBalancingV2::TargetGroup
8683
Properties:
8784
VpcId:
88-
Fn::ImportValue:
89-
!Sub ${StackName}:VpcId
85+
Fn::ImportValue: !Sub ${StackName}:VpcId
9086
Port: 80
9187
Protocol: HTTP
9288
Matcher:

0 commit comments

Comments
 (0)