How do I launch ECS instances with Amazon ECS-optimized AMIs?
Last updated: 2022-04-26
I want to launch instances with Amazon Machine Images (AMIs) optimized for Amazon Elastic Container Service (Amazon ECS). How can I do that?
To launch Amazon ECS-optimized AMIs, you can choose from the following options:
- Launch an Amazon ECS-optimized container instance with an Amazon ECS-optimized AMI from the Amazon Elastic Compute Cloud (Amazon EC2) console
- Automate the creation of your Amazon ECS-optimized AMIs
Launch an Amazon ECS-optimized container instance with an Amazon ECS-optimized AMI
- Open the Amazon EC2 console.
- In the Create Instance section, choose Launch Instance.
- From the left navigation pane, choose AWS Marketplace. Then, enter ecs-optimized in the search bar.
- Choose one of the following AMI versions based on your needs: Amazon Linux AMI, Amazon Linux 2 AMI, Windows Server 2016, or Amazon Linux 2 AMI (ARM).
Important: To get your Amazon ECS-optimized instance to join your Amazon ECS cluster, see Launching an Amazon ECS Linux container instance.
Automate the creation of your Amazon ECS-optimized AMIs
In the following example, the image_id sub-parameter returns the machine image AMI ID for only the current recommended Amazon Linux EC2-optimized AMI version in us-east-1.
aws ssm get-parameters --names /aws/service/ecs/optimized-ami/amazon-linux/recommended/image_id --region us-east-1 --query "Parameters.Value"
You can use the AMI ID to launch your Amazon Linux container instance in us-east-1. You can also modify the command to return the machine image for a specific version and AWS Region.
You can use SSM parameters as input parameters for AWS CloudFormation templates. See the following example:
AWSTemplateFormatVersion: '2010-09-09' Parameters: ImageId: Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id> Description: Use an Image from SSM Parameter Store Default: /aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id Resources: EC2Instance: Type: AWS::EC2::Instance Properties: InstanceType: t3.micro SecurityGroups: [!Ref 'EC2SecurityGroup'] ImageId: !Ref ImageId EC2SecurityGroup: Type: AWS::EC2::SecurityGroup Properties: GroupDescription: SSH access SecurityGroupIngress: - IpProtocol: tcp FromPort: '22' ToPort: '22' CidrIp: 0.0.0.0/0