Skip to content
v2.274.0

InitService Class ​

A services that be enabled, disabled or restarted when the instance is launched.

Example ​

ts
declare const myBucket: s3.Bucket;

const handle = new ec2.InitServiceRestartHandle();

ec2.CloudFormationInit.fromElements(
  ec2.InitFile.fromString('/etc/nginx/nginx.conf', '...', { serviceRestartHandles: [handle] }),
  ec2.InitSource.fromS3Object('/var/www/html', myBucket, 'html.zip', { serviceRestartHandles: [handle] }),
  ec2.InitService.enable('nginx', {
    serviceRestartHandle: handle,
  })
);

Properties ​

elementTypestringelement_typestrelementTypeStringElementTypestringElementType*stringrequired#
Returns the init element type for this element.

Methods ​

disable(serviceName: string): InitService // chainable@staticmethod def disable(service_name: str) -> InitServicepublic static InitService disable(String serviceName)public static InitService Disable(string serviceName)func Disable(ServiceName *string) InitServicestatic#
Disable and stop the given service.
enable(serviceName: string, options?: InitServiceOptions): InitService // chainable@staticmethod def enable(service_name: str, options: typing.Optional[InitServiceOptions] = None) -> InitServicepublic static InitService enable(String serviceName, InitServiceOptions options)public static InitService Enable(string serviceName, InitServiceOptions? options = null)func Enable(ServiceName *string, Options InitServiceOptions) InitServicestatic#
Enable and start the given service, optionally restarting it.
systemdConfigFile(serviceName: string, options: SystemdConfigFileOptions): InitFile@staticmethod def systemd_config_file(service_name: str, options: SystemdConfigFileOptions) -> InitFilepublic static InitFile systemdConfigFile(String serviceName, SystemdConfigFileOptions options)public static InitFile SystemdConfigFile(string serviceName, SystemdConfigFileOptions options)func SystemdConfigFile(ServiceName *string, Options SystemdConfigFileOptions) InitFilestatic#
Install a systemd-compatible config file for the given service.
notes

This is a helper function to create a simple systemd configuration file that will allow running a service on the machine using InitService.enable().

Systemd allows many configuration options; this function does not pretend to expose all of them. If you need advanced configuration options, you can use InitFile to create exactly the configuration file you need at /etc/systemd/system/${serviceName}.service.