Skip to content
v2.274.0

InitServiceRestartHandle Class ​

An object that represents reasons to restart an InitService.

Pass an instance of this object to the InitFile, InitCommand, InitSource and InitPackage objects, and finally to an InitService itself to cause the actions (files, commands, sources, and packages) to trigger a restart of the service.

For example, the following will run a custom command to install Nginx, and trigger the nginx service to be restarted after the command has run.

ts
const handle = new ec2.InitServiceRestartHandle();
ec2.CloudFormationInit.fromElements(
  ec2.InitCommand.shellCommand('/usr/bin/custom-nginx-install.sh', { serviceRestartHandles: [handle] }),
  ec2.InitService.enable('nginx', { serviceRestartHandle: handle }),
);

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,
}))

Initializer ​

ts
new InitServiceRestartHandle()
python
InitServiceRestartHandle()
java
new InitServiceRestartHandle()
csharp
new InitServiceRestartHandle()
go
NewInitServiceRestartHandle()