cdklabs/cdk-ecs-service-extensions

Docs: custom resource name

PrettySolution opened this issue · 2 comments

Hi,

I have a requirement to set custom Service and TaskDefinition names.
Is that posible?

thanks!

Hey there! Thanks for asking. We don't necessarily recommend doing this, as changing the service name can trigger a replacement of the CFN resource. That's why we don't have the Name property exposed in the ServiceBuild interface. If you really need to change it, you could do so by modifying the underlying service after it's created.

You can do both of those things with a custom extension.

You could use the modifyTaskDefinitionProps and useService functions in the ServiceExtension interface like so:

public class NameExtension extends ServiceExtension {
  
  constructor(private readonly taskDefFamily: string, private readonly serviceName: string) {
    super('name-extension');
  }

  public modifyTaskDefinitionProps(props: ecs.TaskDefinitionProps): ecs.TaskDefinitionProps {
    return {
      ...props,
      family: this.taskDefFamily,
    } as ecs.TaskDefinitionProps;
  }

  public useService(service: ecs.Ec2Service | ecs.FargateService) {
    service.defaultChild.serviceName = this.serviceName;
  }
}

Does that help? Feel free to reopen if this doesn't work for you!