class MachineImage
Language | Type name |
---|---|
.NET | Amazon.CDK.AWS.EC2.MachineImage |
Go | github.com/aws/aws-cdk-go/awscdk/v2/awsec2#MachineImage |
Java | software.amazon.awscdk.services.ec2.MachineImage |
Python | aws_cdk.aws_ec2.MachineImage |
TypeScript (source) | aws-cdk-lib » aws_ec2 » MachineImage |
Factory functions for standard Amazon Machine Image objects.
Example
declare const vpc: ec2.Vpc;
const mySecurityGroup = new ec2.SecurityGroup(this, 'SecurityGroup', { vpc });
new autoscaling.AutoScalingGroup(this, 'ASG', {
vpc,
instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
machineImage: ec2.MachineImage.latestAmazonLinux2(),
securityGroup: mySecurityGroup,
});
Initializer
new MachineImage()
Methods
Name | Description |
---|---|
static from | An image specified in SSM parameter store. |
static generic | A Linux image where you specify the AMI ID for every region. |
static generic | A Windows image where you specify the AMI ID for every region. |
static latest | An Amazon Linux image that is automatically kept up-to-date. |
static latest | An Amazon Linux 2 image that is automatically kept up-to-date. |
static latest | An Amazon Linux 2022 image that is automatically kept up-to-date. |
static latest | An Amazon Linux 2023 image that is automatically kept up-to-date. |
static latest | A Windows image that is automatically kept up-to-date. |
static lookup(props) | Look up a shared Machine Image using DescribeImages. |
static resolve | An image specified in SSM parameter store that will be resolved at instance launch time. |
SsmParameter(parameterName, options?)
static frompublic static fromSsmParameter(parameterName: string, options?: SsmParameterImageOptions): IMachineImage
Parameters
- parameterName
string
- options
Ssm
Parameter Image Options
Returns
An image specified in SSM parameter store.
By default, the SSM parameter is refreshed at every deployment, causing your instances to be replaced whenever a new version of the AMI is released.
Pass { cachedInContext: true }
to keep the AMI ID stable. If you do, you
will have to remember to periodically invalidate the context to refresh
to the newest AMI ID.
Linux(amiMap, props?)
static genericpublic static genericLinux(amiMap: { [string]: string }, props?: GenericLinuxImageProps): IMachineImage
Parameters
- amiMap
{ [string]: string }
— For every region where you are deploying the stack, specify the AMI ID for that region. - props
Generic
— Customize the image by supplying additional props.Linux Image Props
Returns
A Linux image where you specify the AMI ID for every region.
Windows(amiMap, props?)
static genericpublic static genericWindows(amiMap: { [string]: string }, props?: GenericWindowsImageProps): IMachineImage
Parameters
- amiMap
{ [string]: string }
— For every region where you are deploying the stack, specify the AMI ID for that region. - props
Generic
— Customize the image by supplying additional props.Windows Image Props
Returns
A Windows image where you specify the AMI ID for every region.
AmazonLinux(props?)
static latestpublic static latestAmazonLinux(props?: AmazonLinuxImageProps): IMachineImage
⚠️ Deprecated: use MachineImage.latestAmazonLinux2 instead
Parameters
- props
Amazon
Linux Image Props
Returns
An Amazon Linux image that is automatically kept up-to-date.
This Machine Image automatically updates to the latest version on every deployment. Be aware this will cause your instances to be replaced when a new version of the image becomes available. Do not store stateful information on the instance if you are using this image.
N.B.: "latest" in the name of this function indicates that it always uses the most recent image of a particular generation of Amazon Linux, not that it uses the "latest generation". For backwards compatibility, this function uses Amazon Linux 1 if no generation is specified.
Specify the desired generation using the generation
property:
ec2.MachineImage.latestAmazonLinux({
// Use Amazon Linux 2
generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2,
})
AmazonLinux2(props?)
static latestpublic static latestAmazonLinux2(props?: AmazonLinux2ImageSsmParameterProps): IMachineImage
Parameters
Returns
An Amazon Linux 2 image that is automatically kept up-to-date.
This Machine Image automatically updates to the latest version on every deployment. Be aware this will cause your instances to be replaced when a new version of the image becomes available. Do not store stateful information on the instance if you are using this image.
AmazonLinux2022(props?)
static latestpublic static latestAmazonLinux2022(props?: AmazonLinux2022ImageSsmParameterProps): IMachineImage
⚠️ Deprecated: - use latestAmazonLinux2023() instead
Parameters
Returns
An Amazon Linux 2022 image that is automatically kept up-to-date.
This Machine Image automatically updates to the latest version on every deployment. Be aware this will cause your instances to be replaced when a new version of the image becomes available. Do not store stateful information on the instance if you are using this image.
AmazonLinux2023(props?)
static latestpublic static latestAmazonLinux2023(props?: AmazonLinux2023ImageSsmParameterProps): IMachineImage
Parameters
Returns
An Amazon Linux 2023 image that is automatically kept up-to-date.
This Machine Image automatically updates to the latest version on every deployment. Be aware this will cause your instances to be replaced when a new version of the image becomes available. Do not store stateful information on the instance if you are using this image.
Windows(version, props?)
static latestpublic static latestWindows(version: WindowsVersion, props?: WindowsImageProps): IMachineImage
Parameters
- version
Windows
Version - props
Windows
Image Props
Returns
A Windows image that is automatically kept up-to-date.
This Machine Image automatically updates to the latest version on every deployment. Be aware this will cause your instances to be replaced when a new version of the image becomes available. Do not store stateful information on the instance if you are using this image.
static lookup(props)
public static lookup(props: LookupMachineImageProps): IMachineImage
Parameters
- props
Lookup
Machine Image Props
Returns
Look up a shared Machine Image using DescribeImages.
The most recent, available, launchable image matching the given filter criteria will be used. Looking up AMIs may take a long time; specify as many filter criteria as possible to narrow down the search.
The AMI selected will be cached in cdk.context.json
and the same value
will be used on future runs. 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.
This function can not be used in environment-agnostic stacks.
SsmParameterAtLaunch(parameterName, options?)
static resolvepublic static resolveSsmParameterAtLaunch(parameterName: string, options?: SsmParameterImageOptions): IMachineImage
Parameters
- parameterName
string
— The name of SSM parameter containing the AMI ID. - options
Ssm
— The parameter image options.Parameter Image Options
Returns
An image specified in SSM parameter store that will be resolved at instance launch time.
The AMI ID will be resolved at instance launch time.
See also: https://docs.aws.amazon.com/autoscaling/ec2/userguide/using-systems-manager-parameters.html