# EcsOptimizedImageOptions

Additional configuration properties for EcsOptimizedImage factory functions.

Used as argument in: [EcsOptimizedImage.amazonLinux](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.EcsOptimizedImage.html#amazonlinux), [EcsOptimizedImage.amazonLinux2](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.EcsOptimizedImage.html#amazonlinux2), EcsOptimizedImage.amazonLinux2023, EcsOptimizedImage.windows

- Source: [`aws-ecs/lib/amis.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-ecs/lib/amis.ts#L191)
- Type last changed: v2.201.0 · 13 Jun 2025

## Example

TypeScript:

```ts
declare const vpc: ec2.Vpc;
const autoScalingGroup = new autoscaling.AutoScalingGroup(this, 'ASG', {
  machineImage: ecs.EcsOptimizedImage.amazonLinux({ cachedInContext: true }),
  vpc,
  instanceType: new ec2.InstanceType('t2.micro'),
});
```

Python:

```python
# vpc: ec2.Vpc

auto_scaling_group = autoscaling.AutoScalingGroup(self, "ASG",
    machine_image=ecs.EcsOptimizedImage.amazon_linux(cached_in_context=True),
    vpc=vpc,
    instance_type=ec2.InstanceType("t2.micro")
)
```

Java:

```java
Vpc vpc;

AutoScalingGroup autoScalingGroup = AutoScalingGroup.Builder.create(this, "ASG")
        .machineImage(EcsOptimizedImage.amazonLinux(EcsOptimizedImageOptions.builder().cachedInContext(true).build()))
        .vpc(vpc)
        .instanceType(new InstanceType("t2.micro"))
        .build();
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
Vpc vpc;

var autoScalingGroup = new AutoScalingGroup(this, "ASG", new AutoScalingGroupProps {
    MachineImage = EcsOptimizedImage.AmazonLinux(new EcsOptimizedImageOptions { CachedInContext = true }),
    Vpc = vpc,
    InstanceType = new InstanceType("t2.micro")
});
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var vpc Vpc

autoScalingGroup := autoscaling.NewAutoScalingGroup(this, jsii.String("ASG"), &AutoScalingGroupProps{
	MachineImage: ecs.EcsOptimizedImage_AmazonLinux(&EcsOptimizedImageOptions{
		CachedInContext: jsii.Boolean(true),
	}),
	Vpc: Vpc,
	InstanceType: ec2.NewInstanceType(jsii.String("t2.micro")),
})
```

## Properties

- `additionalCacheKey`: string — Adds an additional discriminator to the `cdk.context.json` cache key.

  **Default:** no additional cache key

- `cachedInContext`: boolean = false — Whether the AMI ID is cached to be stable between deployments.

  By default, the newest image is used on each deployment. This will cause
  instances to be replaced whenever a new version is released, and may cause
  downtime if there aren't enough running instances in the AutoScalingGroup
  to reschedule the tasks on.

  If set to true, the AMI ID will be cached in `cdk.context.json` and the
  same value will be used on future runs. Your instances will not be replaced
  but your AMI version will grow old over time. To refresh the AMI lookup,
  you will have to evict the value from the cache using the `cdk context`
  command. See https://docs.aws.amazon.com/cdk/latest/guide/context.html for
  more information.

  Can not be set to `true` in environment-agnostic stacks.
