# ApplyCloudFormationInitOptions

Options for applying CloudFormation init to an instance or instance group.

- Source: [`aws-ec2/lib/instance.ts`](https://github.com/aws/aws-cdk/blob/v2.274.0/packages/aws-cdk-lib/aws-ec2/lib/instance.ts#L926)

## Example

```ts
declare const vpc: ec2.Vpc;
declare const instanceType: ec2.InstanceType;
declare const machineImage: ec2.IMachineImage;

new ec2.Instance(this, 'Instance', {
  vpc,
  instanceType,
  machineImage,

  // Showing the most complex setup, if you have simpler requirements
  // you can use `CloudFormationInit.fromElements()`.
  init: ec2.CloudFormationInit.fromConfigSets({
    configSets: {
      // Applies the configs below in this order
      default: ['yumPreinstall', 'config'],
    },
    configs: {
      yumPreinstall: new ec2.InitConfig([
        // Install an Amazon Linux package using yum
        ec2.InitPackage.yum('git'),
      ]),
      config: new ec2.InitConfig([
        // Create a JSON file from tokens (can also create other files)
        ec2.InitFile.fromObject('/etc/stack.json', {
          stackId: Stack.of(this).stackId,
          stackName: Stack.of(this).stackName,
          region: Stack.of(this).region,
        }),

        // Create a group and user
        ec2.InitGroup.fromName('my-group'),
        ec2.InitUser.fromName('my-user'),

        // Install an RPM from the internet
        ec2.InitPackage.rpm('http://mirrors.ukfast.co.uk/sites/dl.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/r/rubygem-git-1.5.0-2.el8.noarch.rpm'),
      ]),
    },
  }),
  initOptions: {
    // Optional, which configsets to activate (['default'] by default)
    configSets: ['default'],

    // Optional, how long the installation is expected to take (5 minutes by default)
    timeout: Duration.minutes(30),

    // Optional, whether to include the --url argument when running cfn-init and cfn-signal commands (false by default)
    includeUrl: true,

    // Optional, whether to include the --role argument when running cfn-init and cfn-signal commands (false by default)
    includeRole: true,
  },
});
```

## Properties

- `configSets`: string[] = ['default'] — ConfigSet to activate.

- `embedFingerprint`: boolean = true — Force instance replacement by embedding a config fingerprint.

  If `true` (the default), a hash of the config will be embedded into the
  UserData, so that if the config changes, the UserData changes.

  - If the EC2 instance is instance-store backed or
    `userDataCausesReplacement` is set, this will cause the instance to be
    replaced and the new configuration to be applied.
  - If the instance is EBS-backed and `userDataCausesReplacement` is not
    set, the change of UserData will make the instance restart but not be
    replaced, and the configuration will not be applied automatically.

  If `false`, no hash will be embedded, and if the CloudFormation Init
  config changes nothing will happen to the running instance. If a
  config update introduces errors, you will not notice until after the
  CloudFormation deployment successfully finishes and the next instance
  fails to launch.

- `ignoreFailures`: boolean = false — Don't fail the instance creation when cfn-init fails.

  You can use this to prevent CloudFormation from rolling back when
  instances fail to start up, to help in debugging.

- `includeRole`: boolean = false — Include --role argument when running cfn-init and cfn-signal commands.

  This will be the IAM instance profile attached to the EC2 instance

- `includeUrl`: boolean = false — Include --url argument when running cfn-init and cfn-signal commands.

  This will be the cloudformation endpoint in the deployed region
  e.g. https://cloudformation.us-east-1.amazonaws.com

- `printLog`: boolean = true — Print the results of running cfn-init to the Instance System Log.

  By default, the output of running cfn-init is written to a log file
  on the instance. Set this to `true` to print it to the System Log
  (visible from the EC2 Console), `false` to not print it.

  (Be aware that the system log is refreshed at certain points in
  time of the instance life cycle, and successful execution may
  not always show up).

- `timeout`: [Duration](/cdk/api/v2/docs/aws-cdk-lib.Duration.html) = Duration.minutes(5) — Timeout waiting for the configuration to be applied.
