Skip to content
v2.275.0

PlacementGroup L2 construct ​

Defines a placement group.

Placement groups give you fine-grained control over where your instances are provisioned.

Example ​

ts
declare const instanceType: ec2.InstanceType;

const pg = new ec2.PlacementGroup(this, 'test-pg', {
  strategy: ec2.PlacementGroupStrategy.SPREAD,
});

new ec2.Instance(this, 'Instance', {
  vpc,
  instanceType,
  machineImage: ec2.MachineImage.latestAmazonLinux2023(),
  placementGroup: pg,
});
python
# instance_type: ec2.InstanceType


pg = ec2.PlacementGroup(self, "test-pg",
    strategy=ec2.PlacementGroupStrategy.SPREAD
)

ec2.Instance(self, "Instance",
    vpc=vpc,
    instance_type=instance_type,
    machine_image=ec2.MachineImage.latest_amazon_linux2023(),
    placement_group=pg
)
java
InstanceType instanceType;


PlacementGroup pg = PlacementGroup.Builder.create(this, "test-pg")
        .strategy(PlacementGroupStrategy.SPREAD)
        .build();

Instance.Builder.create(this, "Instance")
        .vpc(vpc)
        .instanceType(instanceType)
        .machineImage(MachineImage.latestAmazonLinux2023())
        .placementGroup(pg)
        .build();
csharp
// Install: dotnet add package Amazon.CDK.Lib
InstanceType instanceType;


var pg = new PlacementGroup(this, "test-pg", new PlacementGroupProps {
    Strategy = PlacementGroupStrategy.SPREAD
});

new Instance(this, "Instance", new InstanceProps {
    Vpc = vpc,
    InstanceType = instanceType,
    MachineImage = MachineImage.LatestAmazonLinux2023(),
    PlacementGroup = pg
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var instanceType InstanceType


pg := ec2.NewPlacementGroup(this, jsii.String("test-pg"), &PlacementGroupProps{
	Strategy: ec2.PlacementGroupStrategy_SPREAD,
})

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

Initializer ​

ts
new PlacementGroup(scope: Construct, id: string, props?: PlacementGroupProps)
python
PlacementGroup(scope: Construct, id: str, props: typing.Optional[PlacementGroupProps] = None)
java
new PlacementGroup(Construct scope, String id, PlacementGroupProps props)
csharp
new PlacementGroup(Construct scope, string id, PlacementGroupProps? props = null)
go
NewPlacementGroup(Scope Construct, Id *string, Props PlacementGroupProps)

Construct Props ​

partitionsoptionalnumberpartitionsoptionalint | floatpartitionsoptionalNumberPartitionsoptionaldoublePartitionsoptional*float64= 0#
The number of partitions.

Valid only when Strategy is set to partition.

placementGroupNameoptionalstringplacement_group_nameoptionalstrplacementGroupNameoptionalStringPlacementGroupNameoptionalstringPlacementGroupNameoptional*string#
the name of this placement group.

Default: generated by CFN

spreadLeveloptionalPlacementGroupSpreadLevelspread_leveloptionalPlacementGroupSpreadLevelspreadLeveloptionalPlacementGroupSpreadLevelSpreadLeveloptionalPlacementGroupSpreadLevelSpreadLeveloptionalPlacementGroupSpreadLevel#
Places instances on distinct hardware.

Default: no spread level

notes

Spread placement groups are recommended for applications that have a small number of critical instances that should be kept separate from each other. Launching instances in a spread level placement group reduces the risk of simultaneous failures that might occur when instances share the same equipment. Spread level placement groups provide access to distinct hardware, and are therefore suitable for mixing instance types or launching instances over time. If you start or launch an instance in a spread placement group and there is insufficient unique hardware to fulfill the request, the request fails. Amazon EC2 makes more distinct hardware available over time, so you can try your request again later. Placement groups can spread instances across racks or hosts. You can use host level spread placement groups only with AWS Outposts.

strategyoptionalPlacementGroupStrategystrategyoptionalPlacementGroupStrategystrategyoptionalPlacementGroupStrategyStrategyoptionalPlacementGroupStrategyStrategyoptionalPlacementGroupStrategy#
Which strategy to use when launching instances.

Default: PlacementGroupStrategy.PARTITION if partitions is defined, CLUSTER otherwise

Properties ​

The environment this resource belongs to.
notes

For resources that are created and managed in a Stack (those created by creating new class instances like new Role(), new Bucket(), etc.), this is always the same as the environment of the stack they belong to.

For referenced resources (those obtained from referencing methods like Role.fromRoleArn(), Bucket.fromBucketName(), etc.), they might be different than the stack they were imported into.

nodeNodenodeNodenodeNodeNodeNodeNodeNode#
The tree node.
partitionsmay be undefinednumberpartitionsmay be undefinedint | floatpartitionsmay be undefinedNumberPartitionsmay be undefineddoublePartitionsmay be undefined*float64#
The number of partitions.

Valid only when Strategy is set to PARTITION.

placementGroupNamestringplacement_group_namestrplacementGroupNameStringPlacementGroupNamestringPlacementGroupName*string#
The name of this placement group.
placementGroupRefPlacementGroupReferenceplacement_group_refPlacementGroupReferenceplacementGroupRefPlacementGroupReferencePlacementGroupRefPlacementGroupReferencePlacementGroupRefPlacementGroupReference#
A reference to a PlacementGroup resource.
PROPERTY_INJECTION_IDstringproperty_injection_idstrPROPERTY_INJECTION_IDStringPROPERTY_INJECTION_IDstringPROPERTY_INJECTION_ID*stringstatic#
Uniquely identifies this class.
spreadLevelmay be undefinedPlacementGroupSpreadLevelspread_levelmay be undefinedPlacementGroupSpreadLevelspreadLevelmay be undefinedPlacementGroupSpreadLevelSpreadLevelmay be undefinedPlacementGroupSpreadLevelSpreadLevelmay be undefinedPlacementGroupSpreadLevel#
Places instances on distinct hardware.
notes

Spread placement groups are recommended for applications that have a small number of critical instances that should be kept separate from each other. Launching instances in a spread level placement group reduces the risk of simultaneous failures that might occur when instances share the same equipment. Spread level placement groups provide access to distinct hardware, and are therefore suitable for mixing instance types or launching instances over time. If you start or launch an instance in a spread placement group and there is insufficient unique hardware to fulfill the request, the request fails. Amazon EC2 makes more distinct hardware available over time, so you can try your request again later. Placement groups can spread instances across racks or hosts. You can use host level spread placement groups only with AWS Outposts.

stackStackstackStackstackStackStackStackStackStack#
The stack in which this resource is defined.
strategymay be undefinedPlacementGroupStrategystrategymay be undefinedPlacementGroupStrategystrategymay be undefinedPlacementGroupStrategyStrategymay be undefinedPlacementGroupStrategyStrategymay be undefinedPlacementGroupStrategy#
Which strategy to use when launching instances.

Methods ​

applyCrossStackReferenceStrength(strength: ReferenceStrength): voiddef apply_cross_stack_reference_strength(self, strength: ReferenceStrength) -> Nonepublic void applyCrossStackReferenceStrength(ReferenceStrength strength)public void ApplyCrossStackReferenceStrength(ReferenceStrength strength)func ApplyCrossStackReferenceStrength(Strength ReferenceStrength)#
Override the cross-stack reference strength for this resource.
notes · 1 parameter

When set, any cross-stack reference to this resource will use the specified mechanism instead of the global default determined by the @aws-cdk/core:defaultCrossStackReferences context key. This is useful for selectively weakening specific references to avoid the "deadly embrace" problem without changing the app-wide default.

Parameters

  • strengthstrengthstrengthstrengthStrength — - The reference strength to use for this resource.
applyRemovalPolicy(policy: RemovalPolicy): voiddef apply_removal_policy(self, policy: RemovalPolicy) -> Nonepublic void applyRemovalPolicy(RemovalPolicy policy)public void ApplyRemovalPolicy(RemovalPolicy policy)func ApplyRemovalPolicy(Policy RemovalPolicy)#
Apply the given removal policy to this resource.
notes

The Removal Policy controls what happens to this resource when it stops being managed by CloudFormation, either because you've removed it from the CDK application or because you've made a change that requires the resource to be replaced.

The resource can be deleted (RemovalPolicy.DESTROY), or left in your AWS account for data recovery and cleanup later (RemovalPolicy.RETAIN).

fromPlacementGroupName(scope: Construct, id: string, placementGroupName: string): IPlacementGroup@staticmethod def from_placement_group_name(scope: Construct, id: str, placement_group_name: str) -> IPlacementGrouppublic static IPlacementGroup fromPlacementGroupName(Construct scope, String id, String placementGroupName)public static IPlacementGroup FromPlacementGroupName(Construct scope, string id, string placementGroupName)func FromPlacementGroupName(Scope Construct, Id *string, PlacementGroupName *string) IPlacementGroupstatic#
Import a PlacementGroup by its arn.
isConstruct(x: any): boolean@staticmethod def is_construct(x: Any) -> boolpublic static Boolean isConstruct(Object x)public static bool IsConstruct(object x)func IsConstruct(X interface{}) *boolstatic#
Checks if `x` is a construct.
notes · 1 parameter · returns

Use this method instead of instanceof to properly detect Construct instances, even when the construct library is symlinked.

Explanation: in JavaScript, multiple copies of the constructs library on disk are seen as independent, completely different libraries. As a consequence, the class Construct in each copy of the constructs library is seen as a different class, and an instance of one class will not test as instanceof the other class. npm install will not create installations like this, but users may manually symlink construct libraries together or use a monorepo tool: in those cases, multiple copies of the constructs library can be accidentally installed, and instanceof will behave unpredictably. It is safest to avoid using instanceof, and using this type-testing method instead.

Parameters

  • xxxxX — Any object.

Returns: true if x is an object created from a class which extends Construct.

isOwnedResource(construct: IConstruct): boolean@staticmethod def is_owned_resource(construct: IConstruct) -> boolpublic static Boolean isOwnedResource(IConstruct construct)public static bool IsOwnedResource(IConstruct construct)func IsOwnedResource(Construct IConstruct) *boolstatic#
Returns true if the construct was created by CDK, and false otherwise.
isResource(construct: IConstruct): boolean@staticmethod def is_resource(construct: IConstruct) -> boolpublic static Boolean isResource(IConstruct construct)public static bool IsResource(IConstruct construct)func IsResource(Construct IConstruct) *boolstatic#
Check whether the given construct is a Resource.
toString(): stringdef to_string(self, ) -> strpublic String toString()public string ToString()func ToString() *string#
Returns a string representation of this construct.
with(...mixins: IMixin[]): IConstructdef with(self, mixins: IMixin) -> IConstructpublic IConstruct with(IMixin mixins)public IConstruct With(IMixin mixins)func With(Mixins IMixin) IConstruct#
Applies one or more mixins to this construct.

Mixins are applied in order. The list of constructs is captured at the start of the call, so constructs added by a mixin will not be visited. Use multiple with() calls if subsequent mixins should apply to added constructs.