class aws_cdk.aws_ec2.InitService

Bases: InitElement

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




# my_bucket: s3.Bucket

handle = ec2.InitServiceRestartHandle()

    ec2.InitFile.from_string("/etc/nginx/nginx.conf", "...", service_restart_handles=[handle]),
    ec2.InitSource.from_s3_object("/var/www/html", my_bucket, "", service_restart_handles=[handle]),



Returns the init element type for this element.

Static Methods

classmethod disable(service_name)

Disable and stop the given service.


service_name (str) –

Return type:


classmethod enable(service_name, *, enabled=None, ensure_running=None, service_manager=None, service_restart_handle=None)

Enable and start the given service, optionally restarting it.

  • service_name (str) –

  • enabled (Optional[bool]) – Enable or disable this service. Set to true to ensure that the service will be started automatically upon boot. Set to false to ensure that the service will not be started automatically upon boot. Default: - true if used in InitService.enable(), no change to service state if used in InitService.fromOptions().

  • ensure_running (Optional[bool]) – Make sure this service is running or not running after cfn-init finishes. Set to true to ensure that the service is running after cfn-init finishes. Set to false to ensure that the service is not running after cfn-init finishes. Default: - same value as enabled.

  • service_manager (Optional[ServiceManager]) – What service manager to use. This needs to match the actual service manager on your Operating System. For example, Amazon Linux 1 uses SysVinit, but Amazon Linux 2 uses Systemd. Default: ServiceManager.SYSVINIT for Linux images, ServiceManager.WINDOWS for Windows images

  • service_restart_handle (Optional[InitServiceRestartHandle]) – Restart service when the actions registered into the restartHandle have been performed. Register actions into the restartHandle by passing it to InitFile, InitCommand, InitPackage and InitSource objects. Default: - No files trigger restart

Return type:


classmethod systemd_config_file(service_name, *, command, after_network=None, cwd=None, description=None, environment_files=None, environment_variables=None, group=None, keep_running=None, user=None)

Install a systemd-compatible config file for the given service.

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.

  • service_name (str) –

  • command (str) – The command to run to start this service.

  • after_network (Optional[bool]) – Start the service after the networking part of the OS comes up. Default: true

  • cwd (Optional[str]) – The working directory for the command. Default: Root directory or home directory of specified user

  • description (Optional[str]) – A description of this service. Default: - No description

  • environment_files (Optional[Sequence[str]]) – Loads environment variables from files when the process is running. Must use absolute paths. Default: - No environment files

  • environment_variables (Optional[Mapping[str, str]]) – Environment variables to load when the process is running. Default: - No environment variables set

  • group (Optional[str]) – The group to execute the process under. Default: root

  • keep_running (Optional[bool]) – Keep the process running all the time. Restarts the process when it exits for any reason other than the machine shutting down. Default: true

  • user (Optional[str]) – The user to execute the process under. Default: root

Return type: