|
| 1 | +import { Vpc, SecurityGroup, Port } from "aws-cdk-lib/aws-ec2"; |
| 2 | +import { Construct } from "constructs"; |
| 3 | +import { SharedProps } from "./constructs/shared-props"; |
| 4 | +import { |
| 5 | + Cluster, |
| 6 | + ContainerImage, |
| 7 | + CpuArchitecture, |
| 8 | + FargateService, |
| 9 | + FargateTaskDefinition, |
| 10 | + FirelensLogRouterType, |
| 11 | + LogDriver, |
| 12 | + LogDrivers, |
| 13 | + OperatingSystemFamily, |
| 14 | + Protocol, |
| 15 | +} from "aws-cdk-lib/aws-ecs"; |
| 16 | +import { Repository } from "aws-cdk-lib/aws-ecr"; |
| 17 | +import { Topic } from "aws-cdk-lib/aws-sns"; |
| 18 | +import { Queue } from "aws-cdk-lib/aws-sqs"; |
| 19 | +import { |
| 20 | + ApplicationLoadBalancer, |
| 21 | + ApplicationTargetGroup, |
| 22 | + TargetType, |
| 23 | + ApplicationProtocol, |
| 24 | + HealthCheck, |
| 25 | + Protocol as HealthCheckProtocol, |
| 26 | +} from "aws-cdk-lib/aws-elasticloadbalancingv2"; |
| 27 | +import { Duration, CfnOutput } from "aws-cdk-lib"; |
| 28 | +import { ApplicationLoadBalancedFargateService } from "aws-cdk-lib/aws-ecs-patterns"; |
| 29 | + |
| 30 | +export class ApiProps { |
| 31 | + sharedProps: SharedProps; |
| 32 | + vpc: Vpc; |
| 33 | + cluster: Cluster; |
| 34 | +} |
| 35 | + |
| 36 | +export class Api extends Construct { |
| 37 | + stickerClaimedQueue: Queue; |
| 38 | + stickerClaimedDLQ: Queue; |
| 39 | + userRegisteredTopic: Topic; |
| 40 | + constructor(scope: Construct, id: string, props: ApiProps) { |
| 41 | + super(scope, id); |
| 42 | + |
| 43 | + const ecrRepository = Repository.fromRepositoryName( |
| 44 | + this, |
| 45 | + "UserServiceRepo", |
| 46 | + "stickerlandia-user-management" |
| 47 | + ); |
| 48 | + |
| 49 | + this.userRegisteredTopic = new Topic(this, "UserRegisteredTopic", { |
| 50 | + topicName: `${props.sharedProps.serviceName}-${props.sharedProps.environment}-user-registered`, |
| 51 | + }); |
| 52 | + this.stickerClaimedDLQ = new Queue(this, "StickerClaimedDLQ", { |
| 53 | + queueName: `${props.sharedProps.serviceName}-${props.sharedProps.environment}-sticker-claimed-dlq`, |
| 54 | + }); |
| 55 | + this.stickerClaimedQueue = new Queue(this, "StickerClaimedQueue", { |
| 56 | + queueName: `${props.sharedProps.serviceName}-${props.sharedProps.environment}-sticker-claimed`, |
| 57 | + deadLetterQueue: { |
| 58 | + queue: this.stickerClaimedDLQ, |
| 59 | + maxReceiveCount: 5, // Messages will be sent to DLQ after 5 failed attempts |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + const applicationTaskDef = new FargateTaskDefinition( |
| 64 | + this, |
| 65 | + "UserServiceTaskDef", |
| 66 | + { |
| 67 | + runtimePlatform: { |
| 68 | + operatingSystemFamily: OperatingSystemFamily.LINUX, |
| 69 | + cpuArchitecture: CpuArchitecture.ARM64, |
| 70 | + }, |
| 71 | + cpu: 256, |
| 72 | + memoryLimitMiB: 512, |
| 73 | + } |
| 74 | + ); |
| 75 | + |
| 76 | + const containerPort = 8080; |
| 77 | + |
| 78 | + const container = applicationTaskDef.addContainer("UserServiceContainer", { |
| 79 | + image: ContainerImage.fromEcrRepository( |
| 80 | + ecrRepository, |
| 81 | + props.sharedProps.version |
| 82 | + ), |
| 83 | + portMappings: [ |
| 84 | + { |
| 85 | + containerPort: containerPort, |
| 86 | + protocol: Protocol.TCP, |
| 87 | + }, |
| 88 | + ], |
| 89 | + environment: { |
| 90 | + ConnectionStrings__messaging: "", |
| 91 | + ConnectionStrings__database: props.sharedProps.connectionString, |
| 92 | + Aws__UserRegisteredTopicArn: this.userRegisteredTopic.topicArn, |
| 93 | + Aws__StickerClaimedQueueUrl: this.stickerClaimedQueue.queueUrl, |
| 94 | + Aws__StickerClaimedDLQUrl: this.stickerClaimedDLQ.queueUrl, |
| 95 | + DRIVING: "ASPNET", |
| 96 | + DRIVEN: "AWS", |
| 97 | + DISABLE_SSL: "true", |
| 98 | + }, |
| 99 | + logging: LogDrivers.firelens({ |
| 100 | + options: { |
| 101 | + Name: "datadog", |
| 102 | + Host: "http-intake.logs.datadoghq.eu", |
| 103 | + TLS: "on", |
| 104 | + dd_service: props.sharedProps.serviceName, |
| 105 | + dd_source: "aspnet", |
| 106 | + dd_message_key: "log", |
| 107 | + dd_tags: `project:${props.sharedProps.serviceName}`, |
| 108 | + provider: "ecs", |
| 109 | + apikey: props.sharedProps.datadog.apiKey, |
| 110 | + }, |
| 111 | + }), |
| 112 | + }); |
| 113 | + container.addDockerLabel( |
| 114 | + "com.datadoghq.tags.env", |
| 115 | + props.sharedProps.environment |
| 116 | + ); |
| 117 | + container.addDockerLabel( |
| 118 | + "com.datadoghq.tags.service", |
| 119 | + props.sharedProps.serviceName |
| 120 | + ); |
| 121 | + container.addDockerLabel( |
| 122 | + "com.datadoghq.tags.version", |
| 123 | + props.sharedProps.version |
| 124 | + ); |
| 125 | + |
| 126 | + container.addPortMappings({ |
| 127 | + containerPort: containerPort, |
| 128 | + protocol: Protocol.TCP, |
| 129 | + }); |
| 130 | + |
| 131 | + applicationTaskDef.addContainer("datadog-agent", { |
| 132 | + image: ContainerImage.fromRegistry("public.ecr.aws/datadog/agent:latest"), |
| 133 | + portMappings: [ |
| 134 | + { |
| 135 | + containerPort: 8125, |
| 136 | + protocol: Protocol.UDP, // Dogstatsd port |
| 137 | + }, |
| 138 | + { |
| 139 | + containerPort: 8126, |
| 140 | + protocol: Protocol.TCP, // APM port |
| 141 | + }, |
| 142 | + ], |
| 143 | + containerName: "datadog-agent", |
| 144 | + environment: { |
| 145 | + DD_API_KEY: props.sharedProps.datadog.apiKey, |
| 146 | + DD_SITE: props.sharedProps.datadog.site, |
| 147 | + DD_APM_ENABLED: "true", |
| 148 | + DD_LOGS_ENABLED: "true", |
| 149 | + ECS_FARGATE: "true", |
| 150 | + DD_APM_NON_LOCAL_TRAFFIC: "true", |
| 151 | + DD_DOGSTATSD_NON_LOCAL_TRAFFIC: "true", |
| 152 | + DD_APM_IGNORE_RESOURCES: `(GET) /api/users/v1/health`, |
| 153 | + }, |
| 154 | + }); |
| 155 | + applicationTaskDef.addFirelensLogRouter("firelens-router", { |
| 156 | + essential: true, |
| 157 | + image: ContainerImage.fromRegistry("amazon/aws-for-fluent-bit:stable"), |
| 158 | + firelensConfig: { |
| 159 | + type: FirelensLogRouterType.FLUENTBIT, |
| 160 | + options: { |
| 161 | + enableECSLogMetadata: true, |
| 162 | + }, |
| 163 | + }, |
| 164 | + }); |
| 165 | + |
| 166 | + this.userRegisteredTopic.grantPublish(applicationTaskDef.taskRole); |
| 167 | + this.stickerClaimedQueue.grantSendMessages(applicationTaskDef.taskRole); |
| 168 | + this.stickerClaimedDLQ.grantSendMessages(applicationTaskDef.taskRole); |
| 169 | + this.stickerClaimedQueue.grantConsumeMessages(applicationTaskDef.taskRole); |
| 170 | + this.stickerClaimedDLQ.grantConsumeMessages(applicationTaskDef.taskRole); |
| 171 | + |
| 172 | + const service = new ApplicationLoadBalancedFargateService( |
| 173 | + this, |
| 174 | + "UserServiceFargateService", |
| 175 | + { |
| 176 | + cluster: props.cluster, |
| 177 | + taskDefinition: applicationTaskDef, |
| 178 | + assignPublicIp: false, |
| 179 | + publicLoadBalancer: true, |
| 180 | + desiredCount: 1, |
| 181 | + loadBalancerName: `${props.sharedProps.serviceName}-${props.sharedProps.environment}-user-service-lb`, |
| 182 | + serviceName: `${props.sharedProps.serviceName}-${props.sharedProps.environment}-user-service`, |
| 183 | + listenerPort: 80, |
| 184 | + protocol: ApplicationProtocol.HTTP, |
| 185 | + healthCheckGracePeriod: Duration.minutes(2), |
| 186 | + circuitBreaker: { |
| 187 | + rollback: true, |
| 188 | + enable: true, |
| 189 | + }, |
| 190 | + } |
| 191 | + ); |
| 192 | + service.targetGroup.configureHealthCheck({ |
| 193 | + path: "/api/users/v1/health", |
| 194 | + interval: Duration.seconds(60), |
| 195 | + timeout: Duration.seconds(5), |
| 196 | + enabled: true, |
| 197 | + healthyHttpCodes: "200-499", |
| 198 | + }); |
| 199 | + } |
| 200 | +} |
0 commit comments