InitFile Class
Create files on the EC2 instance.
| Source | aws-ec2/lib/cfn-init-elements.ts |
|---|
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!'),
),
});
python
# vpc: ec2.Vpc
# instance_type: ec2.InstanceType
ec2.Instance(self, "Instance",
vpc=vpc,
instance_type=instance_type,
machine_image=ec2.MachineImage.latest_amazon_linux2023(),
init=ec2.CloudFormationInit.from_elements(
# Create a simple config file that runs a Python web server
ec2.InitService.systemd_config_file("simpleserver",
command="/usr/bin/python3 -m http.server 8080",
cwd="/var/www/html"
),
# Start the server using SystemD
ec2.InitService.enable("simpleserver",
service_manager=ec2.ServiceManager.SYSTEMD
),
# Drop an example file to show the web server working
ec2.InitFile.from_string("/var/www/html/index.html", "Hello! It's working!"))
)
java
Vpc vpc;
InstanceType instanceType;
Instance.Builder.create(this, "Instance")
.vpc(vpc)
.instanceType(instanceType)
.machineImage(MachineImage.latestAmazonLinux2023())
.init(CloudFormationInit.fromElements(InitService.systemdConfigFile("simpleserver", SystemdConfigFileOptions.builder()
.command("/usr/bin/python3 -m http.server 8080")
.cwd("/var/www/html")
.build()), InitService.enable("simpleserver", InitServiceOptions.builder()
.serviceManager(ServiceManager.SYSTEMD)
.build()), InitFile.fromString("/var/www/html/index.html", "Hello! It's working!")))
.build();
csharp
// Install: dotnet add package Amazon.CDK.Lib
Vpc vpc;
InstanceType instanceType;
new Instance(this, "Instance", new InstanceProps {
Vpc = vpc,
InstanceType = instanceType,
MachineImage = MachineImage.LatestAmazonLinux2023(),
Init = CloudFormationInit.FromElements(InitService.SystemdConfigFile("simpleserver", new SystemdConfigFileOptions {
Command = "/usr/bin/python3 -m http.server 8080",
Cwd = "/var/www/html"
}), InitService.Enable("simpleserver", new InitServiceOptions {
ServiceManager = ServiceManager.SYSTEMD
}), InitFile.FromString("/var/www/html/index.html", "Hello! It's working!"))
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var vpc Vpc
var instanceType InstanceType
ec2.NewInstance(this, jsii.String("Instance"), &InstanceProps{
Vpc: Vpc,
InstanceType: InstanceType,
MachineImage: ec2.MachineImage_LatestAmazonLinux2023(),
Init: ec2.CloudFormationInit_FromElements(ec2.InitService_SystemdConfigFile(jsii.String("simpleserver"), &SystemdConfigFileOptions{
Command: jsii.String("/usr/bin/python3 -m http.server 8080"),
Cwd: jsii.String("/var/www/html"),
}), ec2.InitService_Enable(jsii.String("simpleserver"), &InitServiceOptions{
ServiceManager: ec2.ServiceManager_SYSTEMD,
}), ec2.InitFile_FromString(jsii.String("/var/www/html/index.html"), jsii.String("Hello! It's working!"))),
})
Properties
Returns the init element type for this element.
Methods
fromAsset(targetFileName: string, path: string, options?: InitFileAssetOptions): InitFile // chainable@staticmethod def from_asset(target_file_name: str, path: str, options: typing.Optional[InitFileAssetOptions] = None) -> InitFilepublic static InitFile fromAsset(String targetFileName, String path, InitFileAssetOptions options)public static InitFile FromAsset(string targetFileName, string path, InitFileAssetOptions? options = null)func FromAsset(TargetFileName *string, Path *string, Options InitFileAssetOptions) InitFile 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@staticmethod def from_existing_asset(target_file_name: str, asset: Asset, options: typing.Optional[InitFileOptions] = None) -> InitFilepublic static InitFile fromExistingAsset(String targetFileName, Asset asset, InitFileOptions options)public static InitFile FromExistingAsset(string targetFileName, Asset asset, InitFileOptions? options = null)func FromExistingAsset(TargetFileName *string, Asset Asset, Options InitFileOptions) InitFile static #Use a file from an asset at instance startup time.
fromFileInline(targetFileName: string, sourceFileName: string, options?: InitFileOptions): InitFile // chainable@staticmethod def from_file_inline(target_file_name: str, source_file_name: str, options: typing.Optional[InitFileOptions] = None) -> InitFilepublic static InitFile fromFileInline(String targetFileName, String sourceFileName, InitFileOptions options)public static InitFile FromFileInline(string targetFileName, string sourceFileName, InitFileOptions? options = null)func FromFileInline(TargetFileName *string, SourceFileName *string, Options InitFileOptions) InitFile 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@staticmethod def from_object(file_name: str, obj: typing.Mapping[str, Any], options: typing.Optional[InitFileOptions] = None) -> InitFilepublic static InitFile fromObject(String fileName, Map<String, Object> obj, InitFileOptions options)public static InitFile FromObject(string fileName, IDictionary<string, object> obj, InitFileOptions? options = null)func FromObject(FileName *string, Obj *map[string]interface{}, Options InitFileOptions) InitFile 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@staticmethod def from_s3_object(file_name: str, bucket: IBucket, key: str, options: typing.Optional[InitFileOptions] = None) -> InitFilepublic static InitFile fromS3Object(String fileName, IBucket bucket, String key, InitFileOptions options)public static InitFile FromS3Object(string fileName, IBucket bucket, string key, InitFileOptions? options = null)func FromS3Object(FileName *string, Bucket IBucket, Key *string, Options InitFileOptions) InitFile static #Download a file from an S3 bucket at instance startup time.
fromString(fileName: string, content: string, options?: InitFileOptions): InitFile // chainable@staticmethod def from_string(file_name: str, content: str, options: typing.Optional[InitFileOptions] = None) -> InitFilepublic static InitFile fromString(String fileName, String content, InitFileOptions options)public static InitFile FromString(string fileName, string content, InitFileOptions? options = null)func FromString(FileName *string, Content *string, Options InitFileOptions) InitFile static #Use a literal string as the file content.
fromUrl(fileName: string, url: string, options?: InitFileOptions): InitFile // chainable@staticmethod def from_url(file_name: str, url: str, options: typing.Optional[InitFileOptions] = None) -> InitFilepublic static InitFile fromUrl(String fileName, String url, InitFileOptions options)public static InitFile FromUrl(string fileName, string url, InitFileOptions? options = null)func FromUrl(FileName *string, Url *string, Options InitFileOptions) InitFile static #Download from a URL at instance startup time.
symlink(fileName: string, target: string, options?: InitFileOptions): InitFile // chainable@staticmethod def symlink(file_name: str, target: str, options: typing.Optional[InitFileOptions] = None) -> InitFilepublic static InitFile symlink(String fileName, String target, InitFileOptions options)public static InitFile Symlink(string fileName, string target, InitFileOptions? options = null)func Symlink(FileName *string, Target *string, Options InitFileOptions) InitFile static #Write a symlink with the given symlink target.