aws-cdk-lib.aws_ec2.InitFile

class InitFile

LanguageType name
.NETAmazon.CDK.AWS.EC2.InitFile
Gogithub.com/aws/aws-cdk-go/awscdk/v2/awsec2#InitFile
Javasoftware.amazon.awscdk.services.ec2.InitFile
Pythonaws_cdk.aws_ec2.InitFile
TypeScript (source)aws-cdk-lib » aws_ec2 » InitFile

Extends InitElement

Obtainable from InitService.systemdConfigFile()

Create files on the EC2 instance.

Example

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

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

  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!'),
  ),
});

Initializer (protected)

super(fileName: string, options: InitFileOptions)

Parameters

  • fileName string
  • options InitFileOptions

Properties

NameTypeDescription
elementTypestringReturns the init element type for this element.

elementType

Type: string

Returns the init element type for this element.

Methods

NameDescription
static fromAsset(targetFileName, path, options?)Create an asset from the given file.
static fromExistingAsset(targetFileName, asset, options?)Use a file from an asset at instance startup time.
static fromFileInline(targetFileName, sourceFileName, options?)Read a file from disk and use its contents.
static fromObject(fileName, obj, options?)Use a JSON-compatible object as the file content, write it to a JSON file.
static fromS3Object(fileName, bucket, key, options?)Download a file from an S3 bucket at instance startup time.
static fromString(fileName, content, options?)Use a literal string as the file content.
static fromUrl(fileName, url, options?)Download from a URL at instance startup time.
static symlink(fileName, target, options?)Write a symlink with the given symlink target.

static fromAsset(targetFileName, path, options?)

public static fromAsset(targetFileName: string, path: string, options?: InitFileAssetOptions): InitFile

Parameters

  • targetFileName string
  • path string
  • options InitFileAssetOptions

Returns

  • InitFile

Create an asset from the given file.

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


static fromExistingAsset(targetFileName, asset, options?)

public static fromExistingAsset(targetFileName: string, asset: Asset, options?: InitFileOptions): InitFile

Parameters

  • targetFileName string
  • asset Asset
  • options InitFileOptions

Returns

  • InitFile

Use a file from an asset at instance startup time.


static fromFileInline(targetFileName, sourceFileName, options?)

public static fromFileInline(targetFileName: string, sourceFileName: string, options?: InitFileOptions): InitFile

Parameters

  • targetFileName string
  • sourceFileName string
  • options InitFileOptions

Returns

  • InitFile

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.


static fromObject(fileName, obj, options?)

public static fromObject(fileName: string, obj: { [string]: any }, options?: InitFileOptions): InitFile

Parameters

  • fileName string
  • obj { [string]: any }
  • options InitFileOptions

Returns

  • InitFile

Use a JSON-compatible object as the file content, write it to a JSON file.

May contain tokens.


static fromS3Object(fileName, bucket, key, options?)

public static fromS3Object(fileName: string, bucket: IBucket, key: string, options?: InitFileOptions): InitFile

Parameters

  • fileName string
  • bucket IBucket
  • key string
  • options InitFileOptions

Returns

  • InitFile

Download a file from an S3 bucket at instance startup time.


static fromString(fileName, content, options?)

public static fromString(fileName: string, content: string, options?: InitFileOptions): InitFile

Parameters

  • fileName string
  • content string
  • options InitFileOptions

Returns

  • InitFile

Use a literal string as the file content.


static fromUrl(fileName, url, options?)

public static fromUrl(fileName: string, url: string, options?: InitFileOptions): InitFile

Parameters

  • fileName string
  • url string
  • options InitFileOptions

Returns

  • InitFile

Download from a URL at instance startup time.


static symlink(fileName, target, options?)

public static symlink(fileName: string, target: string, options?: InitFileOptions): InitFile

Parameters

  • fileName string
  • target string
  • options InitFileOptions

Returns

  • InitFile

Write a symlink with the given symlink target.