Skip to content
v2.273.0

InitConfig Class ​

A collection of configuration elements.

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,
  },
});
python
# vpc: ec2.Vpc
# instance_type: ec2.InstanceType
# machine_image: ec2.IMachineImage


ec2.Instance(self, "Instance",
    vpc=vpc,
    instance_type=instance_type,
    machine_image=machine_image,

    # Showing the most complex setup, if you have simpler requirements
    # you can use `CloudFormationInit.fromElements()`.
    init=ec2.CloudFormationInit.from_config_sets(
        config_sets={
            # Applies the configs below in this order
            "default": ["yumPreinstall", "config"]
        },
        configs={
            "yum_preinstall": ec2.InitConfig([
                # Install an Amazon Linux package using yum
                ec2.InitPackage.yum("git")
            ]),
            "config": ec2.InitConfig([
                # Create a JSON file from tokens (can also create other files)
                ec2.InitFile.from_object("/etc/stack.json", {
                    "stack_id": Stack.of(self).stack_id,
                    "stack_name": Stack.of(self).stack_name,
                    "region": Stack.of(self).region
                }),

                # Create a group and user
                ec2.InitGroup.from_name("my-group"),
                ec2.InitUser.from_name("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")
            ])
        }
    ),
    init_options=ec2.ApplyCloudFormationInitOptions(
        # Optional, which configsets to activate (['default'] by default)
        config_sets=["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)
        include_url=True,

        # Optional, whether to include the --role argument when running cfn-init and cfn-signal commands (false by default)
        include_role=True
    )
)
java
Vpc vpc;
InstanceType instanceType;
IMachineImage machineImage;


Instance.Builder.create(this, "Instance")
        .vpc(vpc)
        .instanceType(instanceType)
        .machineImage(machineImage)

        // Showing the most complex setup, if you have simpler requirements
        // you can use `CloudFormationInit.fromElements()`.
        .init(CloudFormationInit.fromConfigSets(ConfigSetProps.builder()
                .configSets(Map.of(
                        // Applies the configs below in this order
                        "default", List.of("yumPreinstall", "config")))
                .configs(Map.of(
                        "yumPreinstall", new InitConfig(List.of(InitPackage.yum("git"))),
                        "config", new InitConfig(List.of(InitFile.fromObject("/etc/stack.json", Map.of(
                                "stackId", Stack.of(this).getStackId(),
                                "stackName", Stack.of(this).getStackName(),
                                "region", Stack.of(this).getRegion())), InitGroup.fromName("my-group"), InitUser.fromName("my-user"), 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")))))
                .build()))
        .initOptions(ApplyCloudFormationInitOptions.builder()
                // Optional, which configsets to activate (['default'] by default)
                .configSets(List.of("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)
                .build())
        .build();
csharp
// Install: dotnet add package Amazon.CDK.Lib
Vpc vpc;
InstanceType instanceType;
IMachineImage machineImage;


new Instance(this, "Instance", new InstanceProps {
    Vpc = vpc,
    InstanceType = instanceType,
    MachineImage = machineImage,

    // Showing the most complex setup, if you have simpler requirements
    // you can use `CloudFormationInit.fromElements()`.
    Init = CloudFormationInit.FromConfigSets(new ConfigSetProps {
        ConfigSets = new Dictionary<string, string[]> {
            // Applies the configs below in this order
            { "default", new [] { "yumPreinstall", "config" } }
        },
        Configs = new Dictionary<string, InitConfig> {
            { "yumPreinstall", new InitConfig(new [] { InitPackage.Yum("git") }) },
            { "config", new InitConfig(new [] { InitFile.FromObject("/etc/stack.json", new Dictionary<string, object> {
                { "stackId", Stack.Of(this).StackId },
                { "stackName", Stack.Of(this).StackName },
                { "region", Stack.Of(this).Region }
            }), InitGroup.FromName("my-group"), InitUser.FromName("my-user"), 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 = new ApplyCloudFormationInitOptions {
        // Optional, which configsets to activate (['default'] by default)
        ConfigSets = new [] { "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
    }
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var vpc Vpc
var instanceType InstanceType
var machineImage IMachineImage


ec2.NewInstance(this, jsii.String("Instance"), &InstanceProps{
	Vpc: Vpc,
	InstanceType: InstanceType,
	MachineImage: MachineImage,

	// Showing the most complex setup, if you have simpler requirements
	// you can use `CloudFormationInit.fromElements()`.
	Init: ec2.CloudFormationInit_FromConfigSets(&ConfigSetProps{
		ConfigSets: map[string][]*string{
			// Applies the configs below in this order
			"default": []*string{
				jsii.String("yumPreinstall"),
				jsii.String("config"),
			},
		},
		Configs: map[string]InitConfig{
			"yumPreinstall": ec2.NewInitConfig([]InitElement{
				ec2.InitPackage_yum(jsii.String("git")),
			}),
			"config": ec2.NewInitConfig([]InitElement{
				ec2.InitFile_fromObject(jsii.String("/etc/stack.json"), map[string]interface{}{
					"stackId": awscdk.*stack_of(this).stackId,
					"stackName": awscdk.*stack_of(this).stackName,
					"region": awscdk.*stack_of(this).region,
				}),
				ec2.InitGroup_fromName(jsii.String("my-group")),
				ec2.InitUser_fromName(jsii.String("my-user")),
				ec2.InitPackage_rpm(jsii.String("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: &ApplyCloudFormationInitOptions{
		// Optional, which configsets to activate (['default'] by default)
		ConfigSets: []*string{
			jsii.String("default"),
		},

		// Optional, how long the installation is expected to take (5 minutes by default)
		Timeout: awscdk.Duration_Minutes(jsii.Number(30)),

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

		// Optional, whether to include the --role argument when running cfn-init and cfn-signal commands (false by default)
		IncludeRole: jsii.Boolean(true),
	},
})

Initializer ​

ts
new InitConfig(elements: InitElement[])
python
InitConfig(elements: typing.List[InitElement])
java
new InitConfig(List<InitElement> elements)
csharp
new InitConfig(InitElement[] elements)
go
NewInitConfig(Elements *[]InitElement)

Methods ​

add(...elements: InitElement[]): voiddef add(self, elements: InitElement) -> Nonepublic void add(InitElement elements)public void Add(InitElement elements)func Add(Elements InitElement)#
Add one or more elements to the config.
isEmpty(): booleandef is_empty(self, ) -> boolpublic Boolean isEmpty()public bool IsEmpty()func IsEmpty() *bool#
Whether this configset has elements or not.