# InitService

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

Extends: InitElement › InitService

- Source: [`aws-ec2/lib/cfn-init-elements.ts`](https://github.com/aws/aws-cdk/blob/v2.275.0/packages/aws-cdk-lib/aws-ec2/lib/cfn-init-elements.ts#L798)
- Type last changed: v2.70.0 · 22 Mar 2023

## 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

- `elementType`: string — Returns the init element type for this element.

## Methods

- `disable(serviceName: string): InitService // chainable` (static) — Disable and stop the given service.

- `enable(serviceName: string, options?: InitServiceOptions): InitService // chainable` (static) — Enable and start the given service, optionally restarting it.

- `systemdConfigFile(serviceName: string, options: SystemdConfigFileOptions): InitFile` (static) — 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`.
