4

I have troubles deploying MySQL image on AWS ECS FARGATE.

The cloudformation script that i have is this (dont mind the syntax, i am using python lib Troposphere to manage cloudfromation templates):

    TaskDefinition(
            'WordpressDatabaseTaskDefinition',
            RequiresCompatibilities=['FARGATE'],
            Cpu='512',
            Memory='2048',
            NetworkMode='awsvpc',   
            ContainerDefinitions=[
                ContainerDefinition(
                    Name='WordpressDatabaseContainer',
                    Image='mysql:5.7',
                    Environment=[
                        Environment(Name='MYSQL_ROOT_PASSWORD', Value='root'),
                        Environment(Name='MYSQL_DATABASE', Value='wpdb'),
                        Environment(Name='MYSQL_USER', Value='root'),
                        Environment(Name='MYSQL_PASSWORD', Value='root'),
                    ],
                    PortMappings=[
                        PortMapping(
                            ContainerPort=3306
                        )
                    ]
                )
            ]
        )

The deployment succeeds. I can even see that the task is running for few seconds until its state changes to STOPPED.

The only thing that i can see is:

  1. Stopped reason Essential container in task exited
  2. Exit Code 1

On localhost it works like a charm. What am i doing here wrong? At least - are there ways to debug this?

Laimonas Sutkus
  • 3,247
  • 2
  • 26
  • 47

2 Answers2

1

With AWS ECS, if it is stopping, it may be failing a health check which is causing the container to restart. What port is the container DB mapped to and can you check the container logs to see what is happening when it starts then stops? Also, check the logs in ECS under the service or task. Post it here so I can take a look at them.

Cloud W.
  • 181
  • 5
  • Such a silly mistake. I simply tried to run docker container on localhost with exactly same environment and I got crash too. By running docker logs I found out that it fails to create user root. I had to choose different user and password. – Laimonas Sutkus Sep 05 '19 at 05:43
1

So, I found out a mistake.

THE VERY FIRST THING YOU DO - is you test that docker container on localhost and see if you can reproduce the issue. In my case docker mysql container on a local machine with the exact same environment crashed too. I was able to inspect logs and found out that it fails to create "root" user. Simply changing user and password made everything work, even on ECS.

This is the complete stack to have a mysql docker image running on AWS ECS FARGATE:

    self.wordpress_database_task = TaskDefinition(
            'WordpressDatabaseTaskDefinition',
            RequiresCompatibilities=['FARGATE'],
            Cpu='512',
            Memory='2048',
            NetworkMode='awsvpc',

            # If your tasks are using the Fargate launch type, the host and sourcePath parameters are not supported.
            Volumes=[
                Volume(
                    Name='MySqlVolume',
                    DockerVolumeConfiguration=DockerVolumeConfiguration(
                        Scope='shared',
                        Autoprovision=True
                    )
                )
            ],

            ContainerDefinitions=[
                ContainerDefinition(
                    Name='WordpressDatabaseContainer',
                    Image='mysql:5.7',
                    Environment=[
                        Environment(Name='MYSQL_ROOT_PASSWORD', Value='root'),
                        Environment(Name='MYSQL_DATABASE', Value='wpdb'),
                        Environment(Name='MYSQL_USER', Value='wordpressuser'),
                        Environment(Name='MYSQL_PASSWORD', Value='wordpressuserpassword'),
                    ],
                    PortMappings=[
                        PortMapping(
                            ContainerPort=3306
                        )
                    ]
                )
            ]
        )

        self.wordpress_database_service = Service(
            'WordpressDatabaseService',
            Cluster=Ref(self.ecs_cluster),
            DesiredCount=1,
            TaskDefinition=Ref(self.wordpress_database_task),
            LaunchType='FARGATE',
            NetworkConfiguration=NetworkConfiguration(
                AwsvpcConfiguration=AwsvpcConfiguration(
                    Subnets=[Ref(sub) for sub in VpcFormation().public_subnets],
                    AssignPublicIp='ENABLED',
                    SecurityGroups=[Ref(self.security_group)]
                )
            ),
        )

Note the AssignPublicIp='ENABLED' option so you would be able to connect to the database remotely.

After the stack completed i was able to successfully connect with a command:

mysql -uwordpressuser -pwordpressuserpassword -h18.202.31.123

Thats it :)

Laimonas Sutkus
  • 3,247
  • 2
  • 26
  • 47