Deploying a docker image on ECS using FARGATE instance

0

We have created CloudFormation template to deploy the docker image which we pushed to ECR private repo. But while creating the stack, after uploading template it;s throwing an error saying : Template format error: Unresolved resource dependencies [TargetGroup, Listener] in the Resources block of the template. Can you please check my template and provide me the solution.

AWSTemplateFormatVersion: 2010-09-09 Description: CloudFormation template for deploying an app using Fargate with EBS storage.

Parameters: VPC: Type: AWS::EC2::VPC::Id # Default: vpc-0e7fa5b1456919151 Default: vpc-017326a9bde693167 SubnetA: Type: AWS::EC2::Subnet::Id # Default: subnet-0468bc81339a8e324 Default: subnet-0a5b2db7ade0b984c SubnetB: Type: AWS::EC2::Subnet::Id Default: subnet-03768793cf205ee4d SecurityGroup: Type: AWS::EC2::SecurityGroup::Id Default: sg-0f847124ae6322c82 Image: Type: String Default: public.ecr.aws/x2k3c8w4/munlq:v1 ServiceName: Type: String Default: MyService ContainerPort: Type: Number Default: 8080

Resources: Cluster: Type: AWS::ECS::Cluster Properties: ClusterName: !Join ['', [!Ref ServiceName, Cluster]]

TaskDefinition: Type: AWS::ECS::TaskDefinition Properties: NetworkMode: awsvpc RequiresCompatibilities: - FARGATE Cpu: '4096' Memory: '16384'

  ExecutionRoleArn: !GetAtt ExecutionRole.Arn
  TaskRoleArn: !GetAtt TaskRole.Arn
  ContainerDefinitions:
    - Name: !Ref ServiceName
      Image: !Ref Image
      PortMappings:
        - ContainerPort: !Ref ContainerPort
      Essential: true
  EphemeralStorage:
    SizeInGiB: 150

ExecutionRole: Type: AWS::IAM::Role Properties: RoleName: !Join ['', [!Ref ServiceName, ExecutionRole]] AssumeRolePolicyDocument: Statement: - Effect: Allow Principal: Service: ecs-tasks.amazonaws.com Action: 'sts:AssumeRole' ManagedPolicyArns: - 'arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy'

TaskRole: Type: AWS::IAM::Role Properties: RoleName: !Join ['', [!Ref ServiceName, TaskRole]] AssumeRolePolicyDocument: Statement: - Effect: Allow Principal: Service: ecs-tasks.amazonaws.com Action: 'sts:AssumeRole'

FargateService: Type: AWS::ECS::Service DependsOn: Listener Properties: Cluster: !Ref Cluster LaunchType: FARGATE LoadBalancers: - TargetGroupArn: !Ref TargetGroup ContainerPort: 8080 ContainerName: test ServiceName: munlq-service DesiredCount: 1 TaskDefinition: !Ref TaskDefinition NetworkConfiguration: AwsvpcConfiguration: AssignPublicIp: ENABLED SecurityGroups: - !Ref SecurityGroup Subnets: - !Ref SubnetA - !Ref SubnetB DeploymentConfiguration: MaximumPercent: 200 MinimumHealthyPercent: 100 DeploymentCircuitBreaker: Enable: true Rollback: true

1 Answer
0
Accepted Answer

Hello Only a part of your template is showing up properly and with YAML indentation is a bit of a pain when not copy-pasted properly. If you are just starting with AWS ECS & CloudFormation, you should try ECS Compose-X which will take care of creating all your CFN templates for you from the docker compose definition, allowing you to add other things such as the LoadBalancer, the VPC (or use the existing one) etc. You could also see about ECS CLI v2

But here, the error about missing [TargetGroup, Listener] means that these two things are neither a Parameter nor a resource in the template and are "missing".

profile picture
answered 8 months ago
profile pictureAWS
EXPERT
reviewed 8 months ago
  • Thank you for your answer. I have created the cloudformation template and added load balancer. It's showing 0 healthy and 0 unhealthy. The DNS Name url is not accessible showing 503 Service Temporarily Unavailable.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions