Skip to content
v2.277.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,
  })
);
python
# my_bucket: s3.Bucket


handle = ec2.InitServiceRestartHandle()

ec2.CloudFormationInit.from_elements(
    ec2.InitFile.from_string("/etc/nginx/nginx.conf", "...", service_restart_handles=[handle]),
    ec2.InitSource.from_s3_object("/var/www/html", my_bucket, "html.zip", service_restart_handles=[handle]),
    ec2.InitService.enable("nginx",
        service_restart_handle=handle
    ))
java
Bucket myBucket;


InitServiceRestartHandle handle = new InitServiceRestartHandle();

CloudFormationInit.fromElements(InitFile.fromString("/etc/nginx/nginx.conf", "...", InitFileOptions.builder().serviceRestartHandles(List.of(handle)).build()), InitSource.fromS3Object("/var/www/html", myBucket, "html.zip", InitSourceOptions.builder().serviceRestartHandles(List.of(handle)).build()), InitService.enable("nginx", InitServiceOptions.builder()
        .serviceRestartHandle(handle)
        .build()));
csharp
// Install: dotnet add package Amazon.CDK.Lib
Bucket myBucket;


var handle = new InitServiceRestartHandle();

CloudFormationInit.FromElements(InitFile.FromString("/etc/nginx/nginx.conf", "...", new InitFileOptions { ServiceRestartHandles = new [] { handle } }), InitSource.FromS3Object("/var/www/html", myBucket, "html.zip", new InitSourceOptions { ServiceRestartHandles = new [] { handle } }), InitService.Enable("nginx", new InitServiceOptions {
    ServiceRestartHandle = handle
}));
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var myBucket Bucket


handle := ec2.NewInitServiceRestartHandle()

ec2.CloudFormationInit_FromElements(ec2.InitFile_FromString(jsii.String("/etc/nginx/nginx.conf"), jsii.String("..."), &InitFileOptions{
	ServiceRestartHandles: []InitServiceRestartHandle{
		handle,
	},
}), ec2.InitSource_FromS3Object(jsii.String("/var/www/html"), myBucket, jsii.String("html.zip"), &InitSourceOptions{
	ServiceRestartHandles: []InitServiceRestartHandle{
		handle,
	},
}), ec2.InitService_Enable(jsii.String("nginx"), &InitServiceOptions{
	ServiceRestartHandle: handle,
}))

Properties ​

elementTypestringelement_typestrelementTypeStringElementTypestringElementType*string#
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.