@@ -133,7 +133,158 @@ Resources:
133133 - dynamodb:UpdateItem
134134 Resource : !GetAtt RoomsTable.Arn
135135
136+ ECSSecurityGroup :
137+ Type : AWS::EC2::SecurityGroup
138+ Properties :
139+ GroupDescription : Security group for ECS tasks
140+ VpcId : !Ref VPC
141+ SecurityGroupIngress :
142+ - IpProtocol : tcp
143+ FromPort : 8081
144+ ToPort : 8081
145+ SourceSecurityGroupId : !Ref ALBSecurityGroup
146+
147+ ALBSecurityGroup :
148+ Type : AWS::EC2::SecurityGroup
149+ Properties :
150+ GroupDescription : Security group for ALB - CloudFront only
151+ VpcId : !Ref VPC
152+ SecurityGroupIngress :
153+ - IpProtocol : tcp
154+ FromPort : 80
155+ ToPort : 80
156+ SourcePrefixListId : pl-3b927c52
157+
158+ ApplicationLoadBalancer :
159+ Type : AWS::ElasticLoadBalancingV2::LoadBalancer
160+ Properties :
161+ Type : application
162+ Scheme : internet-facing
163+ SecurityGroups : [!Ref ALBSecurityGroup]
164+ Subnets : [!Ref PublicSubnet1, !Ref PublicSubnet2]
165+
166+ TargetGroup :
167+ Type : AWS::ElasticLoadBalancingV2::TargetGroup
168+ Properties :
169+ Port : 8081
170+ Protocol : HTTP
171+ VpcId : !Ref VPC
172+ TargetType : ip
173+ HealthCheckPath : /
174+
175+ ALBListener :
176+ Type : AWS::ElasticLoadBalancingV2::Listener
177+ Properties :
178+ DefaultActions :
179+ - Type : forward
180+ TargetGroupArn : !Ref TargetGroup
181+ LoadBalancerArn : !Ref ApplicationLoadBalancer
182+ Port : 80
183+ Protocol : HTTP
184+
185+ CloudFrontDistribution :
186+ Type : AWS::CloudFront::Distribution
187+ Properties :
188+ DistributionConfig :
189+ Enabled : true
190+ Origins :
191+ - Id : ALBOrigin
192+ DomainName : !GetAtt ApplicationLoadBalancer.DNSName
193+ CustomOriginConfig :
194+ HTTPPort : 80
195+ OriginProtocolPolicy : http-only
196+ DefaultCacheBehavior :
197+ TargetOriginId : ALBOrigin
198+ ViewerProtocolPolicy : redirect-to-https
199+ AllowedMethods : [GET, HEAD, OPTIONS, PUT, POST, PATCH, DELETE]
200+ CachedMethods : [GET, HEAD]
201+ ForwardedValues :
202+ QueryString : true
203+ Headers : ["*"]
204+ MinTTL : 0
205+ DefaultTTL : 0
206+ MaxTTL : 0
207+ ViewerCertificate :
208+ CloudFrontDefaultCertificate : true
209+
210+ ECSCluster :
211+ Type : AWS::ECS::Cluster
212+ Properties :
213+ ClusterName : !Sub "hotel-app-${Environment}"
214+
215+ TaskDefinition :
216+ Type : AWS::ECS::TaskDefinition
217+ Properties :
218+ Family : !Sub "hotel-app-${Environment}"
219+ NetworkMode : awsvpc
220+ RequiresCompatibilities : [FARGATE]
221+ Cpu : 256
222+ Memory : 512
223+ ExecutionRoleArn : !Ref ECSExecutionRole
224+ TaskRoleArn : !Ref ECSTaskRole
225+ ContainerDefinitions :
226+ - Name : hotel-app
227+ Image : !Sub "${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/hotel-app:latest"
228+ PortMappings :
229+ - ContainerPort : 8081
230+ Environment :
231+ - Name : DYNAMODB_TABLE_NAME
232+ Value : !Ref RoomsTable
233+ - Name : HOTEL_NAME
234+ Value : !Ref HotelName
235+ - Name : DYNAMODB_ENDPOINT
236+ Value : " https://dynamodb.us-east-1.amazonaws.com"
237+ LogConfiguration :
238+ LogDriver : awslogs
239+ Options :
240+ awslogs-group : !Ref LogGroup
241+ awslogs-region : !Ref AWS::Region
242+ awslogs-stream-prefix : ecs
243+
244+ ECSService :
245+ Type : AWS::ECS::Service
246+ DependsOn : ALBListener
247+ Properties :
248+ ServiceName : !Sub "hotel-service-${Environment}"
249+ Cluster : !Ref ECSCluster
250+ TaskDefinition : !Ref TaskDefinition
251+ LaunchType : FARGATE
252+ DesiredCount : 1
253+ NetworkConfiguration :
254+ AwsvpcConfiguration :
255+ SecurityGroups : [!Ref ECSSecurityGroup]
256+ Subnets : [!Ref PublicSubnet1, !Ref PublicSubnet2]
257+ AssignPublicIp : ENABLED
258+ LoadBalancers :
259+ - ContainerName : hotel-app
260+ ContainerPort : 8081
261+ TargetGroupArn : !Ref TargetGroup
262+
263+ LogGroup :
264+ Type : AWS::Logs::LogGroup
265+ Properties :
266+ LogGroupName : !Sub "/ecs/hotel-app-${Environment}"
267+ RetentionInDays : 7
268+
269+ ApplicationURLSSMParameter :
270+ Type : AWS::SSM::Parameter
271+ Properties :
272+ Name : !Sub "/hotelapp/${Environment}/url"
273+ Description : " Hotel app URI"
274+ Type : String
275+ Value : !Sub "https://${CloudFrontDistribution.DomainName}"
276+
277+
136278Outputs :
137279 DynamoDBTableName :
138280 Description : " Name of the DynamoDB Table"
139- Value : !Ref RoomsTable
281+ Value : !Ref RoomsTable
282+ ApplicationURL :
283+ Description : " CloudFront Distribution URL"
284+ Value : !Sub "https://${CloudFrontDistribution.DomainName}"
285+ ECSClusterName :
286+ Description : " Name of the ECS Cluster"
287+ Value : !Ref ECSCluster
288+ ECSServiceName :
289+ Description : " Name of the ECS Service"
290+ Value : !Ref ECSService
0 commit comments