# InitFile

Create files on the EC2 instance.

Extends: InitElement › InitFile

- Source: [`aws-ec2/lib/cfn-init-elements.ts`](https://github.com/aws/aws-cdk/blob/v2.274.0/packages/aws-cdk-lib/aws-ec2/lib/cfn-init-elements.ts#L324)

## Example

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

new ec2.Instance(this, 'Instance', {
  vpc,
  instanceType,
  machineImage: ec2.MachineImage.latestAmazonLinux2023(),

  init: ec2.CloudFormationInit.fromElements(
    // Create a simple config file that runs a Python web server
    ec2.InitService.systemdConfigFile('simpleserver', {
      command: '/usr/bin/python3 -m http.server 8080',
      cwd: '/var/www/html',
    }),
    // Start the server using SystemD
    ec2.InitService.enable('simpleserver', {
      serviceManager: ec2.ServiceManager.SYSTEMD,
    }),
    // Drop an example file to show the web server working
    ec2.InitFile.fromString('/var/www/html/index.html', 'Hello! It\'s working!'),
  ),
});
```

## Properties

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

## Methods

- `fromAsset(targetFileName: string, path: string, options?: InitFileAssetOptions): InitFile // chainable` (static) — Create an asset from the given file.

  This is appropriate for files that are too large to embed into the template.

- `fromExistingAsset(targetFileName: string, asset: Asset, options?: InitFileOptions): InitFile // chainable` (static) — Use a file from an asset at instance startup time.

- `fromFileInline(targetFileName: string, sourceFileName: string, options?: InitFileOptions): InitFile // chainable` (static) — Read a file from disk and use its contents.

  The file will be embedded in the template, so care should be taken to not
  exceed the template size.

  If options.base64encoded is set to true, this will base64-encode the file's contents.

- `fromObject(fileName: string, obj: { [key: string]: any }, options?: InitFileOptions): InitFile // chainable` (static) — Use a JSON-compatible object as the file content, write it to a JSON file.

  May contain tokens.

- `fromS3Object(fileName: string, bucket: IBucket, key: string, options?: InitFileOptions): InitFile // chainable` (static) — Download a file from an S3 bucket at instance startup time.

- `fromString(fileName: string, content: string, options?: InitFileOptions): InitFile // chainable` (static) — Use a literal string as the file content.

- `fromUrl(fileName: string, url: string, options?: InitFileOptions): InitFile // chainable` (static) — Download from a URL at instance startup time.

- `symlink(fileName: string, target: string, options?: InitFileOptions): InitFile // chainable` (static) — Write a symlink with the given symlink target.
