# BastionHostLinux

This creates a linux bastion host you can use to connect to other instances or services in your VPC.

The recommended way to connect to the bastion host is by using AWS Systems Manager Session Manager.

The operating system is Amazon Linux 2 with the latest SSM agent installed

You can also configure this bastion host to allow connections via SSH

Extends: Construct › Resource › BastionHostLinux
Implements: IResource, IConnectable, IInstance, IGrantable, IEnvironmentAware, IInstanceRef, IConstruct, IDependable

- CloudFormation: [`AWS::EC2::Instance`](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-ec2-instance.html)
- L1 type: [`CfnInstance`](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.CfnInstance.html)
- Source: [`aws-ec2/lib/bastion-host.ts`](https://github.com/aws/aws-cdk/blob/v2.275.0/packages/aws-cdk-lib/aws-ec2/lib/bastion-host.ts#L147)
- Type last changed: v2.236.0 · 23 Jan 2026

## Example

```ts
const host = new ec2.BastionHostLinux(this, 'BastionHost', {
  vpc,
  blockDevices: [{
    deviceName: '/dev/sdh',
    volume: ec2.BlockDeviceVolume.ebs(10, {
      encrypted: true,
    }),
  }],
});
```

## Initializer

```ts
new BastionHostLinux(scope: Construct, id: string, props: BastionHostLinuxProps)
```

## Construct Props

- `availabilityZone`: string — In which AZ to place the instance within the VPC.

  **Default:** Random zone.

- `blockDevices`: [BlockDevice](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.BlockDevice.html)[] — Specifies how block devices are exposed to the instance. You can specify virtual devices and EBS volumes.

  **Default:** Uses the block device mapping of the AMI

  Each instance that is launched has an associated root device volume,
  either an Amazon EBS volume or an instance store volume.
  You can use block device mappings to specify additional EBS volumes or
  instance store volumes to attach to an instance when it is launched.

- `init`: [CloudFormationInit](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.CloudFormationInit.html) — Apply the given CloudFormation Init configuration to the instance at startup.

  **Default:** no CloudFormation init

- `initOptions`: [ApplyCloudFormationInitOptions](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.ApplyCloudFormationInitOptions.html) — Use the given options for applying CloudFormation Init.

  **Default:** default options

  Describes the configsets to use and the timeout to wait

- `instanceName`: string = 'BastionHost' — The name of the instance.

- `instanceType`: [InstanceType](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.InstanceType.html) = 't3.nano' — Type of instance to launch.

- `machineImage`: [IMachineImage](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.IMachineImage.html) — The machine image to use, assumed to have SSM Agent preinstalled.

  **Default:** An Amazon Linux 2023 image if the `@aws-cdk/aws-ec2:bastionHostUseAmazonLinux2023ByDefault` feature flag is enabled. Otherwise, an Amazon Linux 2 image. In both cases, the image is kept up-to-date automatically (the instance may be replaced on every deployment) and already has SSM Agent installed.

- `requireImdsv2`: boolean = false — Whether IMDSv2 should be required on this instance.

- `securityGroup`: [ISecurityGroup](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.ISecurityGroup.html) — Security Group to assign to this instance.

  **Default:** create new security group with no inbound and all outbound traffic allowed

- `subnetSelection`: [SubnetSelection](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.SubnetSelection.html) — Select the subnets to run the bastion host in.

  **Default:** private subnets of the supplied VPC

  Set this to PUBLIC if you need to connect to this instance via the internet and cannot use SSM.
  You have to allow port 22 manually by using the connections field

- `userDataCausesReplacement`: boolean — Determines whether changes to the UserData will force instance replacement.

  **Default:** `true` if `initOptions` is specified, otherwise `false`.

  Depending on the EC2 instance type, modifying the UserData may either restart
  or replace the instance:

  - Instance store-backed instances are replaced.
  - EBS-backed instances are restarted.

  Note that by default, restarting does not execute the updated UserData, so an alternative
  mechanism is needed to ensure the instance re-executes the UserData.

  When set to `true`, the instance's Logical ID will depend on the UserData, causing
  CloudFormation to replace the instance if the UserData changes.

- `vpc`: [IVpc](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.IVpc.html) (required) — VPC to launch the instance in.

## Properties

- `connections`: [Connections](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.Connections.html) — Allows specify security group connections for the instance.

- `env`: [ResourceEnvironment](/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.

- `grantPrincipal`: [IPrincipal](/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IPrincipal.html) — The principal to grant permissions to.

- `instance`: [Instance](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.Instance.html) — The underlying instance resource.

- `instanceAvailabilityZone`: string — The availability zone the instance was launched in.

- `instanceId`: string — The instance's ID.

- `instancePrivateDnsName`: string — Private DNS name for this instance.

- `instancePrivateIp`: string — Private IP for this instance.

- `instancePublicDnsName`: string — Publicly-routable DNS name for this instance.

  (May be an empty string if the instance does not have a public name).

- `instancePublicIp`: string — Publicly-routable IP address for this instance.

  (May be an empty string if the instance does not have a public IP).

- `instanceRef`: [InstanceReference](/cdk/api/v2/docs/aws-cdk-lib.interfaces.aws_ec2.InstanceReference.html) — A reference to a Instance resource.

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

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

- `role`: [IRole](/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IRole.html) — The IAM role assumed by the instance.

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

## Methods

- `allowSshAccessFrom(...peer: IPeer[]): void` — Allow SSH access from the given peer or peers.

  Necessary if you want to connect to the instance using ssh. If not
  called, you should use SSM Session Manager to connect to the instance.

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

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