Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ECS cluster name with cloudformation

I'm trying to deploy an ECS cluster on AWS with CloudFormation, but I can't figure out how to set the cluster a custom name.

The cluster is created, but with somehow generated name, with format like stackname-ECSCluster-randomstring.

Is there a way how to set a custom name for the cluster?

The AWS::ECS::Cluster doesn't accept any parameters or tags, nor the AWS::ECS:Service.

It's kind of cosmetic thing, but it would be nice to have meaningfull names.

like image 380
stibi Avatar asked Oct 28 '25 09:10

stibi


1 Answers

Clipped directly from my CloudFormation scripts. If you want to pass it in, you can do so as a parameter. Specifying the name may have been something that was added since you wrote the post. Either way, this works for me.

Resources:
  MyCluster:
    Type: AWS::ECS::Cluster
    Properties:
      ClusterName: TheNameOfTheCluster

If you want to mangle the name of the cluster you can use Fn::Join to join two (or more) strings. The following will result in a cluster named "MyClusterNamePrefix-MyClusterNameSuffix"

Resources:
  MyCluster:
    Type: AWS::ECS::Cluster
    Properties:
      ClusterName: !Join
        - "-"    # The delimiter String
        - - MyClusterNamePrefix
          - MyClusterNameSuffix

If you ever need the name back out of it you can reference elsewhere, they don't make that easy. I know this isn't exactly what you asked, but chances are you may need to reference the name elsewhere (eg UserData).

Outputs:
  MyClusterName:
    Description: The Name of My Cluster
    Value:
      Fn::Select:
        - 1
        - Fn::Split:
          - "/"
          - Fn::GetAtt:
              - MyCluster

This is because (at the time of this writing) Fn::GetAtt can only return the Cluster's ARN (scroll down to the table of supported attributes). The specification for ECS Cluster ARNs is as follows:

arn:aws:ecs:region:account-id:cluster/cluster-name

So, putting it all together:

  • Fn::Split splits on "/", discards the delimiter, and returns the ["arn:aws:ecs:region:account-id:cluster", "cluster-name"]
  • Fn::Select allows you to select from the list of strings. So passing '1' returns just "cluster-name".

Port to JSON as needed. It's kind of an ugly approach to it, but it works. I would keep an eye out for if/when Amazon can actually expose that as an attribute and update your code.

like image 146
Patrick Twohig Avatar answered Oct 31 '25 00:10

Patrick Twohig