# PlacementGroup

Defines a placement group.

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

Extends: [Construct](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.Construct.html) › [Resource](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.Resource.html) › PlacementGroup
Implements: [IResource](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.IResource.html), [IPlacementGroup](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.IPlacementGroup.html), [IEnvironmentAware](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.interfaces.IEnvironmentAware.html), [IPlacementGroupRef](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.interfaces.aws_ec2.IPlacementGroupRef.html), IConstruct, IDependable

- CloudFormation: [`AWS::EC2::PlacementGroup`](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-ec2-placementgroup.html)
- L1 type: [`CfnPlacementGroup`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.CfnPlacementGroup.html)
- Source: [`aws-ec2/lib/placement-group.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-ec2/lib/placement-group.ts#L156)
- Type last changed: v2.224.0 · 13 Nov 2025

## Example

TypeScript:

```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:

```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:

```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();
```

C# / .NET:

```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
});
```

Golang:

```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

TypeScript:

```ts
new PlacementGroup(scope: Construct, id: string, props?: PlacementGroupProps)
```

Python:

```python
PlacementGroup(scope: Construct, id: str, props: typing.Optional[PlacementGroupProps] = None)
```

Java:

```java
new PlacementGroup(Construct scope, String id, PlacementGroupProps props)
```

C# / .NET:

```csharp
new PlacementGroup(Construct scope, string id, PlacementGroupProps? props = null)
```

Golang:

```go
NewPlacementGroup(Scope Construct, Id *string, Props PlacementGroupProps)
```

## Construct Props

- `partitions`: number = 0 — The number of partitions.

  Valid only when Strategy is set to partition.

- `placementGroupName`: string — the name of this placement group.

  **Default:** generated by CFN

- `spreadLevel`: [PlacementGroupSpreadLevel](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.PlacementGroupSpreadLevel.html) — Places instances on distinct hardware.

  **Default:** no spread level

  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.

- `strategy`: [PlacementGroupStrategy](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.PlacementGroupStrategy.html) — Which strategy to use when launching instances.

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

## Properties

- `env`: [ResourceEnvironment](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.interfaces.ResourceEnvironment.html) — The environment this resource belongs to.

  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.

- `node`: [Node](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.Node.html) — The tree node.

- `partitions`: number — The number of partitions.

  Valid only when Strategy is set to PARTITION.

- `placementGroupName`: string — The name of this placement group.

- `placementGroupRef`: [PlacementGroupReference](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.interfaces.aws_ec2.PlacementGroupReference.html) — A reference to a PlacementGroup resource.

- `PROPERTY_INJECTION_ID`: string (static) — Uniquely identifies this class.

- `spreadLevel`: [PlacementGroupSpreadLevel](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.PlacementGroupSpreadLevel.html) — Places instances on distinct hardware.

  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.

- `stack`: [Stack](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.Stack.html) — The stack in which this resource is defined.

- `strategy`: [PlacementGroupStrategy](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.PlacementGroupStrategy.html) — Which strategy to use when launching instances.

## Methods

- `applyCrossStackReferenceStrength(strength: ReferenceStrength): void` — Override the cross-stack reference strength for this resource.

  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**

  - `strength` — - The reference strength to use for this resource.

- `applyRemovalPolicy(policy: RemovalPolicy): void` — Apply the given removal policy to this resource.

  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` (static) — Import a PlacementGroup by its arn.

- `isConstruct(x: any): boolean` (static) — Checks if `x` is a construct.

  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**

  - `x` — Any object.

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

- `isOwnedResource(construct: IConstruct): boolean` (static) — Returns true if the construct was created by CDK, and false otherwise.

- `isResource(construct: IConstruct): boolean` (static) — Check whether the given construct is a Resource.

- `toString(): string` — Returns a string representation of this construct.

- `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.
