How do I launch Amazon ECS instances with Amazon ECS optimized AMIs?

2 minute read
0

I want to launch instances with Amazon Machine Images (AMIs) optimized for Amazon Elastic Container Service (Amazon ECS).

Resolution

Launch an Amazon ECS optimized container instance with an Amazon ECS-optimized AMI

Complete the following steps:

  1. Open the Amazon EC2 console.
  2. In the Create Instance section, choose Launch Instance.
  3. In the navigation pane, choose AWS Marketplace. Then, enter ecs-optimized in the search bar.
  4. Choose one of the following AMI versions based on your needs:
    Linux AMIs
    Windows AMIs.
    Note: Amazon Linux 1 reached its end of life on December 31, 2023. It's a best practice to upgrade applications to Amazon Linux 2023.

To have your Amazon ECS optimized instance join your Amazon ECS cluster, see Launching an Amazon ECS Linux container instance.

Automate the creation of your Amazon ECS optimized AMIs

Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshoot AWS CLI errors. Also, make sure that you're using the most recent AWS CLI version.

Use AWS Systems Manager parameters to automate and retrieve Amazon ECS optimized AMI metadata.

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[0].Value"

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.

Use SSM parameters as input parameters for AWS CloudFormation templates.

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

Related information

Amazon Machine Images (AMI)

AWS OFFICIAL
AWS OFFICIALUpdated 7 months ago